-/*
+/*
Unix SMB/CIFS implementation.
Database interface wrapper around ctdbd
Copyright (C) Volker Lendecke 2007-2009
#include "includes.h"
#include "system/filesys.h"
-#include "lib/util/tdb_wrap.h"
+#include "lib/tdb_wrap/tdb_wrap.h"
#include "util_tdb.h"
+#include "dbwrap/dbwrap.h"
#include "dbwrap/dbwrap_ctdb.h"
#include "dbwrap/dbwrap_rbt.h"
#include "lib/param/param.h"
struct db_ctdb_ctx {
struct db_context *db;
struct tdb_wrap *wtdb;
- uint32 db_id;
+ uint32_t db_id;
struct db_ctdb_transaction_handle *transaction;
struct g_lock_ctx *lock_ctx;
};
return map_nt_error_from_tdb(tret);
}
+struct db_ctdb_ltdb_parse_state {
+ void (*parser)(TDB_DATA key, struct ctdb_ltdb_header *header,
+ TDB_DATA data, void *private_data);
+ void *private_data;
+};
+
+static int db_ctdb_ltdb_parser(TDB_DATA key, TDB_DATA data,
+ void *private_data)
+{
+ struct db_ctdb_ltdb_parse_state *state =
+ (struct db_ctdb_ltdb_parse_state *)private_data;
+
+ if (data.dsize < sizeof(struct ctdb_ltdb_header)) {
+ return -1;
+ }
+ state->parser(
+ key, (struct ctdb_ltdb_header *)data.dptr,
+ make_tdb_data(data.dptr + sizeof(struct ctdb_ltdb_header),
+ data.dsize - sizeof(struct ctdb_ltdb_header)),
+ state->private_data);
+ return 0;
+}
+
+static NTSTATUS db_ctdb_ltdb_parse(
+ struct db_ctdb_ctx *db, TDB_DATA key,
+ void (*parser)(TDB_DATA key, struct ctdb_ltdb_header *header,
+ TDB_DATA data, void *private_data),
+ void *private_data)
+{
+ struct db_ctdb_ltdb_parse_state state;
+ int ret;
+
+ state.parser = parser;
+ state.private_data = private_data;
+
+ ret = tdb_parse_record(db->wtdb->tdb, key, db_ctdb_ltdb_parser,
+ &state);
+ if (ret == -1) {
+ return NT_STATUS_NOT_FOUND;
+ }
+ return NT_STATUS_OK;
+}
+
+struct db_ctdb_ltdb_fetch_state {
+ struct ctdb_ltdb_header *header;
+ TALLOC_CTX *mem_ctx;
+ TDB_DATA *data;
+ bool oom;
+};
+
+static void db_ctdb_ltdb_fetch_parser(
+ TDB_DATA key, struct ctdb_ltdb_header *header,
+ TDB_DATA data, void *private_data)
+{
+ struct db_ctdb_ltdb_fetch_state *state =
+ (struct db_ctdb_ltdb_fetch_state *)private_data;
+
+ if (state->header != NULL) {
+ memcpy(state->header, header, sizeof(struct ctdb_ltdb_header));
+ }
+ if (state->data == NULL) {
+ return;
+ }
+ state->data->dsize = data.dsize;
+ if (data.dsize == 0) {
+ state->data->dptr = NULL;
+ return;
+ }
+ state->data->dptr = talloc_memdup(state->mem_ctx, data.dptr,
+ data.dsize);
+ if (state->data->dptr == NULL) {
+ state->oom = true;
+ return;
+ }
+}
/**
* fetch a record from the tdb, separating out the header
TALLOC_CTX *mem_ctx,
TDB_DATA *data)
{
- TDB_DATA rec;
+ struct db_ctdb_ltdb_fetch_state state;
NTSTATUS status;
- rec = tdb_fetch_compat(db->wtdb->tdb, key);
- if (rec.dsize < sizeof(struct ctdb_ltdb_header)) {
- status = NT_STATUS_NOT_FOUND;
+ state.header = header;
+ state.mem_ctx = mem_ctx;
+ state.data = data;
+ state.oom = false;
+
+ status = db_ctdb_ltdb_parse(db, key, db_ctdb_ltdb_fetch_parser,
+ &state);
+ if (!NT_STATUS_IS_OK(status)) {
if (data) {
ZERO_STRUCTP(data);
}
header->dmaster = (uint32_t)-1;
header->rsn = 0;
}
- goto done;
- }
-
- if (header) {
- *header = *(struct ctdb_ltdb_header *)rec.dptr;
+ return status;
}
-
- 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;
- }
- }
+ if (state.oom) {
+ return NT_STATUS_NO_MEMORY;
}
-
- status = NT_STATUS_OK;
-
-done:
- SAFE_FREE(rec.dptr);
- return status;
+ return NT_STATUS_OK;
}
/*
/*
form a ctdb_rec_data record from a key/data pair
-
- 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,
+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)
{
size_t length;
struct ctdb_rec_data *d;
- length = offsetof(struct ctdb_rec_data, data) + key.dsize +
- data.dsize + (header?sizeof(*header):0);
+ length = offsetof(struct ctdb_rec_data, data) + key.dsize +
+ data.dsize + sizeof(*header);
d = (struct ctdb_rec_data *)talloc_size(mem_ctx, length);
if (d == NULL) {
return NULL;
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);
- }
+
+ 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);
return d;
}
/* helper function for marshalling multiple records */
-static struct ctdb_marshall_buffer *db_ctdb_marshall_add(TALLOC_CTX *mem_ctx,
+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,
return data;
}
-/*
- loop over a marshalling buffer
+/*
+ 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)
+static struct ctdb_rec_data *db_ctdb_marshall_loop_next_key(
+ struct ctdb_marshall_buffer *m, struct ctdb_rec_data *r, TDB_DATA *key)
{
if (r == NULL) {
r = (struct ctdb_rec_data *)&m->data[0];
r = (struct ctdb_rec_data *)(r->length + (uint8_t *)r);
}
- if (reqid != NULL) {
- *reqid = r->reqid;
- }
+ key->dptr = &r->data[0];
+ key->dsize = r->keylen;
+ return r;
+}
- 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);
- }
+static bool db_ctdb_marshall_buf_parse(
+ struct ctdb_rec_data *r, uint32_t *reqid,
+ struct ctdb_ltdb_header **header, TDB_DATA *data)
+{
+ if (r->datalen < sizeof(struct ctdb_ltdb_header)) {
+ return false;
}
- if (header != NULL) {
- if (r->datalen < sizeof(*header)) {
- return NULL;
- }
- *header = *(struct ctdb_ltdb_header *)&r->data[r->keylen];
- }
+ *reqid = r->reqid;
- return r;
+ data->dptr = &r->data[r->keylen] + sizeof(struct ctdb_ltdb_header);
+ data->dsize = r->datalen - sizeof(struct ctdb_ltdb_header);
+
+ *header = (struct ctdb_ltdb_header *)&r->data[r->keylen];
+
+ return true;
}
/**
h = talloc_zero(db, struct db_ctdb_transaction_handle);
if (h == NULL) {
- DEBUG(0,(__location__ " oom for transaction handle\n"));
+ DEBUG(0,(__location__ " oom for transaction handle\n"));
return -1;
}
return 0;
}
-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)
+static bool parse_newest_in_marshall_buffer(
+ struct ctdb_marshall_buffer *buf, TDB_DATA key,
+ void (*parser)(TDB_DATA key, struct ctdb_ltdb_header *header,
+ TDB_DATA data, void *private_data),
+ void *private_data)
{
struct ctdb_rec_data *rec = NULL;
- struct ctdb_ltdb_header h;
- bool found = false;
+ struct ctdb_ltdb_header *h = NULL;
TDB_DATA data;
int i;
return false;
}
- ZERO_STRUCT(h);
- ZERO_STRUCT(data);
-
/*
* Walk the list of records written during this
* transaction. If we want to read one we have already
*/
for (i=0; i<buf->count; i++) {
- TDB_DATA tkey, tdata;
+ TDB_DATA tkey;
uint32_t reqid;
- struct ctdb_ltdb_header hdr;
- ZERO_STRUCT(hdr);
-
- rec = db_ctdb_marshall_loop_next(buf, rec, &reqid, &hdr, &tkey,
- &tdata);
+ rec = db_ctdb_marshall_loop_next_key(buf, rec, &tkey);
if (rec == NULL) {
return false;
}
- if (tdb_data_equal(key, tkey)) {
- found = true;
- data = tdata;
- h = hdr;
+ if (!tdb_data_equal(key, tkey)) {
+ continue;
+ }
+
+ if (!db_ctdb_marshall_buf_parse(rec, &reqid, &h, &data)) {
+ return false;
}
}
- if (!found) {
+ if (h == NULL) {
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;
- }
+ parser(key, h, data, private_data);
+
+ return true;
+}
+
+struct pull_newest_from_marshall_buffer_state {
+ struct ctdb_ltdb_header *pheader;
+ TALLOC_CTX *mem_ctx;
+ TDB_DATA *pdata;
+};
+
+static void pull_newest_from_marshall_buffer_parser(
+ TDB_DATA key, struct ctdb_ltdb_header *header,
+ TDB_DATA data, void *private_data)
+{
+ struct pull_newest_from_marshall_buffer_state *state =
+ (struct pull_newest_from_marshall_buffer_state *)private_data;
- if (pheader != NULL) {
- *pheader = h;
+ if (state->pheader != NULL) {
+ memcpy(state->pheader, header, sizeof(*state->pheader));
}
+ if (state->pdata != NULL) {
+ state->pdata->dsize = data.dsize;
+ state->pdata->dptr = (uint8_t *)talloc_memdup(
+ state->mem_ctx, data.dptr, data.dsize);
+ }
+}
+
+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 pull_newest_from_marshall_buffer_state state;
+ state.pheader = pheader;
+ state.mem_ctx = mem_ctx;
+ state.pdata = pdata;
+
+ if (!parse_newest_in_marshall_buffer(
+ buf, key, pull_newest_from_marshall_buffer_parser,
+ &state)) {
+ return false;
+ }
+ if ((pdata != NULL) && (pdata->dsize != 0) && (pdata->dptr == NULL)) {
+ /* ENOMEM */
+ return false;
+ }
return true;
}
result->private_data = ctx->transaction;
result->key.dsize = key.dsize;
- result->key.dptr = (uint8 *)talloc_memdup(result, key.dptr, key.dsize);
+ result->key.dptr = (uint8_t *)talloc_memdup(result, key.dptr,
+ key.dsize);
if (result->key.dptr == NULL) {
DEBUG(0, ("talloc failed\n"));
TALLOC_FREE(result);
result->value.dptr = NULL;
if ((result->value.dsize != 0)
- && !(result->value.dptr = (uint8 *)talloc_memdup(
+ && !(result->value.dptr = (uint8_t *)talloc_memdup(
result, ctdb_data.dptr + sizeof(struct ctdb_ltdb_header),
result->value.dsize))) {
DEBUG(0, ("talloc failed\n"));
rec = db_ctdb_fetch_locked_transaction(ctx, mem_ctx, key);
if (rec == NULL) {
- ctx->db->transaction_cancel(ctx->db);
+ ctx->db->transaction_cancel(ctx->db);
return NULL;
}
return status;
}
-/*
+/*
a record delete inside a transaction
*/
static NTSTATUS db_ctdb_delete_transaction(struct db_record *rec)
return status;
}
+static void db_ctdb_fetch_db_seqnum_parser(
+ TDB_DATA key, struct ctdb_ltdb_header *header,
+ TDB_DATA data, void *private_data)
+{
+ uint64_t *seqnum = (uint64_t *)private_data;
+
+ if (data.dsize != sizeof(uint64_t)) {
+ *seqnum = 0;
+ return;
+ }
+ memcpy(seqnum, data.dptr, sizeof(*seqnum));
+}
+
/**
* Fetch the db sequence number of a persistent db directly from the db.
*/
uint64_t *seqnum)
{
NTSTATUS status;
- const char *keyname = CTDB_DB_SEQNUM_KEY;
TDB_DATA key;
- TDB_DATA data;
- struct ctdb_ltdb_header header;
- TALLOC_CTX *mem_ctx = talloc_stackframe();
if (seqnum == NULL) {
return NT_STATUS_INVALID_PARAMETER;
}
- key = string_term_tdb_data(keyname);
-
- status = db_ctdb_ltdb_fetch(db, key, &header, mem_ctx, &data);
- if (!NT_STATUS_IS_OK(status) &&
- !NT_STATUS_EQUAL(status, NT_STATUS_NOT_FOUND))
- {
- goto done;
- }
+ key = string_term_tdb_data(CTDB_DB_SEQNUM_KEY);
- status = NT_STATUS_OK;
+ status = db_ctdb_ltdb_parse(
+ db, key, db_ctdb_fetch_db_seqnum_parser, seqnum);
- if (data.dsize != sizeof(uint64_t)) {
+ if (NT_STATUS_IS_OK(status)) {
+ return NT_STATUS_OK;
+ }
+ if (NT_STATUS_EQUAL(status, NT_STATUS_NOT_FOUND)) {
*seqnum = 0;
- goto done;
+ return NT_STATUS_OK;
}
-
- *seqnum = *(uint64_t *)data.dptr;
-
-done:
- TALLOC_FREE(mem_ctx);
return status;
}
if (new_seqnum == old_seqnum) {
/* Recovery prevented all our changes: retry. */
goto again;
- } else if (new_seqnum != (old_seqnum + 1)) {
+ }
+ if (new_seqnum != (old_seqnum + 1)) {
DEBUG(0, (__location__ " ERROR: new_seqnum[%lu] != "
"old_seqnum[%lu] + (0 or 1) after failed "
"TRANS3_COMMIT - this should not happen!\n",
if (threshold != 0) {
double timediff = timeval_elapsed(&crec->lock_time);
if ((timediff * 1000) > threshold) {
- DEBUG(0, ("Held tdb lock %f seconds\n", timediff));
+ const char *key;
+
+ key = hex_encode_talloc(data,
+ (unsigned char *)data->key.dptr,
+ data->key.dsize);
+ DEBUG(0, ("Held tdb lock on db %s, key %s %f seconds\n",
+ tdb_name(crec->ctdb_ctx->wtdb->tdb), key,
+ timediff));
}
}
return 0;
}
+/**
+ * Check whether we have a valid local copy of the given record,
+ * either for reading or for writing.
+ */
+static bool db_ctdb_can_use_local_copy(TDB_DATA ctdb_data, bool read_only)
+{
+ struct ctdb_ltdb_header *hdr;
+
+ if (ctdb_data.dptr == NULL)
+ return false;
+
+ if (ctdb_data.dsize < sizeof(struct ctdb_ltdb_header))
+ return false;
+
+ hdr = (struct ctdb_ltdb_header *)ctdb_data.dptr;
+
+#ifdef HAVE_CTDB_WANT_READONLY_DECL
+ if (hdr->dmaster != get_my_vnn()) {
+ /* If we're not dmaster, it must be r/o copy. */
+ return read_only && (hdr->flags & CTDB_REC_RO_HAVE_READONLY);
+ }
+
+ /*
+ * If we want write access, no one may have r/o copies.
+ */
+ return read_only || !(hdr->flags & CTDB_REC_RO_HAVE_DELEGATIONS);
+#else
+ return (hdr->dmaster == get_my_vnn());
+#endif
+}
+
static struct db_record *fetch_locked_internal(struct db_ctdb_ctx *ctx,
TALLOC_CTX *mem_ctx,
- TDB_DATA key)
+ TDB_DATA key,
+ bool tryonly)
{
struct db_record *result;
struct db_ctdb_rec *crec;
NTSTATUS status;
TDB_DATA ctdb_data;
int migrate_attempts = 0;
+ int lockret;
if (!(result = talloc(mem_ctx, struct db_record))) {
DEBUG(0, ("talloc failed\n"));
return NULL;
}
+ result->db = ctx->db;
result->private_data = (void *)crec;
crec->ctdb_ctx = ctx;
result->key.dsize = key.dsize;
- result->key.dptr = (uint8 *)talloc_memdup(result, key.dptr, key.dsize);
+ result->key.dptr = (uint8_t *)talloc_memdup(result, key.dptr,
+ key.dsize);
if (result->key.dptr == NULL) {
DEBUG(0, ("talloc failed\n"));
TALLOC_FREE(result);
TALLOC_FREE(keystr);
}
- if (tdb_chainlock(ctx->wtdb->tdb, key) != 0) {
+ lockret = tryonly
+ ? tdb_chainlock_nonblock(ctx->wtdb->tdb, key)
+ : tdb_chainlock(ctx->wtdb->tdb, key);
+ if (lockret != 0) {
DEBUG(3, ("tdb_chainlock failed\n"));
TALLOC_FREE(result);
return NULL;
* take the shortcut and just return it.
*/
- if ((ctdb_data.dptr == NULL) ||
- (ctdb_data.dsize < sizeof(struct ctdb_ltdb_header)) ||
- ((struct ctdb_ltdb_header *)ctdb_data.dptr)->dmaster != get_my_vnn()
-#if 0
- || (random() % 2 != 0)
-#endif
-) {
+ if (!db_ctdb_can_use_local_copy(ctdb_data, false)) {
SAFE_FREE(ctdb_data.dptr);
tdb_chainunlock(ctx->wtdb->tdb, key);
talloc_set_destructor(result, NULL);
+ if (tryonly && (migrate_attempts != 0)) {
+ DEBUG(5, ("record migrated away again\n"));
+ TALLOC_FREE(result);
+ return NULL;
+ }
+
migrate_attempts += 1;
- DEBUG(10, ("ctdb_data.dptr = %p, dmaster = %u (%u)\n",
+ DEBUG(10, ("ctdb_data.dptr = %p, dmaster = %u (%u) %u\n",
ctdb_data.dptr, ctdb_data.dptr ?
((struct ctdb_ltdb_header *)ctdb_data.dptr)->dmaster : -1,
- get_my_vnn()));
+ get_my_vnn(),
+ ctdb_data.dptr ?
+ ((struct ctdb_ltdb_header *)ctdb_data.dptr)->flags : 0));
status = ctdbd_migrate(messaging_ctdbd_connection(), ctx->db_id,
key);
}
if (migrate_attempts > 10) {
- DEBUG(0, ("db_ctdb_fetch_locked needed %d attempts\n",
+ DEBUG(0, ("db_ctdb_fetch_locked for %s key %s needed %d "
+ "attempts\n", tdb_name(ctx->wtdb->tdb),
+ hex_encode_talloc(talloc_tos(),
+ (unsigned char *)key.dptr,
+ key.dsize),
migrate_attempts));
}
result->value.dptr = NULL;
if ((result->value.dsize != 0)
- && !(result->value.dptr = (uint8 *)talloc_memdup(
+ && !(result->value.dptr = (uint8_t *)talloc_memdup(
result, ctdb_data.dptr + sizeof(crec->header),
result->value.dsize))) {
DEBUG(0, ("talloc failed\n"));
return db_ctdb_fetch_locked_persistent(ctx, mem_ctx, key);
}
- return fetch_locked_internal(ctx, mem_ctx, key);
+ return fetch_locked_internal(ctx, mem_ctx, key, false);
+}
+
+static struct db_record *db_ctdb_try_fetch_locked(struct db_context *db,
+ TALLOC_CTX *mem_ctx,
+ TDB_DATA 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, true);
}
/*
* take the shortcut and just return it.
* we bypass the dmaster check for persistent databases
*/
- if ((ctdb_data.dptr != NULL) &&
- (ctdb_data.dsize >= sizeof(struct ctdb_ltdb_header)) &&
- ((struct ctdb_ltdb_header *)ctdb_data.dptr)->dmaster == get_my_vnn())
- {
- /* we are the dmaster - avoid the ctdb protocol op */
-
+ if (db_ctdb_can_use_local_copy(ctdb_data, true)) {
+ /*
+ * We have a valid local copy - avoid the ctdb protocol op
+ */
data->dsize = ctdb_data.dsize - sizeof(struct ctdb_ltdb_header);
- data->dptr = (uint8 *)talloc_memdup(
+ data->dptr = (uint8_t *)talloc_memdup(
mem_ctx, ctdb_data.dptr+sizeof(struct ctdb_ltdb_header),
data->dsize);
SAFE_FREE(ctdb_data.dptr);
- /* we weren't able to get it locally - ask ctdb to fetch it for us */
+ /*
+ * We weren't able to get it locally - ask ctdb to fetch it for us.
+ * If we already had *something*, it's probably worth making a local
+ * read-only copy.
+ */
status = ctdbd_fetch(messaging_ctdbd_connection(), ctx->db_id, key,
- mem_ctx, data);
+ mem_ctx, data,
+ ctdb_data.dsize >= sizeof(struct ctdb_ltdb_header));
if (!NT_STATUS_IS_OK(status)) {
DEBUG(5, ("ctdbd_fetch failed: %s\n", nt_errstr(status)));
}
struct db_context *db;
int (*fn)(struct db_record *rec, void *private_data);
void *private_data;
+ int count;
};
static void traverse_callback(TDB_DATA key, TDB_DATA data, void *private_data)
void *private_data),
void *private_data)
{
+ NTSTATUS status;
struct db_ctdb_ctx *ctx = talloc_get_type_abort(db->private_data,
struct db_ctdb_ctx);
struct traverse_state state;
state.db = db;
state.fn = fn;
state.private_data = private_data;
+ state.count = 0;
if (db->persistent) {
struct tdb_context *ltdb = ctx->wtdb->tdb;
struct db_context *newkeys = db_open_rbt(talloc_tos());
struct ctdb_marshall_buffer *mbuf = ctx->transaction->m_write;
struct ctdb_rec_data *rec=NULL;
- NTSTATUS status;
int i;
int count = 0;
for (i=0; i<mbuf->count; i++) {
TDB_DATA key;
- rec =db_ctdb_marshall_loop_next(mbuf, rec,
- NULL, NULL,
- &key, NULL);
+ rec = db_ctdb_marshall_loop_next_key(
+ mbuf, rec, &key);
SMB_ASSERT(rec != NULL);
if (!tdb_exists(ltdb, key)) {
return ret;
}
-
- ctdbd_traverse(ctx->db_id, traverse_callback, &state);
- return 0;
+ status = ctdbd_traverse(ctx->db_id, traverse_callback, &state);
+ if (!NT_STATUS_IS_OK(status)) {
+ return -1;
+ }
+ return state.count;
}
static NTSTATUS db_ctdb_store_deny(struct db_record *rec, TDB_DATA data, int flag)
rec.delete_rec = db_ctdb_delete_deny;
rec.private_data = state->db;
state->fn(&rec, state->private_data);
+ state->count++;
}
static int traverse_persistent_callback_read(TDB_CONTEXT *tdb, TDB_DATA kbuf, TDB_DATA dbuf,
rec.value.dsize -= sizeof(struct ctdb_ltdb_header);
rec.value.dptr += sizeof(struct ctdb_ltdb_header);
+ state->count++;
return state->fn(&rec, state->private_data);
}
void *private_data),
void *private_data)
{
+ NTSTATUS status;
struct db_ctdb_ctx *ctx = talloc_get_type_abort(db->private_data,
struct db_ctdb_ctx);
struct traverse_state state;
state.db = db;
state.fn = fn;
state.private_data = private_data;
+ state.count = 0;
if (db->persistent) {
/* for persistent databases we don't need to do a ctdb traverse,
return tdb_traverse_read(ctx->wtdb->tdb, traverse_persistent_callback_read, &state);
}
- ctdbd_traverse(ctx->db_id, traverse_read_callback, &state);
- return 0;
+ status = ctdbd_traverse(ctx->db_id, traverse_read_callback, &state);
+ if (!NT_STATUS_IS_OK(status)) {
+ return -1;
+ }
+ return state.count;
}
static int db_ctdb_get_seqnum(struct db_context *db)
return tdb_get_seqnum(ctx->wtdb->tdb);
}
-static int db_ctdb_get_flags(struct db_context *db)
+static void db_ctdb_id(struct db_context *db, const uint8_t **id,
+ size_t *idlen)
{
- 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_ctdb_ctx *ctx = talloc_get_type_abort(
+ db->private_data, struct db_ctdb_ctx);
+
+ *id = (uint8_t *)&ctx->db_id;
+ *idlen = sizeof(ctx->db_id);
}
struct db_context *db_open_ctdb(TALLOC_CTX *mem_ctx,
const char *name,
int hash_size, int tdb_flags,
- int open_flags, mode_t mode)
+ int open_flags, mode_t mode,
+ enum dbwrap_lock_order lock_order)
{
struct db_context *result;
struct db_ctdb_ctx *db_ctdb;
char *db_path;
struct ctdbd_connection *conn;
struct loadparm_context *lp_ctx;
+ struct ctdb_db_priority prio;
+ NTSTATUS status;
+ int cstatus;
if (!lp_clustering()) {
DEBUG(10, ("Clustering disabled -- no ctdb\n"));
db_path = ctdbd_dbpath(conn, db_ctdb, db_ctdb->db_id);
result->persistent = ((tdb_flags & TDB_CLEAR_IF_FIRST) == 0);
+ result->lock_order = lock_order;
/* only pass through specific flags */
tdb_flags &= TDB_SEQNUM;
chmod(db_path, mode);
}
- lp_ctx = loadparm_init_s3(db_path, loadparm_s3_context());
+ prio.db_id = db_ctdb->db_id;
+ prio.priority = lock_order;
+
+ status = ctdbd_control_local(
+ conn, CTDB_CONTROL_SET_DB_PRIORITY, 0, 0,
+ make_tdb_data((uint8_t *)&prio, sizeof(prio)),
+ NULL, NULL, &cstatus);
+
+ if (!NT_STATUS_IS_OK(status) || (cstatus != 0)) {
+ DEBUG(1, ("CTDB_CONTROL_SET_DB_PRIORITY failed: %s, %d\n",
+ nt_errstr(status), cstatus));
+ TALLOC_FREE(result);
+ return NULL;
+ }
+
+ lp_ctx = loadparm_init_s3(db_path, loadparm_s3_helpers());
db_ctdb->wtdb = tdb_wrap_open(db_ctdb, db_path, hash_size, tdb_flags,
O_RDWR, 0, lp_ctx);
result->private_data = (void *)db_ctdb;
result->fetch_locked = db_ctdb_fetch_locked;
- result->fetch = db_ctdb_fetch;
+ result->try_fetch_locked = db_ctdb_try_fetch_locked;
result->parse_record = db_ctdb_parse_record;
result->traverse = db_ctdb_traverse;
result->traverse_read = db_ctdb_traverse_read;
result->get_seqnum = db_ctdb_get_seqnum;
- 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;
+ result->id = db_ctdb_id;
+ result->stored_callback = NULL;
DEBUG(3,("db_open_ctdb: opened database '%s' with dbid 0x%x\n",
name, db_ctdb->db_id));
struct db_context *db_open_ctdb(TALLOC_CTX *mem_ctx,
const char *name,
int hash_size, int tdb_flags,
- int open_flags, mode_t mode)
+ int open_flags, mode_t mode,
+ enum dbwrap_lock_order lock_order)
{
DEBUG(3, ("db_open_ctdb: no cluster support!\n"));
+ errno = ENOSYS;
return NULL;
}