s3:dbwrap_ctdb: fix an uninitialized variable.
[Samba/bb.git] / source3 / lib / dbwrap_ctdb.c
blobd6fde3560d5b58ffc182cffcace05f13509e6634
1 /*
2 Unix SMB/CIFS implementation.
3 Database interface wrapper around ctdbd
4 Copyright (C) Volker Lendecke 2007-2009
5 Copyright (C) Michael Adam 2009
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>.
21 #include "includes.h"
22 #ifdef CLUSTER_SUPPORT
23 #include "ctdb.h"
24 #include "ctdb_private.h"
25 #include "ctdbd_conn.h"
26 #include "g_lock.h"
28 struct db_ctdb_transaction_handle {
29 struct db_ctdb_ctx *ctx;
31 * we store the reads and writes done under a transaction:
32 * - one list stores both reads and writes (m_all),
33 * - the other just writes (m_write)
35 struct ctdb_marshall_buffer *m_all;
36 struct ctdb_marshall_buffer *m_write;
37 uint32_t nesting;
38 bool nested_cancel;
39 char *lock_name;
42 struct db_ctdb_ctx {
43 struct db_context *db;
44 struct tdb_wrap *wtdb;
45 uint32 db_id;
46 struct db_ctdb_transaction_handle *transaction;
47 struct g_lock_ctx *lock_ctx;
50 struct db_ctdb_rec {
51 struct db_ctdb_ctx *ctdb_ctx;
52 struct ctdb_ltdb_header header;
55 static struct db_record *fetch_locked_internal(struct db_ctdb_ctx *ctx,
56 TALLOC_CTX *mem_ctx,
57 TDB_DATA key,
58 bool persistent);
60 static NTSTATUS tdb_error_to_ntstatus(struct tdb_context *tdb)
62 NTSTATUS status;
63 enum TDB_ERROR tret = tdb_error(tdb);
65 switch (tret) {
66 case TDB_ERR_EXISTS:
67 status = NT_STATUS_OBJECT_NAME_COLLISION;
68 break;
69 case TDB_ERR_NOEXIST:
70 status = NT_STATUS_OBJECT_NAME_NOT_FOUND;
71 break;
72 default:
73 status = NT_STATUS_INTERNAL_DB_CORRUPTION;
74 break;
77 return status;
81 /**
82 * fetch a record from the tdb, separating out the header
83 * information and returning the body of the record.
85 static NTSTATUS db_ctdb_ltdb_fetch(struct db_ctdb_ctx *db,
86 TDB_DATA key,
87 struct ctdb_ltdb_header *header,
88 TALLOC_CTX *mem_ctx,
89 TDB_DATA *data)
91 TDB_DATA rec;
92 NTSTATUS status;
94 rec = tdb_fetch(db->wtdb->tdb, key);
95 if (rec.dsize < sizeof(struct ctdb_ltdb_header)) {
96 status = NT_STATUS_NOT_FOUND;
97 if (data) {
98 ZERO_STRUCTP(data);
100 if (header) {
101 header->dmaster = (uint32_t)-1;
102 header->rsn = 0;
104 goto done;
107 if (header) {
108 *header = *(struct ctdb_ltdb_header *)rec.dptr;
111 if (data) {
112 data->dsize = rec.dsize - sizeof(struct ctdb_ltdb_header);
113 if (data->dsize == 0) {
114 data->dptr = NULL;
115 } else {
116 data->dptr = (unsigned char *)talloc_memdup(mem_ctx,
117 rec.dptr
118 + sizeof(struct ctdb_ltdb_header),
119 data->dsize);
120 if (data->dptr == NULL) {
121 status = NT_STATUS_NO_MEMORY;
122 goto done;
127 status = NT_STATUS_OK;
129 done:
130 SAFE_FREE(rec.dptr);
131 return status;
135 * Store a record together with the ctdb record header
136 * in the local copy of the database.
138 static NTSTATUS db_ctdb_ltdb_store(struct db_ctdb_ctx *db,
139 TDB_DATA key,
140 struct ctdb_ltdb_header *header,
141 TDB_DATA data)
143 TALLOC_CTX *tmp_ctx = talloc_stackframe();
144 TDB_DATA rec;
145 int ret;
147 rec.dsize = data.dsize + sizeof(struct ctdb_ltdb_header);
148 rec.dptr = (uint8_t *)talloc_size(tmp_ctx, rec.dsize);
150 if (rec.dptr == NULL) {
151 talloc_free(tmp_ctx);
152 return NT_STATUS_NO_MEMORY;
155 memcpy(rec.dptr, header, sizeof(struct ctdb_ltdb_header));
156 memcpy(sizeof(struct ctdb_ltdb_header) + (uint8_t *)rec.dptr, data.dptr, data.dsize);
158 ret = tdb_store(db->wtdb->tdb, key, rec, TDB_REPLACE);
160 talloc_free(tmp_ctx);
162 return (ret == 0) ? NT_STATUS_OK
163 : tdb_error_to_ntstatus(db->wtdb->tdb);
168 form a ctdb_rec_data record from a key/data pair
170 note that header may be NULL. If not NULL then it is included in the data portion
171 of the record
173 static struct ctdb_rec_data *db_ctdb_marshall_record(TALLOC_CTX *mem_ctx, uint32_t reqid,
174 TDB_DATA key,
175 struct ctdb_ltdb_header *header,
176 TDB_DATA data)
178 size_t length;
179 struct ctdb_rec_data *d;
181 length = offsetof(struct ctdb_rec_data, data) + key.dsize +
182 data.dsize + (header?sizeof(*header):0);
183 d = (struct ctdb_rec_data *)talloc_size(mem_ctx, length);
184 if (d == NULL) {
185 return NULL;
187 d->length = length;
188 d->reqid = reqid;
189 d->keylen = key.dsize;
190 memcpy(&d->data[0], key.dptr, key.dsize);
191 if (header) {
192 d->datalen = data.dsize + sizeof(*header);
193 memcpy(&d->data[key.dsize], header, sizeof(*header));
194 memcpy(&d->data[key.dsize+sizeof(*header)], data.dptr, data.dsize);
195 } else {
196 d->datalen = data.dsize;
197 memcpy(&d->data[key.dsize], data.dptr, data.dsize);
199 return d;
203 /* helper function for marshalling multiple records */
204 static struct ctdb_marshall_buffer *db_ctdb_marshall_add(TALLOC_CTX *mem_ctx,
205 struct ctdb_marshall_buffer *m,
206 uint64_t db_id,
207 uint32_t reqid,
208 TDB_DATA key,
209 struct ctdb_ltdb_header *header,
210 TDB_DATA data)
212 struct ctdb_rec_data *r;
213 size_t m_size, r_size;
214 struct ctdb_marshall_buffer *m2 = NULL;
216 r = db_ctdb_marshall_record(talloc_tos(), reqid, key, header, data);
217 if (r == NULL) {
218 talloc_free(m);
219 return NULL;
222 if (m == NULL) {
223 m = (struct ctdb_marshall_buffer *)talloc_zero_size(
224 mem_ctx, offsetof(struct ctdb_marshall_buffer, data));
225 if (m == NULL) {
226 goto done;
228 m->db_id = db_id;
231 m_size = talloc_get_size(m);
232 r_size = talloc_get_size(r);
234 m2 = (struct ctdb_marshall_buffer *)talloc_realloc_size(
235 mem_ctx, m, m_size + r_size);
236 if (m2 == NULL) {
237 talloc_free(m);
238 goto done;
241 memcpy(m_size + (uint8_t *)m2, r, r_size);
243 m2->count++;
245 done:
246 talloc_free(r);
247 return m2;
250 /* we've finished marshalling, return a data blob with the marshalled records */
251 static TDB_DATA db_ctdb_marshall_finish(struct ctdb_marshall_buffer *m)
253 TDB_DATA data;
254 data.dptr = (uint8_t *)m;
255 data.dsize = talloc_get_size(m);
256 return data;
260 loop over a marshalling buffer
262 - pass r==NULL to start
263 - loop the number of times indicated by m->count
265 static struct ctdb_rec_data *db_ctdb_marshall_loop_next(struct ctdb_marshall_buffer *m, struct ctdb_rec_data *r,
266 uint32_t *reqid,
267 struct ctdb_ltdb_header *header,
268 TDB_DATA *key, TDB_DATA *data)
270 if (r == NULL) {
271 r = (struct ctdb_rec_data *)&m->data[0];
272 } else {
273 r = (struct ctdb_rec_data *)(r->length + (uint8_t *)r);
276 if (reqid != NULL) {
277 *reqid = r->reqid;
280 if (key != NULL) {
281 key->dptr = &r->data[0];
282 key->dsize = r->keylen;
284 if (data != NULL) {
285 data->dptr = &r->data[r->keylen];
286 data->dsize = r->datalen;
287 if (header != NULL) {
288 data->dptr += sizeof(*header);
289 data->dsize -= sizeof(*header);
293 if (header != NULL) {
294 if (r->datalen < sizeof(*header)) {
295 return NULL;
297 *header = *(struct ctdb_ltdb_header *)&r->data[r->keylen];
300 return r;
304 * CTDB transaction destructor
306 static int db_ctdb_transaction_destructor(struct db_ctdb_transaction_handle *h)
308 NTSTATUS status;
310 status = g_lock_unlock(h->ctx->lock_ctx, h->lock_name);
311 if (!NT_STATUS_IS_OK(status)) {
312 DEBUG(0, ("g_lock_unlock failed: %s\n", nt_errstr(status)));
313 return -1;
315 return 0;
319 * CTDB dbwrap API: transaction_start function
320 * starts a transaction on a persistent database
322 static int db_ctdb_transaction_start(struct db_context *db)
324 struct db_ctdb_transaction_handle *h;
325 NTSTATUS status;
326 struct db_ctdb_ctx *ctx = talloc_get_type_abort(db->private_data,
327 struct db_ctdb_ctx);
329 if (!db->persistent) {
330 DEBUG(0,("transactions not supported on non-persistent database 0x%08x\n",
331 ctx->db_id));
332 return -1;
335 if (ctx->transaction) {
336 ctx->transaction->nesting++;
337 return 0;
340 h = talloc_zero(db, struct db_ctdb_transaction_handle);
341 if (h == NULL) {
342 DEBUG(0,(__location__ " oom for transaction handle\n"));
343 return -1;
346 h->ctx = ctx;
348 h->lock_name = talloc_asprintf(h, "transaction_db_0x%08x",
349 (unsigned int)ctx->db_id);
350 if (h->lock_name == NULL) {
351 DEBUG(0, ("talloc_asprintf failed\n"));
352 TALLOC_FREE(h);
353 return -1;
357 * Wait a day, i.e. forever...
359 status = g_lock_lock(ctx->lock_ctx, h->lock_name, G_LOCK_WRITE,
360 timeval_set(86400, 0));
361 if (!NT_STATUS_IS_OK(status)) {
362 DEBUG(0, ("g_lock_lock failed: %s\n", nt_errstr(status)));
363 TALLOC_FREE(h);
364 return -1;
367 talloc_set_destructor(h, db_ctdb_transaction_destructor);
369 ctx->transaction = h;
371 DEBUG(5,(__location__ " Started transaction on db 0x%08x\n", ctx->db_id));
373 return 0;
376 static bool pull_newest_from_marshall_buffer(struct ctdb_marshall_buffer *buf,
377 TDB_DATA key,
378 struct ctdb_ltdb_header *pheader,
379 TALLOC_CTX *mem_ctx,
380 TDB_DATA *pdata)
382 struct ctdb_rec_data *rec = NULL;
383 struct ctdb_ltdb_header h;
384 bool found = false;
385 TDB_DATA data;
386 int i;
388 if (buf == NULL) {
389 return false;
392 ZERO_STRUCT(h);
393 ZERO_STRUCT(data);
396 * Walk the list of records written during this
397 * transaction. If we want to read one we have already
398 * written, return the last written sample. Thus we do not do
399 * a "break;" for the first hit, this record might have been
400 * overwritten later.
403 for (i=0; i<buf->count; i++) {
404 TDB_DATA tkey, tdata;
405 uint32_t reqid;
407 rec = db_ctdb_marshall_loop_next(buf, rec, &reqid, &h, &tkey,
408 &tdata);
409 if (rec == NULL) {
410 return false;
413 if (tdb_data_equal(key, tkey)) {
414 found = true;
415 data = tdata;
419 if (!found) {
420 return false;
423 if (pdata != NULL) {
424 data.dptr = (uint8_t *)talloc_memdup(mem_ctx, data.dptr,
425 data.dsize);
426 if ((data.dsize != 0) && (data.dptr == NULL)) {
427 return false;
429 *pdata = data;
432 if (pheader != NULL) {
433 *pheader = h;
436 return true;
440 fetch a record inside a transaction
442 static int db_ctdb_transaction_fetch(struct db_ctdb_ctx *db,
443 TALLOC_CTX *mem_ctx,
444 TDB_DATA key, TDB_DATA *data)
446 struct db_ctdb_transaction_handle *h = db->transaction;
447 NTSTATUS status;
448 bool found;
450 found = pull_newest_from_marshall_buffer(h->m_write, key, NULL,
451 mem_ctx, data);
452 if (found) {
453 return 0;
456 status = db_ctdb_ltdb_fetch(h->ctx, key, NULL, mem_ctx, data);
458 if (NT_STATUS_EQUAL(status, NT_STATUS_NOT_FOUND)) {
459 *data = tdb_null;
460 } else if (!NT_STATUS_IS_OK(status)) {
461 return -1;
464 h->m_all = db_ctdb_marshall_add(h, h->m_all, h->ctx->db_id, 1, key,
465 NULL, *data);
466 if (h->m_all == NULL) {
467 DEBUG(0,(__location__ " Failed to add to marshalling "
468 "record\n"));
469 data->dsize = 0;
470 talloc_free(data->dptr);
471 return -1;
474 return 0;
478 static NTSTATUS db_ctdb_store_transaction(struct db_record *rec, TDB_DATA data, int flag);
479 static NTSTATUS db_ctdb_delete_transaction(struct db_record *rec);
481 static struct db_record *db_ctdb_fetch_locked_transaction(struct db_ctdb_ctx *ctx,
482 TALLOC_CTX *mem_ctx,
483 TDB_DATA key)
485 struct db_record *result;
486 TDB_DATA ctdb_data;
488 if (!(result = talloc(mem_ctx, struct db_record))) {
489 DEBUG(0, ("talloc failed\n"));
490 return NULL;
493 result->private_data = ctx->transaction;
495 result->key.dsize = key.dsize;
496 result->key.dptr = (uint8 *)talloc_memdup(result, key.dptr, key.dsize);
497 if (result->key.dptr == NULL) {
498 DEBUG(0, ("talloc failed\n"));
499 TALLOC_FREE(result);
500 return NULL;
503 result->store = db_ctdb_store_transaction;
504 result->delete_rec = db_ctdb_delete_transaction;
506 if (pull_newest_from_marshall_buffer(ctx->transaction->m_write, key,
507 NULL, result, &result->value)) {
508 return result;
511 ctdb_data = tdb_fetch(ctx->wtdb->tdb, key);
512 if (ctdb_data.dptr == NULL) {
513 /* create the record */
514 result->value = tdb_null;
515 return result;
518 result->value.dsize = ctdb_data.dsize - sizeof(struct ctdb_ltdb_header);
519 result->value.dptr = NULL;
521 if ((result->value.dsize != 0)
522 && !(result->value.dptr = (uint8 *)talloc_memdup(
523 result, ctdb_data.dptr + sizeof(struct ctdb_ltdb_header),
524 result->value.dsize))) {
525 DEBUG(0, ("talloc failed\n"));
526 TALLOC_FREE(result);
529 SAFE_FREE(ctdb_data.dptr);
531 return result;
534 static int db_ctdb_record_destructor(struct db_record **recp)
536 struct db_record *rec = talloc_get_type_abort(*recp, struct db_record);
537 struct db_ctdb_transaction_handle *h = talloc_get_type_abort(
538 rec->private_data, struct db_ctdb_transaction_handle);
539 int ret = h->ctx->db->transaction_commit(h->ctx->db);
540 if (ret != 0) {
541 DEBUG(0,(__location__ " transaction_commit failed\n"));
543 return 0;
547 auto-create a transaction for persistent databases
549 static struct db_record *db_ctdb_fetch_locked_persistent(struct db_ctdb_ctx *ctx,
550 TALLOC_CTX *mem_ctx,
551 TDB_DATA key)
553 int res;
554 struct db_record *rec, **recp;
556 res = db_ctdb_transaction_start(ctx->db);
557 if (res == -1) {
558 return NULL;
561 rec = db_ctdb_fetch_locked_transaction(ctx, mem_ctx, key);
562 if (rec == NULL) {
563 ctx->db->transaction_cancel(ctx->db);
564 return NULL;
567 /* destroy this transaction when we release the lock */
568 recp = talloc(rec, struct db_record *);
569 if (recp == NULL) {
570 ctx->db->transaction_cancel(ctx->db);
571 talloc_free(rec);
572 return NULL;
574 *recp = rec;
575 talloc_set_destructor(recp, db_ctdb_record_destructor);
576 return rec;
581 stores a record inside a transaction
583 static NTSTATUS db_ctdb_transaction_store(struct db_ctdb_transaction_handle *h,
584 TDB_DATA key, TDB_DATA data)
586 TALLOC_CTX *tmp_ctx = talloc_new(h);
587 TDB_DATA rec;
588 struct ctdb_ltdb_header header;
590 ZERO_STRUCT(header);
592 /* we need the header so we can update the RSN */
594 if (!pull_newest_from_marshall_buffer(h->m_write, key, &header,
595 NULL, NULL)) {
597 rec = tdb_fetch(h->ctx->wtdb->tdb, key);
599 if (rec.dptr != NULL) {
600 memcpy(&header, rec.dptr,
601 sizeof(struct ctdb_ltdb_header));
602 rec.dsize -= sizeof(struct ctdb_ltdb_header);
605 * a special case, we are writing the same
606 * data that is there now
608 if (data.dsize == rec.dsize &&
609 memcmp(data.dptr,
610 rec.dptr + sizeof(struct ctdb_ltdb_header),
611 data.dsize) == 0) {
612 SAFE_FREE(rec.dptr);
613 talloc_free(tmp_ctx);
614 return NT_STATUS_OK;
617 SAFE_FREE(rec.dptr);
620 header.dmaster = get_my_vnn();
621 header.rsn++;
623 h->m_all = db_ctdb_marshall_add(h, h->m_all, h->ctx->db_id, 0, key,
624 NULL, data);
625 if (h->m_all == NULL) {
626 DEBUG(0,(__location__ " Failed to add to marshalling "
627 "record\n"));
628 talloc_free(tmp_ctx);
629 return NT_STATUS_NO_MEMORY;
632 h->m_write = db_ctdb_marshall_add(h, h->m_write, h->ctx->db_id, 0, key, &header, data);
633 if (h->m_write == NULL) {
634 DEBUG(0,(__location__ " Failed to add to marshalling record\n"));
635 talloc_free(tmp_ctx);
636 return NT_STATUS_NO_MEMORY;
639 talloc_free(tmp_ctx);
640 return NT_STATUS_OK;
645 a record store inside a transaction
647 static NTSTATUS db_ctdb_store_transaction(struct db_record *rec, TDB_DATA data, int flag)
649 struct db_ctdb_transaction_handle *h = talloc_get_type_abort(
650 rec->private_data, struct db_ctdb_transaction_handle);
651 NTSTATUS status;
653 status = db_ctdb_transaction_store(h, rec->key, data);
654 return status;
658 a record delete inside a transaction
660 static NTSTATUS db_ctdb_delete_transaction(struct db_record *rec)
662 struct db_ctdb_transaction_handle *h = talloc_get_type_abort(
663 rec->private_data, struct db_ctdb_transaction_handle);
664 NTSTATUS status;
666 status = db_ctdb_transaction_store(h, rec->key, tdb_null);
667 return status;
671 * Fetch the db sequence number of a persistent db directly from the db.
673 static NTSTATUS db_ctdb_fetch_db_seqnum_from_db(struct db_ctdb_ctx *db,
674 uint64_t *seqnum)
676 NTSTATUS status;
677 const char *keyname = CTDB_DB_SEQNUM_KEY;
678 TDB_DATA key;
679 TDB_DATA data;
680 struct ctdb_ltdb_header header;
681 TALLOC_CTX *mem_ctx = talloc_stackframe();
683 if (seqnum == NULL) {
684 return NT_STATUS_INVALID_PARAMETER;
687 key.dptr = (uint8_t *)discard_const(keyname);
688 key.dsize = strlen(keyname) + 1;
690 status = db_ctdb_ltdb_fetch(db, key, &header, mem_ctx, &data);
691 if (!NT_STATUS_IS_OK(status) &&
692 !NT_STATUS_EQUAL(status, NT_STATUS_NOT_FOUND))
694 goto done;
697 status = NT_STATUS_OK;
699 if (data.dsize != sizeof(uint64_t)) {
700 *seqnum = 0;
701 goto done;
704 *seqnum = *(uint64_t *)data.dptr;
706 done:
707 TALLOC_FREE(mem_ctx);
708 return status;
712 * Store the database sequence number inside a transaction.
714 static NTSTATUS db_ctdb_store_db_seqnum(struct db_ctdb_transaction_handle *h,
715 uint64_t seqnum)
717 NTSTATUS status;
718 const char *keyname = CTDB_DB_SEQNUM_KEY;
719 TDB_DATA key;
720 TDB_DATA data;
722 key.dptr = (uint8_t *)discard_const(keyname);
723 key.dsize = strlen(keyname);
725 data.dptr = (uint8_t *)&seqnum;
726 data.dsize = sizeof(uint64_t);
728 status = db_ctdb_transaction_store(h, key, data);
730 return status;
734 commit a transaction
736 static int db_ctdb_transaction_commit(struct db_context *db)
738 struct db_ctdb_ctx *ctx = talloc_get_type_abort(db->private_data,
739 struct db_ctdb_ctx);
740 NTSTATUS rets;
741 int status;
742 struct db_ctdb_transaction_handle *h = ctx->transaction;
743 uint64_t old_seqnum, new_seqnum;
744 int ret;
746 if (h == NULL) {
747 DEBUG(0,(__location__ " transaction commit with no open transaction on db 0x%08x\n", ctx->db_id));
748 return -1;
751 if (h->nested_cancel) {
752 db->transaction_cancel(db);
753 DEBUG(5,(__location__ " Failed transaction commit after nested cancel\n"));
754 return -1;
757 if (h->nesting != 0) {
758 h->nesting--;
759 return 0;
762 DEBUG(5,(__location__ " Commit transaction on db 0x%08x\n", ctx->db_id));
765 * As the last db action before committing, bump the database sequence
766 * number. Note that this undoes all changes to the seqnum records
767 * performed under the transaction. This record is not meant to be
768 * modified by user interaction. It is for internal use only...
770 rets = db_ctdb_fetch_db_seqnum_from_db(ctx, &old_seqnum);
771 if (!NT_STATUS_IS_OK(rets)) {
772 DEBUG(1, (__location__ " failed to fetch the db sequence number "
773 "in transaction commit on db 0x%08x\n", ctx->db_id));
774 ret = -1;
775 goto done;
778 new_seqnum = old_seqnum + 1;
780 rets = db_ctdb_store_db_seqnum(h, new_seqnum);
781 if (!NT_STATUS_IS_OK(rets)) {
782 DEBUG(1, (__location__ "failed to store the db sequence number "
783 " in transaction commit on db 0x%08x\n", ctx->db_id));
784 ret = -1;
785 goto done;
788 again:
789 if (h->m_write == NULL) {
790 /* no changes were made, potentially after a retry */
791 goto done;
794 /* tell ctdbd to commit to the other nodes */
795 rets = ctdbd_control_local(messaging_ctdbd_connection(),
796 CTDB_CONTROL_TRANS3_COMMIT,
797 h->ctx->db_id, 0,
798 db_ctdb_marshall_finish(h->m_write),
799 NULL, NULL, &status);
800 if (!NT_STATUS_IS_OK(rets) || status != 0) {
802 * The TRANS3_COMMIT control should only possibly fail when a
803 * recovery has been running concurrently. In any case, the db
804 * will be the same on all nodes, either the new copy or the
805 * old copy. This can be detected by comparing the old and new
806 * local sequence numbers.
808 rets = db_ctdb_fetch_db_seqnum_from_db(ctx, &new_seqnum);
809 if (!NT_STATUS_IS_OK(rets)) {
810 DEBUG(1, (__location__ " failed to refetch db sequence "
811 "number after failed TRANS3_COMMIT\n"));
812 ret = -1;
813 goto done;
816 if (new_seqnum == old_seqnum) {
817 /* Recovery prevented all our changes: retry. */
818 goto again;
819 } else if (new_seqnum != (old_seqnum + 1)) {
820 DEBUG(0, (__location__ " ERROR: new_seqnum[%lu] != "
821 "old_seqnum[%lu] + (0 or 1) after failed "
822 "TRANS3_COMMIT - this should not happen!\n",
823 (unsigned long)new_seqnum,
824 (unsigned long)old_seqnum));
825 ret = -1;
826 goto done;
829 * Recovery propagated our changes to all nodes, completing
830 * our commit for us - succeed.
834 ret = 0;
836 done:
837 h->ctx->transaction = NULL;
838 talloc_free(h);
839 return 0;
844 cancel a transaction
846 static int db_ctdb_transaction_cancel(struct db_context *db)
848 struct db_ctdb_ctx *ctx = talloc_get_type_abort(db->private_data,
849 struct db_ctdb_ctx);
850 struct db_ctdb_transaction_handle *h = ctx->transaction;
852 if (h == NULL) {
853 DEBUG(0,(__location__ " transaction cancel with no open transaction on db 0x%08x\n", ctx->db_id));
854 return -1;
857 if (h->nesting != 0) {
858 h->nesting--;
859 h->nested_cancel = true;
860 return 0;
863 DEBUG(5,(__location__ " Cancel transaction on db 0x%08x\n", ctx->db_id));
865 ctx->transaction = NULL;
866 talloc_free(h);
867 return 0;
871 static NTSTATUS db_ctdb_store(struct db_record *rec, TDB_DATA data, int flag)
873 struct db_ctdb_rec *crec = talloc_get_type_abort(
874 rec->private_data, struct db_ctdb_rec);
876 return db_ctdb_ltdb_store(crec->ctdb_ctx, rec->key, &(crec->header), data);
881 static NTSTATUS db_ctdb_delete(struct db_record *rec)
883 TDB_DATA data;
886 * We have to store the header with empty data. TODO: Fix the
887 * tdb-level cleanup
890 ZERO_STRUCT(data);
892 return db_ctdb_store(rec, data, 0);
896 static int db_ctdb_record_destr(struct db_record* data)
898 struct db_ctdb_rec *crec = talloc_get_type_abort(
899 data->private_data, struct db_ctdb_rec);
901 DEBUG(10, (DEBUGLEVEL > 10
902 ? "Unlocking db %u key %s\n"
903 : "Unlocking db %u key %.20s\n",
904 (int)crec->ctdb_ctx->db_id,
905 hex_encode_talloc(data, (unsigned char *)data->key.dptr,
906 data->key.dsize)));
908 if (tdb_chainunlock(crec->ctdb_ctx->wtdb->tdb, data->key) != 0) {
909 DEBUG(0, ("tdb_chainunlock failed\n"));
910 return -1;
913 return 0;
916 static struct db_record *fetch_locked_internal(struct db_ctdb_ctx *ctx,
917 TALLOC_CTX *mem_ctx,
918 TDB_DATA key,
919 bool persistent)
921 struct db_record *result;
922 struct db_ctdb_rec *crec;
923 NTSTATUS status;
924 TDB_DATA ctdb_data;
925 int migrate_attempts = 0;
927 if (!(result = talloc(mem_ctx, struct db_record))) {
928 DEBUG(0, ("talloc failed\n"));
929 return NULL;
932 if (!(crec = TALLOC_ZERO_P(result, struct db_ctdb_rec))) {
933 DEBUG(0, ("talloc failed\n"));
934 TALLOC_FREE(result);
935 return NULL;
938 result->private_data = (void *)crec;
939 crec->ctdb_ctx = ctx;
941 result->key.dsize = key.dsize;
942 result->key.dptr = (uint8 *)talloc_memdup(result, key.dptr, key.dsize);
943 if (result->key.dptr == NULL) {
944 DEBUG(0, ("talloc failed\n"));
945 TALLOC_FREE(result);
946 return NULL;
950 * Do a blocking lock on the record
952 again:
954 if (DEBUGLEVEL >= 10) {
955 char *keystr = hex_encode_talloc(result, key.dptr, key.dsize);
956 DEBUG(10, (DEBUGLEVEL > 10
957 ? "Locking db %u key %s\n"
958 : "Locking db %u key %.20s\n",
959 (int)crec->ctdb_ctx->db_id, keystr));
960 TALLOC_FREE(keystr);
963 if (tdb_chainlock(ctx->wtdb->tdb, key) != 0) {
964 DEBUG(3, ("tdb_chainlock failed\n"));
965 TALLOC_FREE(result);
966 return NULL;
969 result->store = db_ctdb_store;
970 result->delete_rec = db_ctdb_delete;
971 talloc_set_destructor(result, db_ctdb_record_destr);
973 ctdb_data = tdb_fetch(ctx->wtdb->tdb, key);
976 * See if we have a valid record and we are the dmaster. If so, we can
977 * take the shortcut and just return it.
980 if ((ctdb_data.dptr == NULL) ||
981 (ctdb_data.dsize < sizeof(struct ctdb_ltdb_header)) ||
982 ((struct ctdb_ltdb_header *)ctdb_data.dptr)->dmaster != get_my_vnn()
983 #if 0
984 || (random() % 2 != 0)
985 #endif
987 SAFE_FREE(ctdb_data.dptr);
988 tdb_chainunlock(ctx->wtdb->tdb, key);
989 talloc_set_destructor(result, NULL);
991 migrate_attempts += 1;
993 DEBUG(10, ("ctdb_data.dptr = %p, dmaster = %u (%u)\n",
994 ctdb_data.dptr, ctdb_data.dptr ?
995 ((struct ctdb_ltdb_header *)ctdb_data.dptr)->dmaster : -1,
996 get_my_vnn()));
998 status = ctdbd_migrate(messaging_ctdbd_connection(),ctx->db_id, key);
999 if (!NT_STATUS_IS_OK(status)) {
1000 DEBUG(5, ("ctdb_migrate failed: %s\n",
1001 nt_errstr(status)));
1002 TALLOC_FREE(result);
1003 return NULL;
1005 /* now its migrated, try again */
1006 goto again;
1009 if (migrate_attempts > 10) {
1010 DEBUG(0, ("db_ctdb_fetch_locked needed %d attempts\n",
1011 migrate_attempts));
1014 memcpy(&crec->header, ctdb_data.dptr, sizeof(crec->header));
1016 result->value.dsize = ctdb_data.dsize - sizeof(crec->header);
1017 result->value.dptr = NULL;
1019 if ((result->value.dsize != 0)
1020 && !(result->value.dptr = (uint8 *)talloc_memdup(
1021 result, ctdb_data.dptr + sizeof(crec->header),
1022 result->value.dsize))) {
1023 DEBUG(0, ("talloc failed\n"));
1024 TALLOC_FREE(result);
1027 SAFE_FREE(ctdb_data.dptr);
1029 return result;
1032 static struct db_record *db_ctdb_fetch_locked(struct db_context *db,
1033 TALLOC_CTX *mem_ctx,
1034 TDB_DATA key)
1036 struct db_ctdb_ctx *ctx = talloc_get_type_abort(db->private_data,
1037 struct db_ctdb_ctx);
1039 if (ctx->transaction != NULL) {
1040 return db_ctdb_fetch_locked_transaction(ctx, mem_ctx, key);
1043 if (db->persistent) {
1044 return db_ctdb_fetch_locked_persistent(ctx, mem_ctx, key);
1047 return fetch_locked_internal(ctx, mem_ctx, key, db->persistent);
1051 fetch (unlocked, no migration) operation on ctdb
1053 static int db_ctdb_fetch(struct db_context *db, TALLOC_CTX *mem_ctx,
1054 TDB_DATA key, TDB_DATA *data)
1056 struct db_ctdb_ctx *ctx = talloc_get_type_abort(db->private_data,
1057 struct db_ctdb_ctx);
1058 NTSTATUS status;
1059 TDB_DATA ctdb_data;
1061 if (ctx->transaction) {
1062 return db_ctdb_transaction_fetch(ctx, mem_ctx, key, data);
1065 /* try a direct fetch */
1066 ctdb_data = tdb_fetch(ctx->wtdb->tdb, key);
1069 * See if we have a valid record and we are the dmaster. If so, we can
1070 * take the shortcut and just return it.
1071 * we bypass the dmaster check for persistent databases
1073 if ((ctdb_data.dptr != NULL) &&
1074 (ctdb_data.dsize >= sizeof(struct ctdb_ltdb_header)) &&
1075 (db->persistent ||
1076 ((struct ctdb_ltdb_header *)ctdb_data.dptr)->dmaster == get_my_vnn())) {
1077 /* we are the dmaster - avoid the ctdb protocol op */
1079 data->dsize = ctdb_data.dsize - sizeof(struct ctdb_ltdb_header);
1080 if (data->dsize == 0) {
1081 SAFE_FREE(ctdb_data.dptr);
1082 data->dptr = NULL;
1083 return 0;
1086 data->dptr = (uint8 *)talloc_memdup(
1087 mem_ctx, ctdb_data.dptr+sizeof(struct ctdb_ltdb_header),
1088 data->dsize);
1090 SAFE_FREE(ctdb_data.dptr);
1092 if (data->dptr == NULL) {
1093 return -1;
1095 return 0;
1098 SAFE_FREE(ctdb_data.dptr);
1100 /* we weren't able to get it locally - ask ctdb to fetch it for us */
1101 status = ctdbd_fetch(messaging_ctdbd_connection(),ctx->db_id, key, mem_ctx, data);
1102 if (!NT_STATUS_IS_OK(status)) {
1103 DEBUG(5, ("ctdbd_fetch failed: %s\n", nt_errstr(status)));
1104 return -1;
1107 return 0;
1110 struct traverse_state {
1111 struct db_context *db;
1112 int (*fn)(struct db_record *rec, void *private_data);
1113 void *private_data;
1116 static void traverse_callback(TDB_DATA key, TDB_DATA data, void *private_data)
1118 struct traverse_state *state = (struct traverse_state *)private_data;
1119 struct db_record *rec;
1120 TALLOC_CTX *tmp_ctx = talloc_new(state->db);
1121 /* we have to give them a locked record to prevent races */
1122 rec = db_ctdb_fetch_locked(state->db, tmp_ctx, key);
1123 if (rec && rec->value.dsize > 0) {
1124 state->fn(rec, state->private_data);
1126 talloc_free(tmp_ctx);
1129 static int traverse_persistent_callback(TDB_CONTEXT *tdb, TDB_DATA kbuf, TDB_DATA dbuf,
1130 void *private_data)
1132 struct traverse_state *state = (struct traverse_state *)private_data;
1133 struct db_record *rec;
1134 TALLOC_CTX *tmp_ctx = talloc_new(state->db);
1135 int ret = 0;
1136 /* we have to give them a locked record to prevent races */
1137 rec = db_ctdb_fetch_locked(state->db, tmp_ctx, kbuf);
1138 if (rec && rec->value.dsize > 0) {
1139 ret = state->fn(rec, state->private_data);
1141 talloc_free(tmp_ctx);
1142 return ret;
1145 static int db_ctdb_traverse(struct db_context *db,
1146 int (*fn)(struct db_record *rec,
1147 void *private_data),
1148 void *private_data)
1150 struct db_ctdb_ctx *ctx = talloc_get_type_abort(db->private_data,
1151 struct db_ctdb_ctx);
1152 struct traverse_state state;
1154 state.db = db;
1155 state.fn = fn;
1156 state.private_data = private_data;
1158 if (db->persistent) {
1159 /* for persistent databases we don't need to do a ctdb traverse,
1160 we can do a faster local traverse */
1161 return tdb_traverse(ctx->wtdb->tdb, traverse_persistent_callback, &state);
1165 ctdbd_traverse(ctx->db_id, traverse_callback, &state);
1166 return 0;
1169 static NTSTATUS db_ctdb_store_deny(struct db_record *rec, TDB_DATA data, int flag)
1171 return NT_STATUS_MEDIA_WRITE_PROTECTED;
1174 static NTSTATUS db_ctdb_delete_deny(struct db_record *rec)
1176 return NT_STATUS_MEDIA_WRITE_PROTECTED;
1179 static void traverse_read_callback(TDB_DATA key, TDB_DATA data, void *private_data)
1181 struct traverse_state *state = (struct traverse_state *)private_data;
1182 struct db_record rec;
1183 rec.key = key;
1184 rec.value = data;
1185 rec.store = db_ctdb_store_deny;
1186 rec.delete_rec = db_ctdb_delete_deny;
1187 rec.private_data = state->db;
1188 state->fn(&rec, state->private_data);
1191 static int traverse_persistent_callback_read(TDB_CONTEXT *tdb, TDB_DATA kbuf, TDB_DATA dbuf,
1192 void *private_data)
1194 struct traverse_state *state = (struct traverse_state *)private_data;
1195 struct db_record rec;
1196 rec.key = kbuf;
1197 rec.value = dbuf;
1198 rec.store = db_ctdb_store_deny;
1199 rec.delete_rec = db_ctdb_delete_deny;
1200 rec.private_data = state->db;
1202 if (rec.value.dsize <= sizeof(struct ctdb_ltdb_header)) {
1203 /* a deleted record */
1204 return 0;
1206 rec.value.dsize -= sizeof(struct ctdb_ltdb_header);
1207 rec.value.dptr += sizeof(struct ctdb_ltdb_header);
1209 return state->fn(&rec, state->private_data);
1212 static int db_ctdb_traverse_read(struct db_context *db,
1213 int (*fn)(struct db_record *rec,
1214 void *private_data),
1215 void *private_data)
1217 struct db_ctdb_ctx *ctx = talloc_get_type_abort(db->private_data,
1218 struct db_ctdb_ctx);
1219 struct traverse_state state;
1221 state.db = db;
1222 state.fn = fn;
1223 state.private_data = private_data;
1225 if (db->persistent) {
1226 /* for persistent databases we don't need to do a ctdb traverse,
1227 we can do a faster local traverse */
1228 return tdb_traverse_read(ctx->wtdb->tdb, traverse_persistent_callback_read, &state);
1231 ctdbd_traverse(ctx->db_id, traverse_read_callback, &state);
1232 return 0;
1235 static int db_ctdb_get_seqnum(struct db_context *db)
1237 struct db_ctdb_ctx *ctx = talloc_get_type_abort(db->private_data,
1238 struct db_ctdb_ctx);
1239 return tdb_get_seqnum(ctx->wtdb->tdb);
1242 static int db_ctdb_get_flags(struct db_context *db)
1244 struct db_ctdb_ctx *ctx = talloc_get_type_abort(db->private_data,
1245 struct db_ctdb_ctx);
1246 return tdb_get_flags(ctx->wtdb->tdb);
1249 struct db_context *db_open_ctdb(TALLOC_CTX *mem_ctx,
1250 const char *name,
1251 int hash_size, int tdb_flags,
1252 int open_flags, mode_t mode)
1254 struct db_context *result;
1255 struct db_ctdb_ctx *db_ctdb;
1256 char *db_path;
1257 struct ctdbd_connection *conn;
1259 if (!lp_clustering()) {
1260 DEBUG(10, ("Clustering disabled -- no ctdb\n"));
1261 return NULL;
1264 if (!(result = TALLOC_ZERO_P(mem_ctx, struct db_context))) {
1265 DEBUG(0, ("talloc failed\n"));
1266 TALLOC_FREE(result);
1267 return NULL;
1270 if (!(db_ctdb = TALLOC_P(result, struct db_ctdb_ctx))) {
1271 DEBUG(0, ("talloc failed\n"));
1272 TALLOC_FREE(result);
1273 return NULL;
1276 db_ctdb->transaction = NULL;
1277 db_ctdb->db = result;
1279 conn = messaging_ctdbd_connection();
1281 if (!NT_STATUS_IS_OK(ctdbd_db_attach(conn, name, &db_ctdb->db_id, tdb_flags))) {
1282 DEBUG(0, ("ctdbd_db_attach failed for %s\n", name));
1283 TALLOC_FREE(result);
1284 return NULL;
1287 db_path = ctdbd_dbpath(conn, db_ctdb, db_ctdb->db_id);
1289 result->persistent = ((tdb_flags & TDB_CLEAR_IF_FIRST) == 0);
1291 /* only pass through specific flags */
1292 tdb_flags &= TDB_SEQNUM;
1294 /* honor permissions if user has specified O_CREAT */
1295 if (open_flags & O_CREAT) {
1296 chmod(db_path, mode);
1299 db_ctdb->wtdb = tdb_wrap_open(db_ctdb, db_path, hash_size, tdb_flags, O_RDWR, 0);
1300 if (db_ctdb->wtdb == NULL) {
1301 DEBUG(0, ("Could not open tdb %s: %s\n", db_path, strerror(errno)));
1302 TALLOC_FREE(result);
1303 return NULL;
1305 talloc_free(db_path);
1307 if (result->persistent) {
1308 db_ctdb->lock_ctx = g_lock_ctx_init(db_ctdb,
1309 ctdb_conn_msg_ctx(conn));
1310 if (db_ctdb->lock_ctx == NULL) {
1311 DEBUG(0, ("g_lock_ctx_init failed\n"));
1312 TALLOC_FREE(result);
1313 return NULL;
1317 result->private_data = (void *)db_ctdb;
1318 result->fetch_locked = db_ctdb_fetch_locked;
1319 result->fetch = db_ctdb_fetch;
1320 result->traverse = db_ctdb_traverse;
1321 result->traverse_read = db_ctdb_traverse_read;
1322 result->get_seqnum = db_ctdb_get_seqnum;
1323 result->get_flags = db_ctdb_get_flags;
1324 result->transaction_start = db_ctdb_transaction_start;
1325 result->transaction_commit = db_ctdb_transaction_commit;
1326 result->transaction_cancel = db_ctdb_transaction_cancel;
1328 DEBUG(3,("db_open_ctdb: opened database '%s' with dbid 0x%x\n",
1329 name, db_ctdb->db_id));
1331 return result;
1333 #endif