s3:dbwrap_ctdb: in ctdb_delete, send a SCHEDULE_FOR_DELETION control to local ctdbd
[Samba/gbeck.git] / source3 / lib / dbwrap_ctdb.c
blob90c477185b16cbbd8e078a8985df338c6a2ead2f
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;
53 struct timeval lock_time;
56 static NTSTATUS tdb_error_to_ntstatus(struct tdb_context *tdb)
58 NTSTATUS status;
59 enum TDB_ERROR tret = tdb_error(tdb);
61 switch (tret) {
62 case TDB_ERR_EXISTS:
63 status = NT_STATUS_OBJECT_NAME_COLLISION;
64 break;
65 case TDB_ERR_NOEXIST:
66 status = NT_STATUS_OBJECT_NAME_NOT_FOUND;
67 break;
68 default:
69 status = NT_STATUS_INTERNAL_DB_CORRUPTION;
70 break;
73 return status;
77 /**
78 * fetch a record from the tdb, separating out the header
79 * information and returning the body of the record.
81 static NTSTATUS db_ctdb_ltdb_fetch(struct db_ctdb_ctx *db,
82 TDB_DATA key,
83 struct ctdb_ltdb_header *header,
84 TALLOC_CTX *mem_ctx,
85 TDB_DATA *data)
87 TDB_DATA rec;
88 NTSTATUS status;
90 rec = tdb_fetch(db->wtdb->tdb, key);
91 if (rec.dsize < sizeof(struct ctdb_ltdb_header)) {
92 status = NT_STATUS_NOT_FOUND;
93 if (data) {
94 ZERO_STRUCTP(data);
96 if (header) {
97 header->dmaster = (uint32_t)-1;
98 header->rsn = 0;
100 goto done;
103 if (header) {
104 *header = *(struct ctdb_ltdb_header *)rec.dptr;
107 if (data) {
108 data->dsize = rec.dsize - sizeof(struct ctdb_ltdb_header);
109 if (data->dsize == 0) {
110 data->dptr = NULL;
111 } else {
112 data->dptr = (unsigned char *)talloc_memdup(mem_ctx,
113 rec.dptr
114 + sizeof(struct ctdb_ltdb_header),
115 data->dsize);
116 if (data->dptr == NULL) {
117 status = NT_STATUS_NO_MEMORY;
118 goto done;
123 status = NT_STATUS_OK;
125 done:
126 SAFE_FREE(rec.dptr);
127 return status;
131 * Store a record together with the ctdb record header
132 * in the local copy of the database.
134 static NTSTATUS db_ctdb_ltdb_store(struct db_ctdb_ctx *db,
135 TDB_DATA key,
136 struct ctdb_ltdb_header *header,
137 TDB_DATA data)
139 TALLOC_CTX *tmp_ctx = talloc_stackframe();
140 TDB_DATA rec;
141 int ret;
143 rec.dsize = data.dsize + sizeof(struct ctdb_ltdb_header);
144 rec.dptr = (uint8_t *)talloc_size(tmp_ctx, rec.dsize);
146 if (rec.dptr == NULL) {
147 talloc_free(tmp_ctx);
148 return NT_STATUS_NO_MEMORY;
151 memcpy(rec.dptr, header, sizeof(struct ctdb_ltdb_header));
152 memcpy(sizeof(struct ctdb_ltdb_header) + (uint8_t *)rec.dptr, data.dptr, data.dsize);
154 ret = tdb_store(db->wtdb->tdb, key, rec, TDB_REPLACE);
156 talloc_free(tmp_ctx);
158 return (ret == 0) ? NT_STATUS_OK
159 : tdb_error_to_ntstatus(db->wtdb->tdb);
164 form a ctdb_rec_data record from a key/data pair
166 note that header may be NULL. If not NULL then it is included in the data portion
167 of the record
169 static struct ctdb_rec_data *db_ctdb_marshall_record(TALLOC_CTX *mem_ctx, uint32_t reqid,
170 TDB_DATA key,
171 struct ctdb_ltdb_header *header,
172 TDB_DATA data)
174 size_t length;
175 struct ctdb_rec_data *d;
177 length = offsetof(struct ctdb_rec_data, data) + key.dsize +
178 data.dsize + (header?sizeof(*header):0);
179 d = (struct ctdb_rec_data *)talloc_size(mem_ctx, length);
180 if (d == NULL) {
181 return NULL;
183 d->length = length;
184 d->reqid = reqid;
185 d->keylen = key.dsize;
186 memcpy(&d->data[0], key.dptr, key.dsize);
187 if (header) {
188 d->datalen = data.dsize + sizeof(*header);
189 memcpy(&d->data[key.dsize], header, sizeof(*header));
190 memcpy(&d->data[key.dsize+sizeof(*header)], data.dptr, data.dsize);
191 } else {
192 d->datalen = data.dsize;
193 memcpy(&d->data[key.dsize], data.dptr, data.dsize);
195 return d;
199 /* helper function for marshalling multiple records */
200 static struct ctdb_marshall_buffer *db_ctdb_marshall_add(TALLOC_CTX *mem_ctx,
201 struct ctdb_marshall_buffer *m,
202 uint64_t db_id,
203 uint32_t reqid,
204 TDB_DATA key,
205 struct ctdb_ltdb_header *header,
206 TDB_DATA data)
208 struct ctdb_rec_data *r;
209 size_t m_size, r_size;
210 struct ctdb_marshall_buffer *m2 = NULL;
212 r = db_ctdb_marshall_record(talloc_tos(), reqid, key, header, data);
213 if (r == NULL) {
214 talloc_free(m);
215 return NULL;
218 if (m == NULL) {
219 m = (struct ctdb_marshall_buffer *)talloc_zero_size(
220 mem_ctx, offsetof(struct ctdb_marshall_buffer, data));
221 if (m == NULL) {
222 goto done;
224 m->db_id = db_id;
227 m_size = talloc_get_size(m);
228 r_size = talloc_get_size(r);
230 m2 = (struct ctdb_marshall_buffer *)talloc_realloc_size(
231 mem_ctx, m, m_size + r_size);
232 if (m2 == NULL) {
233 talloc_free(m);
234 goto done;
237 memcpy(m_size + (uint8_t *)m2, r, r_size);
239 m2->count++;
241 done:
242 talloc_free(r);
243 return m2;
246 /* we've finished marshalling, return a data blob with the marshalled records */
247 static TDB_DATA db_ctdb_marshall_finish(struct ctdb_marshall_buffer *m)
249 TDB_DATA data;
250 data.dptr = (uint8_t *)m;
251 data.dsize = talloc_get_size(m);
252 return data;
256 loop over a marshalling buffer
258 - pass r==NULL to start
259 - loop the number of times indicated by m->count
261 static struct ctdb_rec_data *db_ctdb_marshall_loop_next(struct ctdb_marshall_buffer *m, struct ctdb_rec_data *r,
262 uint32_t *reqid,
263 struct ctdb_ltdb_header *header,
264 TDB_DATA *key, TDB_DATA *data)
266 if (r == NULL) {
267 r = (struct ctdb_rec_data *)&m->data[0];
268 } else {
269 r = (struct ctdb_rec_data *)(r->length + (uint8_t *)r);
272 if (reqid != NULL) {
273 *reqid = r->reqid;
276 if (key != NULL) {
277 key->dptr = &r->data[0];
278 key->dsize = r->keylen;
280 if (data != NULL) {
281 data->dptr = &r->data[r->keylen];
282 data->dsize = r->datalen;
283 if (header != NULL) {
284 data->dptr += sizeof(*header);
285 data->dsize -= sizeof(*header);
289 if (header != NULL) {
290 if (r->datalen < sizeof(*header)) {
291 return NULL;
293 *header = *(struct ctdb_ltdb_header *)&r->data[r->keylen];
296 return r;
300 * CTDB transaction destructor
302 static int db_ctdb_transaction_destructor(struct db_ctdb_transaction_handle *h)
304 NTSTATUS status;
306 status = g_lock_unlock(h->ctx->lock_ctx, h->lock_name);
307 if (!NT_STATUS_IS_OK(status)) {
308 DEBUG(0, ("g_lock_unlock failed: %s\n", nt_errstr(status)));
309 return -1;
311 return 0;
315 * CTDB dbwrap API: transaction_start function
316 * starts a transaction on a persistent database
318 static int db_ctdb_transaction_start(struct db_context *db)
320 struct db_ctdb_transaction_handle *h;
321 NTSTATUS status;
322 struct db_ctdb_ctx *ctx = talloc_get_type_abort(db->private_data,
323 struct db_ctdb_ctx);
325 if (!db->persistent) {
326 DEBUG(0,("transactions not supported on non-persistent database 0x%08x\n",
327 ctx->db_id));
328 return -1;
331 if (ctx->transaction) {
332 ctx->transaction->nesting++;
333 return 0;
336 h = talloc_zero(db, struct db_ctdb_transaction_handle);
337 if (h == NULL) {
338 DEBUG(0,(__location__ " oom for transaction handle\n"));
339 return -1;
342 h->ctx = ctx;
344 h->lock_name = talloc_asprintf(h, "transaction_db_0x%08x",
345 (unsigned int)ctx->db_id);
346 if (h->lock_name == NULL) {
347 DEBUG(0, ("talloc_asprintf failed\n"));
348 TALLOC_FREE(h);
349 return -1;
353 * Wait a day, i.e. forever...
355 status = g_lock_lock(ctx->lock_ctx, h->lock_name, G_LOCK_WRITE,
356 timeval_set(86400, 0));
357 if (!NT_STATUS_IS_OK(status)) {
358 DEBUG(0, ("g_lock_lock failed: %s\n", nt_errstr(status)));
359 TALLOC_FREE(h);
360 return -1;
363 talloc_set_destructor(h, db_ctdb_transaction_destructor);
365 ctx->transaction = h;
367 DEBUG(5,(__location__ " Started transaction on db 0x%08x\n", ctx->db_id));
369 return 0;
372 static bool pull_newest_from_marshall_buffer(struct ctdb_marshall_buffer *buf,
373 TDB_DATA key,
374 struct ctdb_ltdb_header *pheader,
375 TALLOC_CTX *mem_ctx,
376 TDB_DATA *pdata)
378 struct ctdb_rec_data *rec = NULL;
379 struct ctdb_ltdb_header h;
380 bool found = false;
381 TDB_DATA data;
382 int i;
384 if (buf == NULL) {
385 return false;
388 ZERO_STRUCT(h);
389 ZERO_STRUCT(data);
392 * Walk the list of records written during this
393 * transaction. If we want to read one we have already
394 * written, return the last written sample. Thus we do not do
395 * a "break;" for the first hit, this record might have been
396 * overwritten later.
399 for (i=0; i<buf->count; i++) {
400 TDB_DATA tkey, tdata;
401 uint32_t reqid;
402 struct ctdb_ltdb_header hdr;
404 ZERO_STRUCT(hdr);
406 rec = db_ctdb_marshall_loop_next(buf, rec, &reqid, &hdr, &tkey,
407 &tdata);
408 if (rec == NULL) {
409 return false;
412 if (tdb_data_equal(key, tkey)) {
413 found = true;
414 data = tdata;
415 h = hdr;
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 * Fetch a record from a persistent database
479 * without record locking and without an active transaction.
481 * This just fetches from the local database copy.
482 * Since the databases are kept in syc cluster-wide,
483 * there is no point in doing a ctdb call to fetch the
484 * record from the lmaster. It does even harm since migration
485 * of records bump their RSN and hence render the persistent
486 * database inconsistent.
488 static int db_ctdb_fetch_persistent(struct db_ctdb_ctx *db,
489 TALLOC_CTX *mem_ctx,
490 TDB_DATA key, TDB_DATA *data)
492 NTSTATUS status;
493 bool found;
495 status = db_ctdb_ltdb_fetch(db, key, NULL, mem_ctx, data);
497 if (NT_STATUS_EQUAL(status, NT_STATUS_NOT_FOUND)) {
498 *data = tdb_null;
499 } else if (!NT_STATUS_IS_OK(status)) {
500 return -1;
503 return 0;
506 static NTSTATUS db_ctdb_store_transaction(struct db_record *rec, TDB_DATA data, int flag);
507 static NTSTATUS db_ctdb_delete_transaction(struct db_record *rec);
509 static struct db_record *db_ctdb_fetch_locked_transaction(struct db_ctdb_ctx *ctx,
510 TALLOC_CTX *mem_ctx,
511 TDB_DATA key)
513 struct db_record *result;
514 TDB_DATA ctdb_data;
516 if (!(result = talloc(mem_ctx, struct db_record))) {
517 DEBUG(0, ("talloc failed\n"));
518 return NULL;
521 result->private_data = ctx->transaction;
523 result->key.dsize = key.dsize;
524 result->key.dptr = (uint8 *)talloc_memdup(result, key.dptr, key.dsize);
525 if (result->key.dptr == NULL) {
526 DEBUG(0, ("talloc failed\n"));
527 TALLOC_FREE(result);
528 return NULL;
531 result->store = db_ctdb_store_transaction;
532 result->delete_rec = db_ctdb_delete_transaction;
534 if (pull_newest_from_marshall_buffer(ctx->transaction->m_write, key,
535 NULL, result, &result->value)) {
536 return result;
539 ctdb_data = tdb_fetch(ctx->wtdb->tdb, key);
540 if (ctdb_data.dptr == NULL) {
541 /* create the record */
542 result->value = tdb_null;
543 return result;
546 result->value.dsize = ctdb_data.dsize - sizeof(struct ctdb_ltdb_header);
547 result->value.dptr = NULL;
549 if ((result->value.dsize != 0)
550 && !(result->value.dptr = (uint8 *)talloc_memdup(
551 result, ctdb_data.dptr + sizeof(struct ctdb_ltdb_header),
552 result->value.dsize))) {
553 DEBUG(0, ("talloc failed\n"));
554 TALLOC_FREE(result);
557 SAFE_FREE(ctdb_data.dptr);
559 return result;
562 static int db_ctdb_record_destructor(struct db_record **recp)
564 struct db_record *rec = talloc_get_type_abort(*recp, struct db_record);
565 struct db_ctdb_transaction_handle *h = talloc_get_type_abort(
566 rec->private_data, struct db_ctdb_transaction_handle);
567 int ret = h->ctx->db->transaction_commit(h->ctx->db);
568 if (ret != 0) {
569 DEBUG(0,(__location__ " transaction_commit failed\n"));
571 return 0;
575 auto-create a transaction for persistent databases
577 static struct db_record *db_ctdb_fetch_locked_persistent(struct db_ctdb_ctx *ctx,
578 TALLOC_CTX *mem_ctx,
579 TDB_DATA key)
581 int res;
582 struct db_record *rec, **recp;
584 res = db_ctdb_transaction_start(ctx->db);
585 if (res == -1) {
586 return NULL;
589 rec = db_ctdb_fetch_locked_transaction(ctx, mem_ctx, key);
590 if (rec == NULL) {
591 ctx->db->transaction_cancel(ctx->db);
592 return NULL;
595 /* destroy this transaction when we release the lock */
596 recp = talloc(rec, struct db_record *);
597 if (recp == NULL) {
598 ctx->db->transaction_cancel(ctx->db);
599 talloc_free(rec);
600 return NULL;
602 *recp = rec;
603 talloc_set_destructor(recp, db_ctdb_record_destructor);
604 return rec;
609 stores a record inside a transaction
611 static NTSTATUS db_ctdb_transaction_store(struct db_ctdb_transaction_handle *h,
612 TDB_DATA key, TDB_DATA data)
614 TALLOC_CTX *tmp_ctx = talloc_new(h);
615 TDB_DATA rec;
616 struct ctdb_ltdb_header header;
618 ZERO_STRUCT(header);
620 /* we need the header so we can update the RSN */
622 if (!pull_newest_from_marshall_buffer(h->m_write, key, &header,
623 NULL, NULL)) {
625 rec = tdb_fetch(h->ctx->wtdb->tdb, key);
627 if (rec.dptr != NULL) {
628 memcpy(&header, rec.dptr,
629 sizeof(struct ctdb_ltdb_header));
630 rec.dsize -= sizeof(struct ctdb_ltdb_header);
633 * a special case, we are writing the same
634 * data that is there now
636 if (data.dsize == rec.dsize &&
637 memcmp(data.dptr,
638 rec.dptr + sizeof(struct ctdb_ltdb_header),
639 data.dsize) == 0) {
640 SAFE_FREE(rec.dptr);
641 talloc_free(tmp_ctx);
642 return NT_STATUS_OK;
645 SAFE_FREE(rec.dptr);
648 header.dmaster = get_my_vnn();
649 header.rsn++;
651 h->m_all = db_ctdb_marshall_add(h, h->m_all, h->ctx->db_id, 0, key,
652 NULL, data);
653 if (h->m_all == NULL) {
654 DEBUG(0,(__location__ " Failed to add to marshalling "
655 "record\n"));
656 talloc_free(tmp_ctx);
657 return NT_STATUS_NO_MEMORY;
660 h->m_write = db_ctdb_marshall_add(h, h->m_write, h->ctx->db_id, 0, key, &header, data);
661 if (h->m_write == NULL) {
662 DEBUG(0,(__location__ " Failed to add to marshalling record\n"));
663 talloc_free(tmp_ctx);
664 return NT_STATUS_NO_MEMORY;
667 talloc_free(tmp_ctx);
668 return NT_STATUS_OK;
673 a record store inside a transaction
675 static NTSTATUS db_ctdb_store_transaction(struct db_record *rec, TDB_DATA data, int flag)
677 struct db_ctdb_transaction_handle *h = talloc_get_type_abort(
678 rec->private_data, struct db_ctdb_transaction_handle);
679 NTSTATUS status;
681 status = db_ctdb_transaction_store(h, rec->key, data);
682 return status;
686 a record delete inside a transaction
688 static NTSTATUS db_ctdb_delete_transaction(struct db_record *rec)
690 struct db_ctdb_transaction_handle *h = talloc_get_type_abort(
691 rec->private_data, struct db_ctdb_transaction_handle);
692 NTSTATUS status;
694 status = db_ctdb_transaction_store(h, rec->key, tdb_null);
695 return status;
699 * Fetch the db sequence number of a persistent db directly from the db.
701 static NTSTATUS db_ctdb_fetch_db_seqnum_from_db(struct db_ctdb_ctx *db,
702 uint64_t *seqnum)
704 NTSTATUS status;
705 const char *keyname = CTDB_DB_SEQNUM_KEY;
706 TDB_DATA key;
707 TDB_DATA data;
708 struct ctdb_ltdb_header header;
709 TALLOC_CTX *mem_ctx = talloc_stackframe();
711 if (seqnum == NULL) {
712 return NT_STATUS_INVALID_PARAMETER;
715 key = string_term_tdb_data(keyname);
717 status = db_ctdb_ltdb_fetch(db, key, &header, mem_ctx, &data);
718 if (!NT_STATUS_IS_OK(status) &&
719 !NT_STATUS_EQUAL(status, NT_STATUS_NOT_FOUND))
721 goto done;
724 status = NT_STATUS_OK;
726 if (data.dsize != sizeof(uint64_t)) {
727 *seqnum = 0;
728 goto done;
731 *seqnum = *(uint64_t *)data.dptr;
733 done:
734 TALLOC_FREE(mem_ctx);
735 return status;
739 * Store the database sequence number inside a transaction.
741 static NTSTATUS db_ctdb_store_db_seqnum(struct db_ctdb_transaction_handle *h,
742 uint64_t seqnum)
744 NTSTATUS status;
745 const char *keyname = CTDB_DB_SEQNUM_KEY;
746 TDB_DATA key;
747 TDB_DATA data;
749 key = string_term_tdb_data(keyname);
751 data.dptr = (uint8_t *)&seqnum;
752 data.dsize = sizeof(uint64_t);
754 status = db_ctdb_transaction_store(h, key, data);
756 return status;
760 commit a transaction
762 static int db_ctdb_transaction_commit(struct db_context *db)
764 struct db_ctdb_ctx *ctx = talloc_get_type_abort(db->private_data,
765 struct db_ctdb_ctx);
766 NTSTATUS rets;
767 int status;
768 struct db_ctdb_transaction_handle *h = ctx->transaction;
769 uint64_t old_seqnum, new_seqnum;
770 int ret;
772 if (h == NULL) {
773 DEBUG(0,(__location__ " transaction commit with no open transaction on db 0x%08x\n", ctx->db_id));
774 return -1;
777 if (h->nested_cancel) {
778 db->transaction_cancel(db);
779 DEBUG(5,(__location__ " Failed transaction commit after nested cancel\n"));
780 return -1;
783 if (h->nesting != 0) {
784 h->nesting--;
785 return 0;
788 if (h->m_write == NULL) {
790 * No changes were made, so don't change the seqnum,
791 * don't push to other node, just exit with success.
793 ret = 0;
794 goto done;
797 DEBUG(5,(__location__ " Commit transaction on db 0x%08x\n", ctx->db_id));
800 * As the last db action before committing, bump the database sequence
801 * number. Note that this undoes all changes to the seqnum records
802 * performed under the transaction. This record is not meant to be
803 * modified by user interaction. It is for internal use only...
805 rets = db_ctdb_fetch_db_seqnum_from_db(ctx, &old_seqnum);
806 if (!NT_STATUS_IS_OK(rets)) {
807 DEBUG(1, (__location__ " failed to fetch the db sequence number "
808 "in transaction commit on db 0x%08x\n", ctx->db_id));
809 ret = -1;
810 goto done;
813 new_seqnum = old_seqnum + 1;
815 rets = db_ctdb_store_db_seqnum(h, new_seqnum);
816 if (!NT_STATUS_IS_OK(rets)) {
817 DEBUG(1, (__location__ "failed to store the db sequence number "
818 " in transaction commit on db 0x%08x\n", ctx->db_id));
819 ret = -1;
820 goto done;
823 again:
824 /* tell ctdbd to commit to the other nodes */
825 rets = ctdbd_control_local(messaging_ctdbd_connection(),
826 CTDB_CONTROL_TRANS3_COMMIT,
827 h->ctx->db_id, 0,
828 db_ctdb_marshall_finish(h->m_write),
829 NULL, NULL, &status);
830 if (!NT_STATUS_IS_OK(rets) || status != 0) {
832 * The TRANS3_COMMIT control should only possibly fail when a
833 * recovery has been running concurrently. In any case, the db
834 * will be the same on all nodes, either the new copy or the
835 * old copy. This can be detected by comparing the old and new
836 * local sequence numbers.
838 rets = db_ctdb_fetch_db_seqnum_from_db(ctx, &new_seqnum);
839 if (!NT_STATUS_IS_OK(rets)) {
840 DEBUG(1, (__location__ " failed to refetch db sequence "
841 "number after failed TRANS3_COMMIT\n"));
842 ret = -1;
843 goto done;
846 if (new_seqnum == old_seqnum) {
847 /* Recovery prevented all our changes: retry. */
848 goto again;
849 } else if (new_seqnum != (old_seqnum + 1)) {
850 DEBUG(0, (__location__ " ERROR: new_seqnum[%lu] != "
851 "old_seqnum[%lu] + (0 or 1) after failed "
852 "TRANS3_COMMIT - this should not happen!\n",
853 (unsigned long)new_seqnum,
854 (unsigned long)old_seqnum));
855 ret = -1;
856 goto done;
859 * Recovery propagated our changes to all nodes, completing
860 * our commit for us - succeed.
864 ret = 0;
866 done:
867 h->ctx->transaction = NULL;
868 talloc_free(h);
869 return ret;
874 cancel a transaction
876 static int db_ctdb_transaction_cancel(struct db_context *db)
878 struct db_ctdb_ctx *ctx = talloc_get_type_abort(db->private_data,
879 struct db_ctdb_ctx);
880 struct db_ctdb_transaction_handle *h = ctx->transaction;
882 if (h == NULL) {
883 DEBUG(0,(__location__ " transaction cancel with no open transaction on db 0x%08x\n", ctx->db_id));
884 return -1;
887 if (h->nesting != 0) {
888 h->nesting--;
889 h->nested_cancel = true;
890 return 0;
893 DEBUG(5,(__location__ " Cancel transaction on db 0x%08x\n", ctx->db_id));
895 ctx->transaction = NULL;
896 talloc_free(h);
897 return 0;
901 static NTSTATUS db_ctdb_store(struct db_record *rec, TDB_DATA data, int flag)
903 struct db_ctdb_rec *crec = talloc_get_type_abort(
904 rec->private_data, struct db_ctdb_rec);
906 return db_ctdb_ltdb_store(crec->ctdb_ctx, rec->key, &(crec->header), data);
911 #ifdef CTDB_CONTROL_SCHEDULE_FOR_DELETION
912 static NTSTATUS db_ctdb_send_schedule_for_deletion(struct db_record *rec)
914 NTSTATUS status;
915 struct ctdb_control_schedule_for_deletion *dd;
916 TDB_DATA indata;
917 int cstatus;
918 struct db_ctdb_rec *crec = talloc_get_type_abort(
919 rec->private_data, struct db_ctdb_rec);
921 indata.dsize = offsetof(struct ctdb_control_schedule_for_deletion, key) + rec->key.dsize;
922 indata.dptr = talloc_zero_array(crec, uint8_t, indata.dsize);
923 if (indata.dptr == NULL) {
924 DEBUG(0, (__location__ " talloc failed!\n"));
925 return NT_STATUS_NO_MEMORY;
928 dd = (struct ctdb_control_schedule_for_deletion *)(void *)indata.dptr;
929 dd->db_id = crec->ctdb_ctx->db_id;
930 dd->hdr = crec->header;
931 dd->keylen = rec->key.dsize;
932 memcpy(dd->key, rec->key.dptr, rec->key.dsize);
934 status = ctdbd_control_local(messaging_ctdbd_connection(),
935 CTDB_CONTROL_SCHEDULE_FOR_DELETION,
936 crec->ctdb_ctx->db_id,
937 CTDB_CTRL_FLAG_NOREPLY, /* flags */
938 indata,
939 NULL, /* outdata */
940 NULL, /* errmsg */
941 &cstatus);
942 talloc_free(indata.dptr);
944 if (!NT_STATUS_IS_OK(status) || cstatus != 0) {
945 DEBUG(1, (__location__ " Error sending local control "
946 "SCHEDULE_FOR_DELETION: %s, cstatus = %d\n",
947 nt_errstr(status), cstatus));
948 if (NT_STATUS_IS_OK(status)) {
949 status = NT_STATUS_UNSUCCESSFUL;
953 return status;
955 #endif
957 static NTSTATUS db_ctdb_delete(struct db_record *rec)
959 TDB_DATA data;
960 NTSTATUS status;
963 * We have to store the header with empty data. TODO: Fix the
964 * tdb-level cleanup
967 ZERO_STRUCT(data);
969 status = db_ctdb_store(rec, data, 0);
970 if (!NT_STATUS_IS_OK(status)) {
971 return status;
974 #ifdef CTDB_CONTROL_SCHEDULE_FOR_DELETION
975 status = db_ctdb_send_schedule_for_deletion(rec);
976 #endif
978 return status;
981 static int db_ctdb_record_destr(struct db_record* data)
983 struct db_ctdb_rec *crec = talloc_get_type_abort(
984 data->private_data, struct db_ctdb_rec);
985 int threshold;
987 DEBUG(10, (DEBUGLEVEL > 10
988 ? "Unlocking db %u key %s\n"
989 : "Unlocking db %u key %.20s\n",
990 (int)crec->ctdb_ctx->db_id,
991 hex_encode_talloc(data, (unsigned char *)data->key.dptr,
992 data->key.dsize)));
994 if (tdb_chainunlock(crec->ctdb_ctx->wtdb->tdb, data->key) != 0) {
995 DEBUG(0, ("tdb_chainunlock failed\n"));
996 return -1;
999 threshold = lp_ctdb_locktime_warn_threshold();
1000 if (threshold != 0) {
1001 double timediff = timeval_elapsed(&crec->lock_time);
1002 if ((timediff * 1000) > threshold) {
1003 DEBUG(0, ("Held tdb lock %f seconds\n", timediff));
1007 return 0;
1010 static struct db_record *fetch_locked_internal(struct db_ctdb_ctx *ctx,
1011 TALLOC_CTX *mem_ctx,
1012 TDB_DATA key)
1014 struct db_record *result;
1015 struct db_ctdb_rec *crec;
1016 NTSTATUS status;
1017 TDB_DATA ctdb_data;
1018 int migrate_attempts = 0;
1020 if (!(result = talloc(mem_ctx, struct db_record))) {
1021 DEBUG(0, ("talloc failed\n"));
1022 return NULL;
1025 if (!(crec = TALLOC_ZERO_P(result, struct db_ctdb_rec))) {
1026 DEBUG(0, ("talloc failed\n"));
1027 TALLOC_FREE(result);
1028 return NULL;
1031 result->private_data = (void *)crec;
1032 crec->ctdb_ctx = ctx;
1034 result->key.dsize = key.dsize;
1035 result->key.dptr = (uint8 *)talloc_memdup(result, key.dptr, key.dsize);
1036 if (result->key.dptr == NULL) {
1037 DEBUG(0, ("talloc failed\n"));
1038 TALLOC_FREE(result);
1039 return NULL;
1043 * Do a blocking lock on the record
1045 again:
1047 if (DEBUGLEVEL >= 10) {
1048 char *keystr = hex_encode_talloc(result, key.dptr, key.dsize);
1049 DEBUG(10, (DEBUGLEVEL > 10
1050 ? "Locking db %u key %s\n"
1051 : "Locking db %u key %.20s\n",
1052 (int)crec->ctdb_ctx->db_id, keystr));
1053 TALLOC_FREE(keystr);
1056 if (tdb_chainlock(ctx->wtdb->tdb, key) != 0) {
1057 DEBUG(3, ("tdb_chainlock failed\n"));
1058 TALLOC_FREE(result);
1059 return NULL;
1062 result->store = db_ctdb_store;
1063 result->delete_rec = db_ctdb_delete;
1064 talloc_set_destructor(result, db_ctdb_record_destr);
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.
1073 if ((ctdb_data.dptr == NULL) ||
1074 (ctdb_data.dsize < sizeof(struct ctdb_ltdb_header)) ||
1075 ((struct ctdb_ltdb_header *)ctdb_data.dptr)->dmaster != get_my_vnn()
1076 #if 0
1077 || (random() % 2 != 0)
1078 #endif
1080 SAFE_FREE(ctdb_data.dptr);
1081 tdb_chainunlock(ctx->wtdb->tdb, key);
1082 talloc_set_destructor(result, NULL);
1084 migrate_attempts += 1;
1086 DEBUG(10, ("ctdb_data.dptr = %p, dmaster = %u (%u)\n",
1087 ctdb_data.dptr, ctdb_data.dptr ?
1088 ((struct ctdb_ltdb_header *)ctdb_data.dptr)->dmaster : -1,
1089 get_my_vnn()));
1091 status = ctdbd_migrate(messaging_ctdbd_connection(), ctx->db_id,
1092 key);
1093 if (!NT_STATUS_IS_OK(status)) {
1094 DEBUG(5, ("ctdb_migrate failed: %s\n",
1095 nt_errstr(status)));
1096 TALLOC_FREE(result);
1097 return NULL;
1099 /* now its migrated, try again */
1100 goto again;
1103 if (migrate_attempts > 10) {
1104 DEBUG(0, ("db_ctdb_fetch_locked needed %d attempts\n",
1105 migrate_attempts));
1108 GetTimeOfDay(&crec->lock_time);
1110 memcpy(&crec->header, ctdb_data.dptr, sizeof(crec->header));
1112 result->value.dsize = ctdb_data.dsize - sizeof(crec->header);
1113 result->value.dptr = NULL;
1115 if ((result->value.dsize != 0)
1116 && !(result->value.dptr = (uint8 *)talloc_memdup(
1117 result, ctdb_data.dptr + sizeof(crec->header),
1118 result->value.dsize))) {
1119 DEBUG(0, ("talloc failed\n"));
1120 TALLOC_FREE(result);
1123 SAFE_FREE(ctdb_data.dptr);
1125 return result;
1128 static struct db_record *db_ctdb_fetch_locked(struct db_context *db,
1129 TALLOC_CTX *mem_ctx,
1130 TDB_DATA key)
1132 struct db_ctdb_ctx *ctx = talloc_get_type_abort(db->private_data,
1133 struct db_ctdb_ctx);
1135 if (ctx->transaction != NULL) {
1136 return db_ctdb_fetch_locked_transaction(ctx, mem_ctx, key);
1139 if (db->persistent) {
1140 return db_ctdb_fetch_locked_persistent(ctx, mem_ctx, key);
1143 return fetch_locked_internal(ctx, mem_ctx, key);
1147 fetch (unlocked, no migration) operation on ctdb
1149 static int db_ctdb_fetch(struct db_context *db, TALLOC_CTX *mem_ctx,
1150 TDB_DATA key, TDB_DATA *data)
1152 struct db_ctdb_ctx *ctx = talloc_get_type_abort(db->private_data,
1153 struct db_ctdb_ctx);
1154 NTSTATUS status;
1155 TDB_DATA ctdb_data;
1157 if (ctx->transaction) {
1158 return db_ctdb_transaction_fetch(ctx, mem_ctx, key, data);
1161 if (db->persistent) {
1162 return db_ctdb_fetch_persistent(ctx, mem_ctx, key, data);
1165 /* try a direct fetch */
1166 ctdb_data = tdb_fetch(ctx->wtdb->tdb, key);
1169 * See if we have a valid record and we are the dmaster. If so, we can
1170 * take the shortcut and just return it.
1171 * we bypass the dmaster check for persistent databases
1173 if ((ctdb_data.dptr != NULL) &&
1174 (ctdb_data.dsize >= sizeof(struct ctdb_ltdb_header)) &&
1175 ((struct ctdb_ltdb_header *)ctdb_data.dptr)->dmaster == get_my_vnn())
1177 /* we are the dmaster - avoid the ctdb protocol op */
1179 data->dsize = ctdb_data.dsize - sizeof(struct ctdb_ltdb_header);
1180 if (data->dsize == 0) {
1181 SAFE_FREE(ctdb_data.dptr);
1182 data->dptr = NULL;
1183 return 0;
1186 data->dptr = (uint8 *)talloc_memdup(
1187 mem_ctx, ctdb_data.dptr+sizeof(struct ctdb_ltdb_header),
1188 data->dsize);
1190 SAFE_FREE(ctdb_data.dptr);
1192 if (data->dptr == NULL) {
1193 return -1;
1195 return 0;
1198 SAFE_FREE(ctdb_data.dptr);
1200 /* we weren't able to get it locally - ask ctdb to fetch it for us */
1201 status = ctdbd_fetch(messaging_ctdbd_connection(), ctx->db_id, key,
1202 mem_ctx, data);
1203 if (!NT_STATUS_IS_OK(status)) {
1204 DEBUG(5, ("ctdbd_fetch failed: %s\n", nt_errstr(status)));
1205 return -1;
1208 return 0;
1211 struct traverse_state {
1212 struct db_context *db;
1213 int (*fn)(struct db_record *rec, void *private_data);
1214 void *private_data;
1217 static void traverse_callback(TDB_DATA key, TDB_DATA data, void *private_data)
1219 struct traverse_state *state = (struct traverse_state *)private_data;
1220 struct db_record *rec;
1221 TALLOC_CTX *tmp_ctx = talloc_new(state->db);
1222 /* we have to give them a locked record to prevent races */
1223 rec = db_ctdb_fetch_locked(state->db, tmp_ctx, key);
1224 if (rec && rec->value.dsize > 0) {
1225 state->fn(rec, state->private_data);
1227 talloc_free(tmp_ctx);
1230 static int traverse_persistent_callback(TDB_CONTEXT *tdb, TDB_DATA kbuf, TDB_DATA dbuf,
1231 void *private_data)
1233 struct traverse_state *state = (struct traverse_state *)private_data;
1234 struct db_record *rec;
1235 TALLOC_CTX *tmp_ctx = talloc_new(state->db);
1236 int ret = 0;
1237 /* we have to give them a locked record to prevent races */
1238 rec = db_ctdb_fetch_locked(state->db, tmp_ctx, kbuf);
1239 if (rec && rec->value.dsize > 0) {
1240 ret = state->fn(rec, state->private_data);
1242 talloc_free(tmp_ctx);
1243 return ret;
1246 static int db_ctdb_traverse(struct db_context *db,
1247 int (*fn)(struct db_record *rec,
1248 void *private_data),
1249 void *private_data)
1251 struct db_ctdb_ctx *ctx = talloc_get_type_abort(db->private_data,
1252 struct db_ctdb_ctx);
1253 struct traverse_state state;
1255 state.db = db;
1256 state.fn = fn;
1257 state.private_data = private_data;
1259 if (db->persistent) {
1260 /* for persistent databases we don't need to do a ctdb traverse,
1261 we can do a faster local traverse */
1262 return tdb_traverse(ctx->wtdb->tdb, traverse_persistent_callback, &state);
1266 ctdbd_traverse(ctx->db_id, traverse_callback, &state);
1267 return 0;
1270 static NTSTATUS db_ctdb_store_deny(struct db_record *rec, TDB_DATA data, int flag)
1272 return NT_STATUS_MEDIA_WRITE_PROTECTED;
1275 static NTSTATUS db_ctdb_delete_deny(struct db_record *rec)
1277 return NT_STATUS_MEDIA_WRITE_PROTECTED;
1280 static void traverse_read_callback(TDB_DATA key, TDB_DATA data, void *private_data)
1282 struct traverse_state *state = (struct traverse_state *)private_data;
1283 struct db_record rec;
1284 rec.key = key;
1285 rec.value = data;
1286 rec.store = db_ctdb_store_deny;
1287 rec.delete_rec = db_ctdb_delete_deny;
1288 rec.private_data = state->db;
1289 state->fn(&rec, state->private_data);
1292 static int traverse_persistent_callback_read(TDB_CONTEXT *tdb, TDB_DATA kbuf, TDB_DATA dbuf,
1293 void *private_data)
1295 struct traverse_state *state = (struct traverse_state *)private_data;
1296 struct db_record rec;
1297 rec.key = kbuf;
1298 rec.value = dbuf;
1299 rec.store = db_ctdb_store_deny;
1300 rec.delete_rec = db_ctdb_delete_deny;
1301 rec.private_data = state->db;
1303 if (rec.value.dsize <= sizeof(struct ctdb_ltdb_header)) {
1304 /* a deleted record */
1305 return 0;
1307 rec.value.dsize -= sizeof(struct ctdb_ltdb_header);
1308 rec.value.dptr += sizeof(struct ctdb_ltdb_header);
1310 return state->fn(&rec, state->private_data);
1313 static int db_ctdb_traverse_read(struct db_context *db,
1314 int (*fn)(struct db_record *rec,
1315 void *private_data),
1316 void *private_data)
1318 struct db_ctdb_ctx *ctx = talloc_get_type_abort(db->private_data,
1319 struct db_ctdb_ctx);
1320 struct traverse_state state;
1322 state.db = db;
1323 state.fn = fn;
1324 state.private_data = private_data;
1326 if (db->persistent) {
1327 /* for persistent databases we don't need to do a ctdb traverse,
1328 we can do a faster local traverse */
1329 return tdb_traverse_read(ctx->wtdb->tdb, traverse_persistent_callback_read, &state);
1332 ctdbd_traverse(ctx->db_id, traverse_read_callback, &state);
1333 return 0;
1336 static int db_ctdb_get_seqnum(struct db_context *db)
1338 struct db_ctdb_ctx *ctx = talloc_get_type_abort(db->private_data,
1339 struct db_ctdb_ctx);
1340 return tdb_get_seqnum(ctx->wtdb->tdb);
1343 static int db_ctdb_get_flags(struct db_context *db)
1345 struct db_ctdb_ctx *ctx = talloc_get_type_abort(db->private_data,
1346 struct db_ctdb_ctx);
1347 return tdb_get_flags(ctx->wtdb->tdb);
1350 struct db_context *db_open_ctdb(TALLOC_CTX *mem_ctx,
1351 const char *name,
1352 int hash_size, int tdb_flags,
1353 int open_flags, mode_t mode)
1355 struct db_context *result;
1356 struct db_ctdb_ctx *db_ctdb;
1357 char *db_path;
1358 struct ctdbd_connection *conn;
1360 if (!lp_clustering()) {
1361 DEBUG(10, ("Clustering disabled -- no ctdb\n"));
1362 return NULL;
1365 if (!(result = TALLOC_ZERO_P(mem_ctx, struct db_context))) {
1366 DEBUG(0, ("talloc failed\n"));
1367 TALLOC_FREE(result);
1368 return NULL;
1371 if (!(db_ctdb = TALLOC_P(result, struct db_ctdb_ctx))) {
1372 DEBUG(0, ("talloc failed\n"));
1373 TALLOC_FREE(result);
1374 return NULL;
1377 db_ctdb->transaction = NULL;
1378 db_ctdb->db = result;
1380 conn = messaging_ctdbd_connection();
1381 if (conn == NULL) {
1382 DEBUG(1, ("Could not connect to ctdb\n"));
1383 TALLOC_FREE(result);
1384 return NULL;
1387 if (!NT_STATUS_IS_OK(ctdbd_db_attach(conn, name, &db_ctdb->db_id, tdb_flags))) {
1388 DEBUG(0, ("ctdbd_db_attach failed for %s\n", name));
1389 TALLOC_FREE(result);
1390 return NULL;
1393 db_path = ctdbd_dbpath(conn, db_ctdb, db_ctdb->db_id);
1395 result->persistent = ((tdb_flags & TDB_CLEAR_IF_FIRST) == 0);
1397 /* only pass through specific flags */
1398 tdb_flags &= TDB_SEQNUM;
1400 /* honor permissions if user has specified O_CREAT */
1401 if (open_flags & O_CREAT) {
1402 chmod(db_path, mode);
1405 db_ctdb->wtdb = tdb_wrap_open(db_ctdb, db_path, hash_size, tdb_flags, O_RDWR, 0);
1406 if (db_ctdb->wtdb == NULL) {
1407 DEBUG(0, ("Could not open tdb %s: %s\n", db_path, strerror(errno)));
1408 TALLOC_FREE(result);
1409 return NULL;
1411 talloc_free(db_path);
1413 if (result->persistent) {
1414 db_ctdb->lock_ctx = g_lock_ctx_init(db_ctdb,
1415 ctdb_conn_msg_ctx(conn));
1416 if (db_ctdb->lock_ctx == NULL) {
1417 DEBUG(0, ("g_lock_ctx_init failed\n"));
1418 TALLOC_FREE(result);
1419 return NULL;
1423 result->private_data = (void *)db_ctdb;
1424 result->fetch_locked = db_ctdb_fetch_locked;
1425 result->fetch = db_ctdb_fetch;
1426 result->traverse = db_ctdb_traverse;
1427 result->traverse_read = db_ctdb_traverse_read;
1428 result->get_seqnum = db_ctdb_get_seqnum;
1429 result->get_flags = db_ctdb_get_flags;
1430 result->transaction_start = db_ctdb_transaction_start;
1431 result->transaction_commit = db_ctdb_transaction_commit;
1432 result->transaction_cancel = db_ctdb_transaction_cancel;
1434 DEBUG(3,("db_open_ctdb: opened database '%s' with dbid 0x%x\n",
1435 name, db_ctdb->db_id));
1437 return result;
1439 #endif