/*
Unix SMB/CIFS implementation.
Database interface wrapper around ctdbd
- Copyright (C) Volker Lendecke 2007
-
+ Copyright (C) Volker Lendecke 2007-2009
+ Copyright (C) Michael Adam 2009
+
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3 of the License, or
(at your option) any later version.
-
+
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
-
+
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "ctdb.h"
#include "ctdb_private.h"
#include "ctdbd_conn.h"
+#include "g_lock.h"
+
+struct db_ctdb_transaction_handle {
+ struct db_ctdb_ctx *ctx;
+ /*
+ * we store the reads and writes done under a transaction:
+ * - one list stores both reads and writes (m_all),
+ * - the other just writes (m_write)
+ */
+ struct ctdb_marshall_buffer *m_all;
+ struct ctdb_marshall_buffer *m_write;
+ uint32_t nesting;
+ bool nested_cancel;
+ char *lock_name;
+};
struct db_ctdb_ctx {
+ struct db_context *db;
struct tdb_wrap *wtdb;
uint32 db_id;
+ struct db_ctdb_transaction_handle *transaction;
+ struct g_lock_ctx *lock_ctx;
};
struct db_ctdb_rec {
return status;
}
-static NTSTATUS db_ctdb_store(struct db_record *rec, TDB_DATA data, int flag)
+
+/**
+ * fetch a record from the tdb, separating out the header
+ * information and returning the body of the record.
+ */
+static NTSTATUS db_ctdb_ltdb_fetch(struct db_ctdb_ctx *db,
+ TDB_DATA key,
+ struct ctdb_ltdb_header *header,
+ TALLOC_CTX *mem_ctx,
+ TDB_DATA *data)
{
- struct db_ctdb_rec *crec = talloc_get_type_abort(
- rec->private_data, struct db_ctdb_rec);
- TDB_DATA cdata;
+ TDB_DATA rec;
+ NTSTATUS status;
+
+ rec = tdb_fetch(db->wtdb->tdb, key);
+ if (rec.dsize < sizeof(struct ctdb_ltdb_header)) {
+ status = NT_STATUS_NOT_FOUND;
+ if (data) {
+ ZERO_STRUCTP(data);
+ }
+ if (header) {
+ header->dmaster = (uint32_t)-1;
+ header->rsn = 0;
+ }
+ goto done;
+ }
+
+ if (header) {
+ *header = *(struct ctdb_ltdb_header *)rec.dptr;
+ }
+
+ if (data) {
+ data->dsize = rec.dsize - sizeof(struct ctdb_ltdb_header);
+ if (data->dsize == 0) {
+ data->dptr = NULL;
+ } else {
+ data->dptr = (unsigned char *)talloc_memdup(mem_ctx,
+ rec.dptr
+ + sizeof(struct ctdb_ltdb_header),
+ data->dsize);
+ if (data->dptr == NULL) {
+ status = NT_STATUS_NO_MEMORY;
+ goto done;
+ }
+ }
+ }
+
+ status = NT_STATUS_OK;
+
+done:
+ SAFE_FREE(rec.dptr);
+ return status;
+}
+
+/*
+ * Store a record together with the ctdb record header
+ * in the local copy of the database.
+ */
+static NTSTATUS db_ctdb_ltdb_store(struct db_ctdb_ctx *db,
+ TDB_DATA key,
+ struct ctdb_ltdb_header *header,
+ TDB_DATA data)
+{
+ TALLOC_CTX *tmp_ctx = talloc_stackframe();
+ TDB_DATA rec;
int ret;
- cdata.dsize = sizeof(crec->header) + data.dsize;
+ rec.dsize = data.dsize + sizeof(struct ctdb_ltdb_header);
+ rec.dptr = (uint8_t *)talloc_size(tmp_ctx, rec.dsize);
- if (!(cdata.dptr = SMB_MALLOC_ARRAY(uint8, cdata.dsize))) {
+ if (rec.dptr == NULL) {
+ talloc_free(tmp_ctx);
return NT_STATUS_NO_MEMORY;
}
- memcpy(cdata.dptr, &crec->header, sizeof(crec->header));
- memcpy(cdata.dptr + sizeof(crec->header), data.dptr, data.dsize);
+ memcpy(rec.dptr, header, sizeof(struct ctdb_ltdb_header));
+ memcpy(sizeof(struct ctdb_ltdb_header) + (uint8_t *)rec.dptr, data.dptr, data.dsize);
- ret = tdb_store(crec->ctdb_ctx->wtdb->tdb, rec->key, cdata, TDB_REPLACE);
+ ret = tdb_store(db->wtdb->tdb, key, rec, TDB_REPLACE);
- SAFE_FREE(cdata.dptr);
+ talloc_free(tmp_ctx);
return (ret == 0) ? NT_STATUS_OK
- : tdb_error_to_ntstatus(crec->ctdb_ctx->wtdb->tdb);
+ : tdb_error_to_ntstatus(db->wtdb->tdb);
+
}
+/*
+ form a ctdb_rec_data record from a key/data pair
-/* for persistent databases the store is a bit different. We have to
- ask the ctdb daemon to push the record to all nodes after the
- store */
-static NTSTATUS db_ctdb_store_persistent(struct db_record *rec, TDB_DATA data, int flag)
+ note that header may be NULL. If not NULL then it is included in the data portion
+ of the record
+ */
+static struct ctdb_rec_data *db_ctdb_marshall_record(TALLOC_CTX *mem_ctx, uint32_t reqid,
+ TDB_DATA key,
+ struct ctdb_ltdb_header *header,
+ TDB_DATA data)
{
- struct db_ctdb_rec *crec;
- struct db_record *record;
- TDB_DATA cdata;
- int ret;
- NTSTATUS status;
- uint32_t count;
- int max_retries = lp_parm_int(-1, "dbwrap ctdb", "max store retries", 5);
-
- for (count = 0, status = NT_STATUS_UNSUCCESSFUL, record = rec;
- (count < max_retries) && !NT_STATUS_IS_OK(status);
- count++)
- {
- if (count > 0) {
- /* retry */
- /*
- * There is a hack here: We use rec as a memory
- * context and re-use it as the record struct ptr.
- * We don't free the record data allocated
- * in each turn. So all gets freed when the caller
- * releases the original record. This is because
- * we don't get the record passed in by reference
- * in the first place and the caller relies on
- * having to free the record himself.
- */
- record = fetch_locked_internal(crec->ctdb_ctx,
- rec,
- rec->key,
- true /* persistent */);
- if (record == NULL) {
- DEBUG(5, ("fetch_locked_internal failed.\n"));
- status = NT_STATUS_NO_MEMORY;
- break;
- }
- }
+ size_t length;
+ struct ctdb_rec_data *d;
- crec = talloc_get_type_abort(record->private_data,
- struct db_ctdb_rec);
+ length = offsetof(struct ctdb_rec_data, data) + key.dsize +
+ data.dsize + (header?sizeof(*header):0);
+ d = (struct ctdb_rec_data *)talloc_size(mem_ctx, length);
+ if (d == NULL) {
+ return NULL;
+ }
+ d->length = length;
+ d->reqid = reqid;
+ d->keylen = key.dsize;
+ memcpy(&d->data[0], key.dptr, key.dsize);
+ if (header) {
+ d->datalen = data.dsize + sizeof(*header);
+ memcpy(&d->data[key.dsize], header, sizeof(*header));
+ memcpy(&d->data[key.dsize+sizeof(*header)], data.dptr, data.dsize);
+ } else {
+ d->datalen = data.dsize;
+ memcpy(&d->data[key.dsize], data.dptr, data.dsize);
+ }
+ return d;
+}
- cdata.dsize = sizeof(crec->header) + data.dsize;
- if (!(cdata.dptr = SMB_MALLOC_ARRAY(uint8, cdata.dsize))) {
- return NT_STATUS_NO_MEMORY;
+/* helper function for marshalling multiple records */
+static struct ctdb_marshall_buffer *db_ctdb_marshall_add(TALLOC_CTX *mem_ctx,
+ struct ctdb_marshall_buffer *m,
+ uint64_t db_id,
+ uint32_t reqid,
+ TDB_DATA key,
+ struct ctdb_ltdb_header *header,
+ TDB_DATA data)
+{
+ struct ctdb_rec_data *r;
+ size_t m_size, r_size;
+ struct ctdb_marshall_buffer *m2 = NULL;
+
+ r = db_ctdb_marshall_record(talloc_tos(), reqid, key, header, data);
+ if (r == NULL) {
+ talloc_free(m);
+ return NULL;
+ }
+
+ if (m == NULL) {
+ m = (struct ctdb_marshall_buffer *)talloc_zero_size(
+ mem_ctx, offsetof(struct ctdb_marshall_buffer, data));
+ if (m == NULL) {
+ goto done;
}
+ m->db_id = db_id;
+ }
+
+ m_size = talloc_get_size(m);
+ r_size = talloc_get_size(r);
+
+ m2 = (struct ctdb_marshall_buffer *)talloc_realloc_size(
+ mem_ctx, m, m_size + r_size);
+ if (m2 == NULL) {
+ talloc_free(m);
+ goto done;
+ }
+
+ memcpy(m_size + (uint8_t *)m2, r, r_size);
- crec->header.rsn++;
+ m2->count++;
- memcpy(cdata.dptr, &crec->header, sizeof(crec->header));
- memcpy(cdata.dptr + sizeof(crec->header), data.dptr, data.dsize);
+done:
+ talloc_free(r);
+ return m2;
+}
+
+/* we've finished marshalling, return a data blob with the marshalled records */
+static TDB_DATA db_ctdb_marshall_finish(struct ctdb_marshall_buffer *m)
+{
+ TDB_DATA data;
+ data.dptr = (uint8_t *)m;
+ data.dsize = talloc_get_size(m);
+ return data;
+}
- status = ctdbd_start_persistent_update(
- messaging_ctdbd_connection(),
- crec->ctdb_ctx->db_id,
- rec->key,
- cdata);
+/*
+ loop over a marshalling buffer
+
+ - pass r==NULL to start
+ - loop the number of times indicated by m->count
+*/
+static struct ctdb_rec_data *db_ctdb_marshall_loop_next(struct ctdb_marshall_buffer *m, struct ctdb_rec_data *r,
+ uint32_t *reqid,
+ struct ctdb_ltdb_header *header,
+ TDB_DATA *key, TDB_DATA *data)
+{
+ if (r == NULL) {
+ r = (struct ctdb_rec_data *)&m->data[0];
+ } else {
+ r = (struct ctdb_rec_data *)(r->length + (uint8_t *)r);
+ }
- if (NT_STATUS_IS_OK(status)) {
- ret = tdb_store(crec->ctdb_ctx->wtdb->tdb, rec->key,
- cdata, TDB_REPLACE);
- status = (ret == 0)
- ? NT_STATUS_OK
- : tdb_error_to_ntstatus(
- crec->ctdb_ctx->wtdb->tdb);
+ if (reqid != NULL) {
+ *reqid = r->reqid;
+ }
+
+ if (key != NULL) {
+ key->dptr = &r->data[0];
+ key->dsize = r->keylen;
+ }
+ if (data != NULL) {
+ data->dptr = &r->data[r->keylen];
+ data->dsize = r->datalen;
+ if (header != NULL) {
+ data->dptr += sizeof(*header);
+ data->dsize -= sizeof(*header);
}
+ }
- /*
- * release the lock *now* in order to prevent deadlocks.
- *
- * There is a tradeoff: Usually, the record is still locked
- * after db->store operation. This lock is usually released
- * via the talloc destructor with the TALLOC_FREE to
- * the record. So we have two choices:
- *
- * - Either re-lock the record after the call to persistent_store
- * or cancel_persistent update and this way not changing any
- * assumptions callers may have about the state, but possibly
- * introducing new race conditions.
- *
- * - Or don't lock the record again but just remove the
- * talloc_destructor. This is less racy but assumes that
- * the lock is always released via TALLOC_FREE of the record.
- *
- * I choose the first variant for now since it seems less racy.
- * We can't guarantee that we succeed in getting the lock
- * anyways. The only real danger here is that a caller
- * performs multiple store operations after a fetch_locked()
- * which is currently not the case.
- */
- tdb_chainunlock(crec->ctdb_ctx->wtdb->tdb, rec->key);
- talloc_set_destructor(record, NULL);
-
- /* now tell ctdbd to update this record on all other nodes */
- if (NT_STATUS_IS_OK(status)) {
- status = ctdbd_persistent_store(
- messaging_ctdbd_connection(),
- crec->ctdb_ctx->db_id,
- rec->key,
- cdata);
- } else {
- ctdbd_cancel_persistent_update(
- messaging_ctdbd_connection(),
- crec->ctdb_ctx->db_id,
- rec->key,
- cdata);
+ if (header != NULL) {
+ if (r->datalen < sizeof(*header)) {
+ return NULL;
}
+ *header = *(struct ctdb_ltdb_header *)&r->data[r->keylen];
+ }
- SAFE_FREE(cdata.dptr);
- } /* retry-loop */
+ return r;
+}
+/**
+ * CTDB transaction destructor
+ */
+static int db_ctdb_transaction_destructor(struct db_ctdb_transaction_handle *h)
+{
+ NTSTATUS status;
+
+ status = g_lock_unlock(h->ctx->lock_ctx, h->lock_name);
if (!NT_STATUS_IS_OK(status)) {
- DEBUG(5, ("ctdbd_persistent_store still failed after "
- "%d retries with error %s - giving up.\n",
- count, nt_errstr(status)));
+ DEBUG(0, ("g_lock_unlock failed: %s\n", nt_errstr(status)));
+ return -1;
}
+ return 0;
+}
- SAFE_FREE(cdata.dptr);
+/**
+ * CTDB dbwrap API: transaction_start function
+ * starts a transaction on a persistent database
+ */
+static int db_ctdb_transaction_start(struct db_context *db)
+{
+ struct db_ctdb_transaction_handle *h;
+ NTSTATUS status;
+ struct db_ctdb_ctx *ctx = talloc_get_type_abort(db->private_data,
+ struct db_ctdb_ctx);
- return status;
+ if (!db->persistent) {
+ DEBUG(0,("transactions not supported on non-persistent database 0x%08x\n",
+ ctx->db_id));
+ return -1;
+ }
+
+ if (ctx->transaction) {
+ ctx->transaction->nesting++;
+ return 0;
+ }
+
+ h = talloc_zero(db, struct db_ctdb_transaction_handle);
+ if (h == NULL) {
+ DEBUG(0,(__location__ " oom for transaction handle\n"));
+ return -1;
+ }
+
+ h->ctx = ctx;
+
+ h->lock_name = talloc_asprintf(h, "transaction_db_0x%08x",
+ (unsigned int)ctx->db_id);
+ if (h->lock_name == NULL) {
+ DEBUG(0, ("talloc_asprintf failed\n"));
+ TALLOC_FREE(h);
+ return -1;
+ }
+
+ /*
+ * Wait a day, i.e. forever...
+ */
+ status = g_lock_lock(ctx->lock_ctx, h->lock_name, G_LOCK_WRITE,
+ timeval_set(86400, 0));
+ if (!NT_STATUS_IS_OK(status)) {
+ DEBUG(0, ("g_lock_lock failed: %s\n", nt_errstr(status)));
+ TALLOC_FREE(h);
+ return -1;
+ }
+
+ talloc_set_destructor(h, db_ctdb_transaction_destructor);
+
+ ctx->transaction = h;
+
+ DEBUG(5,(__location__ " Started transaction on db 0x%08x\n", ctx->db_id));
+
+ return 0;
}
-static NTSTATUS db_ctdb_delete(struct db_record *rec)
+static bool pull_newest_from_marshall_buffer(struct ctdb_marshall_buffer *buf,
+ TDB_DATA key,
+ struct ctdb_ltdb_header *pheader,
+ TALLOC_CTX *mem_ctx,
+ TDB_DATA *pdata)
{
+ struct ctdb_rec_data *rec = NULL;
+ struct ctdb_ltdb_header h;
+ bool found;
TDB_DATA data;
+ int i;
+
+ if (buf == NULL) {
+ return false;
+ }
/*
- * We have to store the header with empty data. TODO: Fix the
- * tdb-level cleanup
+ * Walk the list of records written during this
+ * transaction. If we want to read one we have already
+ * written, return the last written sample. Thus we do not do
+ * a "break;" for the first hit, this record might have been
+ * overwritten later.
*/
- ZERO_STRUCT(data);
+ for (i=0; i<buf->count; i++) {
+ TDB_DATA tkey, tdata;
+ uint32_t reqid;
- return db_ctdb_store(rec, data, 0);
+ rec = db_ctdb_marshall_loop_next(buf, rec, &reqid, &h, &tkey,
+ &tdata);
+ if (rec == NULL) {
+ return false;
+ }
+
+ if (tdb_data_equal(key, tkey)) {
+ found = true;
+ data = tdata;
+ }
+ }
+
+ if (!found) {
+ return false;
+ }
+
+ if (pdata != NULL) {
+ data.dptr = (uint8_t *)talloc_memdup(mem_ctx, data.dptr,
+ data.dsize);
+ if ((data.dsize != 0) && (data.dptr == NULL)) {
+ return false;
+ }
+ *pdata = data;
+ }
+
+ if (pheader != NULL) {
+ *pheader = h;
+ }
+
+ return true;
+}
+
+/*
+ fetch a record inside a transaction
+ */
+static int db_ctdb_transaction_fetch(struct db_ctdb_ctx *db,
+ TALLOC_CTX *mem_ctx,
+ TDB_DATA key, TDB_DATA *data)
+{
+ struct db_ctdb_transaction_handle *h = db->transaction;
+ NTSTATUS status;
+ bool found;
+
+ found = pull_newest_from_marshall_buffer(h->m_write, key, NULL,
+ mem_ctx, data);
+ if (found) {
+ return 0;
+ }
+
+ status = db_ctdb_ltdb_fetch(h->ctx, key, NULL, mem_ctx, data);
+
+ if (NT_STATUS_EQUAL(status, NT_STATUS_NOT_FOUND)) {
+ *data = tdb_null;
+ } else if (!NT_STATUS_IS_OK(status)) {
+ return -1;
+ }
+
+ h->m_all = db_ctdb_marshall_add(h, h->m_all, h->ctx->db_id, 1, key,
+ NULL, *data);
+ if (h->m_all == NULL) {
+ DEBUG(0,(__location__ " Failed to add to marshalling "
+ "record\n"));
+ data->dsize = 0;
+ talloc_free(data->dptr);
+ return -1;
+ }
+
+ return 0;
+}
+
+
+static NTSTATUS db_ctdb_store_transaction(struct db_record *rec, TDB_DATA data, int flag);
+static NTSTATUS db_ctdb_delete_transaction(struct db_record *rec);
+
+static struct db_record *db_ctdb_fetch_locked_transaction(struct db_ctdb_ctx *ctx,
+ TALLOC_CTX *mem_ctx,
+ TDB_DATA key)
+{
+ struct db_record *result;
+ TDB_DATA ctdb_data;
+
+ if (!(result = talloc(mem_ctx, struct db_record))) {
+ DEBUG(0, ("talloc failed\n"));
+ return NULL;
+ }
+
+ result->private_data = ctx->transaction;
+
+ result->key.dsize = key.dsize;
+ result->key.dptr = (uint8 *)talloc_memdup(result, key.dptr, key.dsize);
+ if (result->key.dptr == NULL) {
+ DEBUG(0, ("talloc failed\n"));
+ TALLOC_FREE(result);
+ return NULL;
+ }
+
+ result->store = db_ctdb_store_transaction;
+ result->delete_rec = db_ctdb_delete_transaction;
+ if (pull_newest_from_marshall_buffer(ctx->transaction->m_write, key,
+ NULL, result, &result->value)) {
+ return result;
+ }
+
+ ctdb_data = tdb_fetch(ctx->wtdb->tdb, key);
+ if (ctdb_data.dptr == NULL) {
+ /* create the record */
+ result->value = tdb_null;
+ return result;
+ }
+
+ result->value.dsize = ctdb_data.dsize - sizeof(struct ctdb_ltdb_header);
+ result->value.dptr = NULL;
+
+ if ((result->value.dsize != 0)
+ && !(result->value.dptr = (uint8 *)talloc_memdup(
+ result, ctdb_data.dptr + sizeof(struct ctdb_ltdb_header),
+ result->value.dsize))) {
+ DEBUG(0, ("talloc failed\n"));
+ TALLOC_FREE(result);
+ }
+
+ SAFE_FREE(ctdb_data.dptr);
+
+ return result;
+}
+
+static int db_ctdb_record_destructor(struct db_record **recp)
+{
+ struct db_record *rec = talloc_get_type_abort(*recp, struct db_record);
+ struct db_ctdb_transaction_handle *h = talloc_get_type_abort(
+ rec->private_data, struct db_ctdb_transaction_handle);
+ int ret = h->ctx->db->transaction_commit(h->ctx->db);
+ if (ret != 0) {
+ DEBUG(0,(__location__ " transaction_commit failed\n"));
+ }
+ return 0;
}
-static NTSTATUS db_ctdb_delete_persistent(struct db_record *rec)
+/*
+ auto-create a transaction for persistent databases
+ */
+static struct db_record *db_ctdb_fetch_locked_persistent(struct db_ctdb_ctx *ctx,
+ TALLOC_CTX *mem_ctx,
+ TDB_DATA key)
+{
+ int res;
+ struct db_record *rec, **recp;
+
+ res = db_ctdb_transaction_start(ctx->db);
+ if (res == -1) {
+ return NULL;
+ }
+
+ rec = db_ctdb_fetch_locked_transaction(ctx, mem_ctx, key);
+ if (rec == NULL) {
+ ctx->db->transaction_cancel(ctx->db);
+ return NULL;
+ }
+
+ /* destroy this transaction when we release the lock */
+ recp = talloc(rec, struct db_record *);
+ if (recp == NULL) {
+ ctx->db->transaction_cancel(ctx->db);
+ talloc_free(rec);
+ return NULL;
+ }
+ *recp = rec;
+ talloc_set_destructor(recp, db_ctdb_record_destructor);
+ return rec;
+}
+
+
+/*
+ stores a record inside a transaction
+ */
+static NTSTATUS db_ctdb_transaction_store(struct db_ctdb_transaction_handle *h,
+ TDB_DATA key, TDB_DATA data)
+{
+ TALLOC_CTX *tmp_ctx = talloc_new(h);
+ TDB_DATA rec;
+ struct ctdb_ltdb_header header;
+
+ ZERO_STRUCT(header);
+
+ /* we need the header so we can update the RSN */
+
+ if (!pull_newest_from_marshall_buffer(h->m_write, key, &header,
+ NULL, NULL)) {
+
+ rec = tdb_fetch(h->ctx->wtdb->tdb, key);
+
+ if (rec.dptr != NULL) {
+ memcpy(&header, rec.dptr,
+ sizeof(struct ctdb_ltdb_header));
+ rec.dsize -= sizeof(struct ctdb_ltdb_header);
+
+ /*
+ * a special case, we are writing the same
+ * data that is there now
+ */
+ if (data.dsize == rec.dsize &&
+ memcmp(data.dptr,
+ rec.dptr + sizeof(struct ctdb_ltdb_header),
+ data.dsize) == 0) {
+ SAFE_FREE(rec.dptr);
+ talloc_free(tmp_ctx);
+ return NT_STATUS_OK;
+ }
+ }
+ SAFE_FREE(rec.dptr);
+ }
+
+ header.dmaster = get_my_vnn();
+ header.rsn++;
+
+ h->m_all = db_ctdb_marshall_add(h, h->m_all, h->ctx->db_id, 0, key,
+ NULL, data);
+ if (h->m_all == NULL) {
+ DEBUG(0,(__location__ " Failed to add to marshalling "
+ "record\n"));
+ talloc_free(tmp_ctx);
+ return NT_STATUS_NO_MEMORY;
+ }
+
+ h->m_write = db_ctdb_marshall_add(h, h->m_write, h->ctx->db_id, 0, key, &header, data);
+ if (h->m_write == NULL) {
+ DEBUG(0,(__location__ " Failed to add to marshalling record\n"));
+ talloc_free(tmp_ctx);
+ return NT_STATUS_NO_MEMORY;
+ }
+
+ talloc_free(tmp_ctx);
+ return NT_STATUS_OK;
+}
+
+
+/*
+ a record store inside a transaction
+ */
+static NTSTATUS db_ctdb_store_transaction(struct db_record *rec, TDB_DATA data, int flag)
+{
+ struct db_ctdb_transaction_handle *h = talloc_get_type_abort(
+ rec->private_data, struct db_ctdb_transaction_handle);
+ NTSTATUS status;
+
+ status = db_ctdb_transaction_store(h, rec->key, data);
+ return status;
+}
+
+/*
+ a record delete inside a transaction
+ */
+static NTSTATUS db_ctdb_delete_transaction(struct db_record *rec)
+{
+ struct db_ctdb_transaction_handle *h = talloc_get_type_abort(
+ rec->private_data, struct db_ctdb_transaction_handle);
+ NTSTATUS status;
+
+ status = db_ctdb_transaction_store(h, rec->key, tdb_null);
+ return status;
+}
+
+/*
+ commit a transaction
+ */
+static int db_ctdb_transaction_commit(struct db_context *db)
+{
+ struct db_ctdb_ctx *ctx = talloc_get_type_abort(db->private_data,
+ struct db_ctdb_ctx);
+ NTSTATUS rets;
+ int status;
+ struct db_ctdb_transaction_handle *h = ctx->transaction;
+
+ if (h == NULL) {
+ DEBUG(0,(__location__ " transaction commit with no open transaction on db 0x%08x\n", ctx->db_id));
+ return -1;
+ }
+
+ if (h->nested_cancel) {
+ db->transaction_cancel(db);
+ DEBUG(5,(__location__ " Failed transaction commit after nested cancel\n"));
+ return -1;
+ }
+
+ if (h->nesting != 0) {
+ h->nesting--;
+ return 0;
+ }
+
+ DEBUG(5,(__location__ " Commit transaction on db 0x%08x\n", ctx->db_id));
+
+again:
+ if (h->m_write == NULL) {
+ /* no changes were made, potentially after a retry */
+ goto done;
+ }
+
+ /* tell ctdbd to commit to the other nodes */
+ rets = ctdbd_control_local(messaging_ctdbd_connection(),
+ CTDB_CONTROL_TRANS3_COMMIT,
+ h->ctx->db_id, 0,
+ db_ctdb_marshall_finish(h->m_write),
+ NULL, NULL, &status);
+ if (!NT_STATUS_IS_OK(rets) || status != 0) {
+ /*
+ * TODO:
+ * check the database sequence number and
+ * compare it to the seqnum after applying the
+ * marshall buffer. If it is the same: return success.
+ */
+ goto again;
+ }
+
+done:
+ h->ctx->transaction = NULL;
+ talloc_free(h);
+ return 0;
+}
+
+
+/*
+ cancel a transaction
+ */
+static int db_ctdb_transaction_cancel(struct db_context *db)
+{
+ struct db_ctdb_ctx *ctx = talloc_get_type_abort(db->private_data,
+ struct db_ctdb_ctx);
+ struct db_ctdb_transaction_handle *h = ctx->transaction;
+
+ if (h == NULL) {
+ DEBUG(0,(__location__ " transaction cancel with no open transaction on db 0x%08x\n", ctx->db_id));
+ return -1;
+ }
+
+ if (h->nesting != 0) {
+ h->nesting--;
+ h->nested_cancel = true;
+ return 0;
+ }
+
+ DEBUG(5,(__location__ " Cancel transaction on db 0x%08x\n", ctx->db_id));
+
+ ctx->transaction = NULL;
+ talloc_free(h);
+ return 0;
+}
+
+
+static NTSTATUS db_ctdb_store(struct db_record *rec, TDB_DATA data, int flag)
+{
+ struct db_ctdb_rec *crec = talloc_get_type_abort(
+ rec->private_data, struct db_ctdb_rec);
+
+ return db_ctdb_ltdb_store(crec->ctdb_ctx, rec->key, &(crec->header), data);
+}
+
+
+
+static NTSTATUS db_ctdb_delete(struct db_record *rec)
{
TDB_DATA data;
ZERO_STRUCT(data);
- return db_ctdb_store_persistent(rec, data, 0);
+ return db_ctdb_store(rec, data, 0);
}
? "Unlocking db %u key %s\n"
: "Unlocking db %u key %.20s\n",
(int)crec->ctdb_ctx->db_id,
- hex_encode(data, (unsigned char *)data->key.dptr,
+ hex_encode_talloc(data, (unsigned char *)data->key.dptr,
data->key.dsize)));
if (tdb_chainunlock(crec->ctdb_ctx->wtdb->tdb, data->key) != 0) {
again:
if (DEBUGLEVEL >= 10) {
- char *keystr = hex_encode(result, key.dptr, key.dsize);
+ char *keystr = hex_encode_talloc(result, key.dptr, key.dsize);
DEBUG(10, (DEBUGLEVEL > 10
? "Locking db %u key %s\n"
: "Locking db %u key %.20s\n",
(int)crec->ctdb_ctx->db_id, keystr));
TALLOC_FREE(keystr);
}
-
+
if (tdb_chainlock(ctx->wtdb->tdb, key) != 0) {
DEBUG(3, ("tdb_chainlock failed\n"));
TALLOC_FREE(result);
return NULL;
}
- if (persistent) {
- result->store = db_ctdb_store_persistent;
- result->delete_rec = db_ctdb_delete_persistent;
- } else {
- result->store = db_ctdb_store;
- result->delete_rec = db_ctdb_delete;
- }
+ result->store = db_ctdb_store;
+ result->delete_rec = db_ctdb_delete;
talloc_set_destructor(result, db_ctdb_record_destr);
ctdb_data = tdb_fetch(ctx->wtdb->tdb, key);
struct db_ctdb_ctx *ctx = talloc_get_type_abort(db->private_data,
struct db_ctdb_ctx);
+ if (ctx->transaction != NULL) {
+ return db_ctdb_fetch_locked_transaction(ctx, mem_ctx, key);
+ }
+
+ if (db->persistent) {
+ return db_ctdb_fetch_locked_persistent(ctx, mem_ctx, key);
+ }
+
return fetch_locked_internal(ctx, mem_ctx, key, db->persistent);
}
NTSTATUS status;
TDB_DATA ctdb_data;
+ if (ctx->transaction) {
+ return db_ctdb_transaction_fetch(ctx, mem_ctx, key, data);
+ }
+
/* try a direct fetch */
ctdb_data = tdb_fetch(ctx->wtdb->tdb, key);
return tdb_get_seqnum(ctx->wtdb->tdb);
}
-static int db_ctdb_trans_dummy(struct db_context *db)
+static int db_ctdb_get_flags(struct db_context *db)
{
- /*
- * Not implemented yet, just return ok
- */
- return 0;
+ struct db_ctdb_ctx *ctx = talloc_get_type_abort(db->private_data,
+ struct db_ctdb_ctx);
+ return tdb_get_flags(ctx->wtdb->tdb);
}
struct db_context *db_open_ctdb(TALLOC_CTX *mem_ctx,
struct db_context *result;
struct db_ctdb_ctx *db_ctdb;
char *db_path;
+ struct ctdbd_connection *conn;
if (!lp_clustering()) {
DEBUG(10, ("Clustering disabled -- no ctdb\n"));
return NULL;
}
- if (!NT_STATUS_IS_OK(ctdbd_db_attach(messaging_ctdbd_connection(),name, &db_ctdb->db_id, tdb_flags))) {
+ db_ctdb->transaction = NULL;
+ db_ctdb->db = result;
+
+ conn = messaging_ctdbd_connection();
+
+ if (!NT_STATUS_IS_OK(ctdbd_db_attach(conn, name, &db_ctdb->db_id, tdb_flags))) {
DEBUG(0, ("ctdbd_db_attach failed for %s\n", name));
TALLOC_FREE(result);
return NULL;
}
- db_path = ctdbd_dbpath(messaging_ctdbd_connection(), db_ctdb, db_ctdb->db_id);
+ db_path = ctdbd_dbpath(conn, db_ctdb, db_ctdb->db_id);
result->persistent = ((tdb_flags & TDB_CLEAR_IF_FIRST) == 0);
}
talloc_free(db_path);
+ if (result->persistent) {
+ db_ctdb->lock_ctx = g_lock_ctx_init(db_ctdb,
+ ctdb_conn_msg_ctx(conn));
+ if (db_ctdb->lock_ctx == NULL) {
+ DEBUG(0, ("g_lock_ctx_init failed\n"));
+ TALLOC_FREE(result);
+ return NULL;
+ }
+ }
+
result->private_data = (void *)db_ctdb;
result->fetch_locked = db_ctdb_fetch_locked;
result->fetch = db_ctdb_fetch;
result->traverse = db_ctdb_traverse;
result->traverse_read = db_ctdb_traverse_read;
result->get_seqnum = db_ctdb_get_seqnum;
- result->transaction_start = db_ctdb_trans_dummy;
- result->transaction_commit = db_ctdb_trans_dummy;
- result->transaction_cancel = db_ctdb_trans_dummy;
+ result->get_flags = db_ctdb_get_flags;
+ result->transaction_start = db_ctdb_transaction_start;
+ result->transaction_commit = db_ctdb_transaction_commit;
+ result->transaction_cancel = db_ctdb_transaction_cancel;
DEBUG(3,("db_open_ctdb: opened database '%s' with dbid 0x%x\n",
name, db_ctdb->db_id));