diff options
author | Michael Adam <obnox@samba.org> | 2011-07-07 17:42:08 +0200 |
---|---|---|
committer | Michael Adam <obnox@samba.org> | 2011-07-29 13:34:22 +0200 |
commit | 0b5c4a601a983aab06e4aba158cd9359babd1e71 (patch) | |
tree | 8e2806398d731036115661a88867be107783e538 /source3/lib/dbwrap | |
parent | d4c93ca721d0edd8780ce84e7cccd0e5a1ea4f37 (diff) | |
download | samba-0b5c4a601a983aab06e4aba158cd9359babd1e71.tar.gz samba-0b5c4a601a983aab06e4aba158cd9359babd1e71.tar.bz2 samba-0b5c4a601a983aab06e4aba158cd9359babd1e71.zip |
s3:dbwrap: move all .c and .h files of dbwrap to lib/dbwrap/
Autobuild-User: Michael Adam <obnox@samba.org>
Autobuild-Date: Fri Jul 29 13:34:22 CEST 2011 on sn-devel-104
Diffstat (limited to 'source3/lib/dbwrap')
-rw-r--r-- | source3/lib/dbwrap/dbwrap.c | 66 | ||||
-rw-r--r-- | source3/lib/dbwrap/dbwrap.h | 138 | ||||
-rw-r--r-- | source3/lib/dbwrap/dbwrap_ctdb.c | 1440 | ||||
-rw-r--r-- | source3/lib/dbwrap/dbwrap_file.c | 410 | ||||
-rw-r--r-- | source3/lib/dbwrap/dbwrap_open.c | 2 | ||||
-rw-r--r-- | source3/lib/dbwrap/dbwrap_rbt.c | 420 | ||||
-rw-r--r-- | source3/lib/dbwrap/dbwrap_tdb.c | 379 | ||||
-rw-r--r-- | source3/lib/dbwrap/dbwrap_util.c | 569 |
8 files changed, 3423 insertions, 1 deletions
diff --git a/source3/lib/dbwrap/dbwrap.c b/source3/lib/dbwrap/dbwrap.c new file mode 100644 index 0000000000..6a6d4c64e5 --- /dev/null +++ b/source3/lib/dbwrap/dbwrap.c @@ -0,0 +1,66 @@ +/* + Unix SMB/CIFS implementation. + Database interface wrapper + Copyright (C) Jim McDonough <jmcd@us.ibm.com> 2006 + + Major code contributions from Aleksey Fedoseev (fedoseev@ru.ibm.com) + + 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 "includes.h" +#include "dbwrap/dbwrap.h" +#include "dbwrap/dbwrap_private.h" + +/* + * Fall back using fetch_locked if no genuine fetch operation is provided + */ + +int dbwrap_fallback_fetch(struct db_context *db, TALLOC_CTX *mem_ctx, + TDB_DATA key, TDB_DATA *data) +{ + struct db_record *rec; + + if (!(rec = db->fetch_locked(db, mem_ctx, key))) { + return -1; + } + + data->dsize = rec->value.dsize; + data->dptr = talloc_move(mem_ctx, &rec->value.dptr); + TALLOC_FREE(rec); + return 0; +} + +/* + * Fall back using fetch if no genuine parse operation is provided + */ + +int dbwrap_fallback_parse_record(struct db_context *db, TDB_DATA key, + int (*parser)(TDB_DATA key, + TDB_DATA data, + void *private_data), + void *private_data) +{ + TDB_DATA data; + int res; + + res = db->fetch(db, talloc_tos(), key, &data); + if (res != 0) { + return res; + } + + res = parser(key, data, private_data); + TALLOC_FREE(data.dptr); + return res; +} diff --git a/source3/lib/dbwrap/dbwrap.h b/source3/lib/dbwrap/dbwrap.h new file mode 100644 index 0000000000..9084f87d30 --- /dev/null +++ b/source3/lib/dbwrap/dbwrap.h @@ -0,0 +1,138 @@ +/* + Unix SMB/CIFS implementation. + Database interface wrapper around tdb + Copyright (C) Volker Lendecke 2005-2007 + + 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/>. +*/ + +#ifndef __DBWRAP_H__ +#define __DBWRAP_H__ + +#include "tdb_compat.h" + +struct db_record { + TDB_DATA key, value; + NTSTATUS (*store)(struct db_record *rec, TDB_DATA data, int flag); + NTSTATUS (*delete_rec)(struct db_record *rec); + void *private_data; +}; + +struct db_context { + struct db_record *(*fetch_locked)(struct db_context *db, + TALLOC_CTX *mem_ctx, + TDB_DATA key); + int (*fetch)(struct db_context *db, TALLOC_CTX *mem_ctx, + TDB_DATA key, TDB_DATA *data); + int (*traverse)(struct db_context *db, + int (*f)(struct db_record *rec, + void *private_data), + void *private_data); + int (*traverse_read)(struct db_context *db, + int (*f)(struct db_record *rec, + void *private_data), + void *private_data); + int (*get_seqnum)(struct db_context *db); + int (*get_flags)(struct db_context *db); + int (*transaction_start)(struct db_context *db); + int (*transaction_commit)(struct db_context *db); + int (*transaction_cancel)(struct db_context *db); + int (*parse_record)(struct db_context *db, TDB_DATA key, + int (*parser)(TDB_DATA key, TDB_DATA data, + void *private_data), + void *private_data); + void *private_data; + bool persistent; +}; + +struct db_context *db_open_rbt(TALLOC_CTX *mem_ctx); + +struct db_context *db_open_tdb(TALLOC_CTX *mem_ctx, + const char *name, + int hash_size, int tdb_flags, + int open_flags, mode_t mode); + +struct messaging_context; + +#ifdef CLUSTER_SUPPORT +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); +#endif + +struct db_context *db_open_file(TALLOC_CTX *mem_ctx, + struct messaging_context *msg_ctx, + const char *name, + int hash_size, int tdb_flags, + int open_flags, mode_t mode); + + +NTSTATUS dbwrap_delete(struct db_context *db, TDB_DATA key); +NTSTATUS dbwrap_store(struct db_context *db, TDB_DATA key, + TDB_DATA data, int flags); +TDB_DATA dbwrap_fetch(struct db_context *db, TALLOC_CTX *mem_ctx, + TDB_DATA key); +NTSTATUS dbwrap_delete_bystring(struct db_context *db, const char *key); +NTSTATUS dbwrap_store_bystring(struct db_context *db, const char *key, + TDB_DATA data, int flags); +TDB_DATA dbwrap_fetch_bystring(struct db_context *db, TALLOC_CTX *mem_ctx, + const char *key); + +/* The following definitions come from lib/dbwrap_util.c */ + +int32_t dbwrap_fetch_int32(struct db_context *db, const char *keystr); +int dbwrap_store_int32(struct db_context *db, const char *keystr, int32_t v); +bool dbwrap_fetch_uint32(struct db_context *db, const char *keystr, + uint32_t *val); +int dbwrap_store_uint32(struct db_context *db, const char *keystr, uint32_t v); +NTSTATUS dbwrap_change_uint32_atomic(struct db_context *db, const char *keystr, + uint32_t *oldval, uint32_t change_val); +NTSTATUS dbwrap_trans_change_uint32_atomic(struct db_context *db, + const char *keystr, + uint32_t *oldval, + uint32_t change_val); +NTSTATUS dbwrap_change_int32_atomic(struct db_context *db, const char *keystr, + int32_t *oldval, int32_t change_val); +NTSTATUS dbwrap_trans_change_int32_atomic(struct db_context *db, + const char *keystr, + int32_t *oldval, + int32_t change_val); +NTSTATUS dbwrap_trans_store(struct db_context *db, TDB_DATA key, TDB_DATA dbuf, + int flag); +NTSTATUS dbwrap_trans_delete(struct db_context *db, TDB_DATA key); +NTSTATUS dbwrap_trans_store_int32(struct db_context *db, const char *keystr, + int32_t v); +NTSTATUS dbwrap_trans_store_uint32(struct db_context *db, const char *keystr, + uint32_t v); +NTSTATUS dbwrap_trans_store_bystring(struct db_context *db, const char *key, + TDB_DATA data, int flags); +NTSTATUS dbwrap_trans_delete_bystring(struct db_context *db, const char *key); +NTSTATUS dbwrap_trans_do(struct db_context *db, + NTSTATUS (*action)(struct db_context *, void *), + void *private_data); +NTSTATUS dbwrap_trans_traverse(struct db_context *db, + int (*f)(struct db_record*, void*), + void *private_data); +NTSTATUS dbwrap_traverse(struct db_context *db, + int (*f)(struct db_record*, void*), + void *private_data); + +NTSTATUS dbwrap_delete_bystring_upper(struct db_context *db, const char *key); +NTSTATUS dbwrap_store_bystring_upper(struct db_context *db, const char *key, + TDB_DATA data, int flags); +TDB_DATA dbwrap_fetch_bystring_upper(struct db_context *db, TALLOC_CTX *mem_ctx, + const char *key); + +#endif /* __DBWRAP_H__ */ diff --git a/source3/lib/dbwrap/dbwrap_ctdb.c b/source3/lib/dbwrap/dbwrap_ctdb.c new file mode 100644 index 0000000000..454a28399f --- /dev/null +++ b/source3/lib/dbwrap/dbwrap_ctdb.c @@ -0,0 +1,1440 @@ +/* + Unix SMB/CIFS implementation. + Database interface wrapper around ctdbd + 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 "includes.h" +#include "system/filesys.h" +#include "lib/util/tdb_wrap.h" +#include "util_tdb.h" +#ifdef CLUSTER_SUPPORT +#include "ctdb.h" +#include "ctdb_private.h" +#include "ctdbd_conn.h" +#include "dbwrap/dbwrap.h" +#include "g_lock.h" +#include "messages.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 { + struct db_ctdb_ctx *ctdb_ctx; + struct ctdb_ltdb_header header; + struct timeval lock_time; +}; + +static NTSTATUS tdb_error_to_ntstatus(struct tdb_context *tdb) +{ + NTSTATUS status; + enum TDB_ERROR tret = tdb_error(tdb); + + switch (tret) { + case TDB_ERR_EXISTS: + status = NT_STATUS_OBJECT_NAME_COLLISION; + break; + case TDB_ERR_NOEXIST: + status = NT_STATUS_OBJECT_NAME_NOT_FOUND; + break; + default: + status = NT_STATUS_INTERNAL_DB_CORRUPTION; + break; + } + + return status; +} + + +/** + * 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) +{ + TDB_DATA rec; + NTSTATUS status; + + rec = tdb_fetch_compat(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; + + rec.dsize = data.dsize + sizeof(struct ctdb_ltdb_header); + rec.dptr = (uint8_t *)talloc_size(tmp_ctx, rec.dsize); + + if (rec.dptr == NULL) { + talloc_free(tmp_ctx); + return NT_STATUS_NO_MEMORY; + } + + 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(db->wtdb->tdb, key, rec, TDB_REPLACE); + + talloc_free(tmp_ctx); + + return (ret == 0) ? NT_STATUS_OK + : tdb_error_to_ntstatus(db->wtdb->tdb); + +} + +/* + 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, + 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); + 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; +} + + +/* 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); + + m2->count++; + +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; +} + +/* + 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 (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); + } + } + + if (header != NULL) { + if (r->datalen < sizeof(*header)) { + return NULL; + } + *header = *(struct ctdb_ltdb_header *)&r->data[r->keylen]; + } + + 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(0, ("g_lock_unlock failed: %s\n", nt_errstr(status))); + return -1; + } + return 0; +} + +/** + * 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); + + 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 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 = false; + TDB_DATA data; + int i; + + if (buf == NULL) { + 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 + * written, return the last written sample. Thus we do not do + * a "break;" for the first hit, this record might have been + * overwritten later. + */ + + for (i=0; i<buf->count; i++) { + TDB_DATA tkey, tdata; + uint32_t reqid; + struct ctdb_ltdb_header hdr; + + ZERO_STRUCT(hdr); + + rec = db_ctdb_marshall_loop_next(buf, rec, &reqid, &hdr, &tkey, + &tdata); + if (rec == NULL) { + return false; + } + + if (tdb_data_equal(key, tkey)) { + found = true; + data = tdata; + h = hdr; + } + } + + 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; +} + +/** + * Fetch a record from a persistent database + * without record locking and without an active transaction. + * + * This just fetches from the local database copy. + * Since the databases are kept in syc cluster-wide, + * there is no point in doing a ctdb call to fetch the + * record from the lmaster. It does even harm since migration + * of records bump their RSN and hence render the persistent + * database inconsistent. + */ +static int db_ctdb_fetch_persistent(struct db_ctdb_ctx *db, + TALLOC_CTX *mem_ctx, + TDB_DATA key, TDB_DATA *data) +{ + NTSTATUS status; + + status = db_ctdb_ltdb_fetch(db, 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; + } + + 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_compat(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; +} + +/* + 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_compat(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; +} + +/** + * Fetch the db sequence number of a persistent db directly from the db. + */ +static NTSTATUS db_ctdb_fetch_db_seqnum_from_db(struct db_ctdb_ctx *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; + } + + status = NT_STATUS_OK; + + if (data.dsize != sizeof(uint64_t)) { + *seqnum = 0; + goto done; + } + + *seqnum = *(uint64_t *)data.dptr; + +done: + TALLOC_FREE(mem_ctx); + return status; +} + +/** + * Store the database sequence number inside a transaction. + */ +static NTSTATUS db_ctdb_store_db_seqnum(struct db_ctdb_transaction_handle *h, + uint64_t seqnum) +{ + NTSTATUS status; + const char *keyname = CTDB_DB_SEQNUM_KEY; + TDB_DATA key; + TDB_DATA data; + + key = string_term_tdb_data(keyname); + + data.dptr = (uint8_t *)&seqnum; + data.dsize = sizeof(uint64_t); + + status = db_ctdb_transaction_store(h, key, data); + + 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; + uint64_t old_seqnum, new_seqnum; + int ret; + + 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; + } + + if (h->m_write == NULL) { + /* + * No changes were made, so don't change the seqnum, + * don't push to other node, just exit with success. + */ + ret = 0; + goto done; + } + + DEBUG(5,(__location__ " Commit transaction on db 0x%08x\n", ctx->db_id)); + + /* + * As the last db action before committing, bump the database sequence + * number. Note that this undoes all changes to the seqnum records + * performed under the transaction. This record is not meant to be + * modified by user interaction. It is for internal use only... + */ + rets = db_ctdb_fetch_db_seqnum_from_db(ctx, &old_seqnum); + if (!NT_STATUS_IS_OK(rets)) { + DEBUG(1, (__location__ " failed to fetch the db sequence number " + "in transaction commit on db 0x%08x\n", ctx->db_id)); + ret = -1; + goto done; + } + + new_seqnum = old_seqnum + 1; + + rets = db_ctdb_store_db_seqnum(h, new_seqnum); + if (!NT_STATUS_IS_OK(rets)) { + DEBUG(1, (__location__ "failed to store the db sequence number " + " in transaction commit on db 0x%08x\n", ctx->db_id)); + ret = -1; + goto done; + } + +again: + /* 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) { + /* + * The TRANS3_COMMIT control should only possibly fail when a + * recovery has been running concurrently. In any case, the db + * will be the same on all nodes, either the new copy or the + * old copy. This can be detected by comparing the old and new + * local sequence numbers. + */ + rets = db_ctdb_fetch_db_seqnum_from_db(ctx, &new_seqnum); + if (!NT_STATUS_IS_OK(rets)) { + DEBUG(1, (__location__ " failed to refetch db sequence " + "number after failed TRANS3_COMMIT\n")); + ret = -1; + goto done; + } + + if (new_seqnum == old_seqnum) { + /* Recovery prevented all our changes: retry. */ + goto again; + } else 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", + (unsigned long)new_seqnum, + (unsigned long)old_seqnum)); + ret = -1; + goto done; + } + /* + * Recovery propagated our changes to all nodes, completing + * our commit for us - succeed. + */ + } + + ret = 0; + +done: + h->ctx->transaction = NULL; + talloc_free(h); + return ret; +} + + +/* + 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); +} + + + +#ifdef HAVE_CTDB_CONTROL_SCHEDULE_FOR_DELETION_DECL +static NTSTATUS db_ctdb_send_schedule_for_deletion(struct db_record *rec) +{ + NTSTATUS status; + struct ctdb_control_schedule_for_deletion *dd; + TDB_DATA indata; + int cstatus; + struct db_ctdb_rec *crec = talloc_get_type_abort( + rec->private_data, struct db_ctdb_rec); + + indata.dsize = offsetof(struct ctdb_control_schedule_for_deletion, key) + rec->key.dsize; + indata.dptr = talloc_zero_array(crec, uint8_t, indata.dsize); + if (indata.dptr == NULL) { + DEBUG(0, (__location__ " talloc failed!\n")); + return NT_STATUS_NO_MEMORY; + } + + dd = (struct ctdb_control_schedule_for_deletion *)(void *)indata.dptr; + dd->db_id = crec->ctdb_ctx->db_id; + dd->hdr = crec->header; + dd->keylen = rec->key.dsize; + memcpy(dd->key, rec->key.dptr, rec->key.dsize); + + status = ctdbd_control_local(messaging_ctdbd_connection(), + CTDB_CONTROL_SCHEDULE_FOR_DELETION, + crec->ctdb_ctx->db_id, + CTDB_CTRL_FLAG_NOREPLY, /* flags */ + indata, + NULL, /* outdata */ + NULL, /* errmsg */ + &cstatus); + talloc_free(indata.dptr); + + if (!NT_STATUS_IS_OK(status) || cstatus != 0) { + DEBUG(1, (__location__ " Error sending local control " + "SCHEDULE_FOR_DELETION: %s, cstatus = %d\n", + nt_errstr(status), cstatus)); + if (NT_STATUS_IS_OK(status)) { + status = NT_STATUS_UNSUCCESSFUL; + } + } + + return status; +} +#endif + +static NTSTATUS db_ctdb_delete(struct db_record *rec) +{ + TDB_DATA data; + NTSTATUS status; + + /* + * We have to store the header with empty data. TODO: Fix the + * tdb-level cleanup + */ + + ZERO_STRUCT(data); + + status = db_ctdb_store(rec, data, 0); + if (!NT_STATUS_IS_OK(status)) { + return status; + } + +#ifdef HAVE_CTDB_CONTROL_SCHEDULE_FOR_DELETION_DECL + status = db_ctdb_send_schedule_for_deletion(rec); +#endif + + return status; +} + +static int db_ctdb_record_destr(struct db_record* data) +{ + struct db_ctdb_rec *crec = talloc_get_type_abort( + data->private_data, struct db_ctdb_rec); + int threshold; + + DEBUG(10, (DEBUGLEVEL > 10 + ? "Unlocking db %u key %s\n" + : "Unlocking db %u key %.20s\n", + (int)crec->ctdb_ctx->db_id, + hex_encode_talloc(data, (unsigned char *)data->key.dptr, + data->key.dsize))); + + tdb_chainunlock(crec->ctdb_ctx->wtdb->tdb, data->key); + + threshold = lp_ctdb_locktime_warn_threshold(); + if (threshold != 0) { + double timediff = timeval_elapsed(&crec->lock_time); + if ((timediff * 1000) > threshold) { + DEBUG(0, ("Held tdb lock %f seconds\n", timediff)); + } + } + + return 0; +} + +static struct db_record *fetch_locked_internal(struct db_ctdb_ctx *ctx, + TALLOC_CTX *mem_ctx, + TDB_DATA key) +{ + struct db_record *result; + struct db_ctdb_rec *crec; + NTSTATUS status; + TDB_DATA ctdb_data; + int migrate_attempts = 0; + + if (!(result = talloc(mem_ctx, struct db_record))) { + DEBUG(0, ("talloc failed\n")); + return NULL; + } + + if (!(crec = talloc_zero(result, struct db_ctdb_rec))) { + DEBUG(0, ("talloc failed\n")); + TALLOC_FREE(result); + return NULL; + } + + 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); + if (result->key.dptr == NULL) { + DEBUG(0, ("talloc failed\n")); + TALLOC_FREE(result); + return NULL; + } + + /* + * Do a blocking lock on the record + */ +again: + + if (DEBUGLEVEL >= 10) { + 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; + } + + result->store = db_ctdb_store; + result->delete_rec = db_ctdb_delete; + talloc_set_destructor(result, db_ctdb_record_destr); + + ctdb_data = tdb_fetch_compat(ctx->wtdb->tdb, key); + + /* + * See if we have a valid record and we are the dmaster. If so, we can + * 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 +) { + SAFE_FREE(ctdb_data.dptr); + tdb_chainunlock(ctx->wtdb->tdb, key); + talloc_set_destructor(result, NULL); + + migrate_attempts += 1; + + DEBUG(10, ("ctdb_data.dptr = %p, dmaster = %u (%u)\n", + ctdb_data.dptr, ctdb_data.dptr ? + ((struct ctdb_ltdb_header *)ctdb_data.dptr)->dmaster : -1, + get_my_vnn())); + + status = ctdbd_migrate(messaging_ctdbd_connection(), ctx->db_id, + key); + if (!NT_STATUS_IS_OK(status)) { + DEBUG(5, ("ctdb_migrate failed: %s\n", + nt_errstr(status))); + TALLOC_FREE(result); + return NULL; + } + /* now its migrated, try again */ + goto again; + } + + if (migrate_attempts > 10) { + DEBUG(0, ("db_ctdb_fetch_locked needed %d attempts\n", + migrate_attempts)); + } + + GetTimeOfDay(&crec->lock_time); + + memcpy(&crec->header, ctdb_data.dptr, sizeof(crec->header)); + + result->value.dsize = ctdb_data.dsize - sizeof(crec->header); + result->value.dptr = NULL; + + if ((result->value.dsize != 0) + && !(result->value.dptr = (uint8 *)talloc_memdup( + result, ctdb_data.dptr + sizeof(crec->header), + result->value.dsize))) { + DEBUG(0, ("talloc failed\n")); + TALLOC_FREE(result); + } + + SAFE_FREE(ctdb_data.dptr); + + return result; +} + +static struct db_record *db_ctdb_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); +} + +/* + fetch (unlocked, no migration) operation on ctdb + */ +static int db_ctdb_fetch(struct db_context *db, TALLOC_CTX *mem_ctx, + TDB_DATA key, TDB_DATA *data) +{ + struct db_ctdb_ctx *ctx = talloc_get_type_abort(db->private_data, + struct db_ctdb_ctx); + NTSTATUS status; + TDB_DATA ctdb_data; + + if (ctx->transaction) { + return db_ctdb_transaction_fetch(ctx, mem_ctx, key, data); + } + + if (db->persistent) { + return db_ctdb_fetch_persistent(ctx, mem_ctx, key, data); + } + + /* try a direct fetch */ + ctdb_data = tdb_fetch_compat(ctx->wtdb->tdb, key); + + /* + * See if we have a valid record and we are the dmaster. If so, we can + * 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 */ + + data->dsize = ctdb_data.dsize - sizeof(struct ctdb_ltdb_header); + if (data->dsize == 0) { + SAFE_FREE(ctdb_data.dptr); + data->dptr = NULL; + return 0; + } + + data->dptr = (uint8 *)talloc_memdup( + mem_ctx, ctdb_data.dptr+sizeof(struct ctdb_ltdb_header), + data->dsize); + + SAFE_FREE(ctdb_data.dptr); + + if (data->dptr == NULL) { + return -1; + } + return 0; + } + + SAFE_FREE(ctdb_data.dptr); + + /* we weren't able to get it locally - ask ctdb to fetch it for us */ + status = ctdbd_fetch(messaging_ctdbd_connection(), ctx->db_id, key, + mem_ctx, data); + if (!NT_STATUS_IS_OK(status)) { + DEBUG(5, ("ctdbd_fetch failed: %s\n", nt_errstr(status))); + return -1; + } + + return 0; +} + +struct traverse_state { + struct db_context *db; + int (*fn)(struct db_record *rec, void *private_data); + void *private_data; +}; + +static void traverse_callback(TDB_DATA key, TDB_DATA data, void *private_data) +{ + struct traverse_state *state = (struct traverse_state *)private_data; + struct db_record *rec; + TALLOC_CTX *tmp_ctx = talloc_new(state->db); + /* we have to give them a locked record to prevent races */ + rec = db_ctdb_fetch_locked(state->db, tmp_ctx, key); + if (rec && rec->value.dsize > 0) { + state->fn(rec, state->private_data); + } + talloc_free(tmp_ctx); +} + +static int traverse_persistent_callback(TDB_CONTEXT *tdb, TDB_DATA kbuf, TDB_DATA dbuf, + void *private_data) +{ + struct traverse_state *state = (struct traverse_state *)private_data; + struct db_record *rec; + TALLOC_CTX *tmp_ctx = talloc_new(state->db); + int ret = 0; + /* we have to give them a locked record to prevent races */ + rec = db_ctdb_fetch_locked(state->db, tmp_ctx, kbuf); + if (rec && rec->value.dsize > 0) { + ret = state->fn(rec, state->private_data); + } + talloc_free(tmp_ctx); + return ret; +} + +static int db_ctdb_traverse(struct db_context *db, + int (*fn)(struct db_record *rec, + void *private_data), + void *private_data) +{ + 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; + + if (db->persistent) { + /* for persistent databases we don't need to do a ctdb traverse, + we can do a faster local traverse */ + return tdb_traverse(ctx->wtdb->tdb, traverse_persistent_callback, &state); + } + + + ctdbd_traverse(ctx->db_id, traverse_callback, &state); + return 0; +} + +static NTSTATUS db_ctdb_store_deny(struct db_record *rec, TDB_DATA data, int flag) +{ + return NT_STATUS_MEDIA_WRITE_PROTECTED; +} + +static NTSTATUS db_ctdb_delete_deny(struct db_record *rec) +{ + return NT_STATUS_MEDIA_WRITE_PROTECTED; +} + +static void traverse_read_callback(TDB_DATA key, TDB_DATA data, void *private_data) +{ + struct traverse_state *state = (struct traverse_state *)private_data; + struct db_record rec; + rec.key = key; + rec.value = data; + rec.store = db_ctdb_store_deny; + rec.delete_rec = db_ctdb_delete_deny; + rec.private_data = state->db; + state->fn(&rec, state->private_data); +} + +static int traverse_persistent_callback_read(TDB_CONTEXT *tdb, TDB_DATA kbuf, TDB_DATA dbuf, + void *private_data) +{ + struct traverse_state *state = (struct traverse_state *)private_data; + struct db_record rec; + rec.key = kbuf; + rec.value = dbuf; + rec.store = db_ctdb_store_deny; + rec.delete_rec = db_ctdb_delete_deny; + rec.private_data = state->db; + + if (rec.value.dsize <= sizeof(struct ctdb_ltdb_header)) { + /* a deleted record */ + return 0; + } + rec.value.dsize -= sizeof(struct ctdb_ltdb_header); + rec.value.dptr += sizeof(struct ctdb_ltdb_header); + + return state->fn(&rec, state->private_data); +} + +static int db_ctdb_traverse_read(struct db_context *db, + int (*fn)(struct db_record *rec, + void *private_data), + void *private_data) +{ + 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; + + if (db->persistent) { + /* for persistent databases we don't need to do a ctdb traverse, + we can do a faster local traverse */ + return tdb_traverse_read(ctx->wtdb->tdb, traverse_persistent_callback_read, &state); + } + + ctdbd_traverse(ctx->db_id, traverse_read_callback, &state); + return 0; +} + +static int db_ctdb_get_seqnum(struct db_context *db) +{ + struct db_ctdb_ctx *ctx = talloc_get_type_abort(db->private_data, + struct db_ctdb_ctx); + return tdb_get_seqnum(ctx->wtdb->tdb); +} + +static int db_ctdb_get_flags(struct db_context *db) +{ + 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, + const char *name, + int hash_size, int tdb_flags, + int open_flags, mode_t mode) +{ + 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 (!(result = talloc_zero(mem_ctx, struct db_context))) { + DEBUG(0, ("talloc failed\n")); + TALLOC_FREE(result); + return NULL; + } + + if (!(db_ctdb = talloc(result, struct db_ctdb_ctx))) { + DEBUG(0, ("talloc failed\n")); + TALLOC_FREE(result); + return NULL; + } + + db_ctdb->transaction = NULL; + db_ctdb->db = result; + + conn = messaging_ctdbd_connection(); + if (conn == NULL) { + DEBUG(1, ("Could not connect to ctdb\n")); + TALLOC_FREE(result); + return NULL; + } + + 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(conn, db_ctdb, db_ctdb->db_id); + + result->persistent = ((tdb_flags & TDB_CLEAR_IF_FIRST) == 0); + + /* only pass through specific flags */ + tdb_flags &= TDB_SEQNUM; + + /* honor permissions if user has specified O_CREAT */ + if (open_flags & O_CREAT) { + chmod(db_path, mode); + } + + db_ctdb->wtdb = tdb_wrap_open(db_ctdb, db_path, hash_size, tdb_flags, O_RDWR, 0); + if (db_ctdb->wtdb == NULL) { + DEBUG(0, ("Could not open tdb %s: %s\n", db_path, strerror(errno))); + TALLOC_FREE(result); + return NULL; + } + 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->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)); + + return result; +} +#endif diff --git a/source3/lib/dbwrap/dbwrap_file.c b/source3/lib/dbwrap/dbwrap_file.c new file mode 100644 index 0000000000..6ecd72810d --- /dev/null +++ b/source3/lib/dbwrap/dbwrap_file.c @@ -0,0 +1,410 @@ +/* + Unix SMB/CIFS implementation. + Database interface using a file per record + Copyright (C) Volker Lendecke 2005 + + 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 "includes.h" + +struct db_file_ctx { + const char *dirname; + + /* We only support one locked record at a time -- everything else + * would lead to a potential deadlock anyway! */ + struct db_record *locked_record; +}; + +struct db_locked_file { + int fd; + uint8 hash; + const char *name; + const char *path; + struct db_file_ctx *parent; +}; + +/* Copy from statcache.c... */ + +static uint32 fsh(const uint8 *p, int len) +{ + uint32 n = 0; + int i; + for (i=0; i<len; i++) { + n = ((n << 5) + n) ^ (uint32)(p[i]); + } + return n; +} + +static int db_locked_file_destr(struct db_locked_file *data) +{ + if (data->parent != NULL) { + data->parent->locked_record = NULL; + } + + if (close(data->fd) != 0) { + DEBUG(3, ("close failed: %s\n", strerror(errno))); + return -1; + } + + return 0; +} + +static NTSTATUS db_file_store(struct db_record *rec, TDB_DATA data, int flag); +static NTSTATUS db_file_delete(struct db_record *rec); + +static struct db_record *db_file_fetch_locked(struct db_context *db, + TALLOC_CTX *mem_ctx, + TDB_DATA key) +{ + struct db_file_ctx *ctx = talloc_get_type_abort(db->private_data, + struct db_file_ctx); + struct db_record *result; + struct db_locked_file *file; + struct flock fl; + SMB_STRUCT_STAT statbuf; + ssize_t nread; + int ret; + + SMB_ASSERT(ctx->locked_record == NULL); + + again: + if (!(result = talloc(mem_ctx, struct db_record))) { + DEBUG(0, ("talloc failed\n")); + return NULL; + } + + if (!(file = talloc(result, struct db_locked_file))) { + DEBUG(0, ("talloc failed\n")); + TALLOC_FREE(result); + return NULL; + } + + result->private_data = file; + result->store = db_file_store; + result->delete_rec = db_file_delete; + + 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; + } + + /* Cut to 8 bits */ + file->hash = fsh(key.dptr, key.dsize); + file->name = hex_encode_talloc(file, (unsigned char *)key.dptr, key.dsize); + if (file->name == NULL) { + DEBUG(0, ("hex_encode failed\n")); + TALLOC_FREE(result); + return NULL; + } + + file->path = talloc_asprintf(file, "%s/%2.2X/%s", ctx->dirname, + file->hash, file->name); + if (file->path == NULL) { + DEBUG(0, ("talloc_asprintf failed\n")); + TALLOC_FREE(result); + return NULL; + } + + become_root(); + file->fd = open(file->path, O_RDWR|O_CREAT, 0644); + unbecome_root(); + + if (file->fd < 0) { + DEBUG(3, ("Could not open/create %s: %s\n", + file->path, strerror(errno))); + TALLOC_FREE(result); + return NULL; + } + + talloc_set_destructor(file, db_locked_file_destr); + + fl.l_type = F_WRLCK; + fl.l_whence = SEEK_SET; + fl.l_start = 0; + fl.l_len = 1; + fl.l_pid = 0; + + do { + ret = fcntl(file->fd, F_SETLKW, &fl); + } while ((ret == -1) && (errno == EINTR)); + + if (ret == -1) { + DEBUG(3, ("Could not get lock on %s: %s\n", + file->path, strerror(errno))); + TALLOC_FREE(result); + return NULL; + } + + if (sys_fstat(file->fd, &statbuf) != 0) { + DEBUG(3, ("Could not fstat %s: %s\n", + file->path, strerror(errno))); + TALLOC_FREE(result); + return NULL; + } + + if (statbuf.st_nlink == 0) { + /* Someone has deleted it under the lock, retry */ + TALLOC_FREE(result); + goto again; + } + + result->value.dsize = 0; + result->value.dptr = NULL; + + if (statbuf.st_size != 0) { + result->value.dsize = statbuf.st_size; + result->value.dptr = talloc_array(result, uint8, + statbuf.st_size); + if (result->value.dptr == NULL) { + DEBUG(1, ("talloc failed\n")); + TALLOC_FREE(result); + return NULL; + } + + nread = read_data(file->fd, (char *)result->value.dptr, + result->value.dsize); + if (nread != result->value.dsize) { + DEBUG(3, ("read_data failed: %s\n", strerror(errno))); + TALLOC_FREE(result); + return NULL; + } + } + + ctx->locked_record = result; + file->parent = (struct db_file_ctx *)talloc_reference(file, ctx); + + return result; +} + +static NTSTATUS db_file_store_root(int fd, TDB_DATA data) +{ + if (sys_lseek(fd, 0, SEEK_SET) != 0) { + DEBUG(0, ("sys_lseek failed: %s\n", strerror(errno))); + return map_nt_error_from_unix(errno); + } + + if (write_data(fd, (char *)data.dptr, data.dsize) != data.dsize) { + DEBUG(3, ("write_data failed: %s\n", strerror(errno))); + return map_nt_error_from_unix(errno); + } + + if (sys_ftruncate(fd, data.dsize) != 0) { + DEBUG(3, ("sys_ftruncate failed: %s\n", strerror(errno))); + return map_nt_error_from_unix(errno); + } + + return NT_STATUS_OK; +} + +static NTSTATUS db_file_store(struct db_record *rec, TDB_DATA data, int flag) +{ + struct db_locked_file *file = + talloc_get_type_abort(rec->private_data, + struct db_locked_file); + NTSTATUS status; + + become_root(); + status = db_file_store_root(file->fd, data); + unbecome_root(); + + return status; +} + +static NTSTATUS db_file_delete(struct db_record *rec) +{ + struct db_locked_file *file = + talloc_get_type_abort(rec->private_data, + struct db_locked_file); + int res; + + become_root(); + res = unlink(file->path); + unbecome_root(); + + if (res == -1) { + DEBUG(3, ("unlink(%s) failed: %s\n", file->path, + strerror(errno))); + return map_nt_error_from_unix(errno); + } + + return NT_STATUS_OK; +} + +static int db_file_traverse(struct db_context *db, + int (*fn)(struct db_record *rec, + void *private_data), + void *private_data) +{ + struct db_file_ctx *ctx = talloc_get_type_abort(db->private_data, + struct db_file_ctx); + TALLOC_CTX *mem_ctx = talloc_init("traversal %s\n", ctx->dirname); + + int i; + int count = 0; + + for (i=0; i<256; i++) { + const char *dirname = talloc_asprintf(mem_ctx, "%s/%2.2X", + ctx->dirname, i); + DIR *dir; + struct dirent *dirent; + + if (dirname == NULL) { + DEBUG(0, ("talloc failed\n")); + TALLOC_FREE(mem_ctx); + return -1; + } + + dir = opendir(dirname); + if (dir == NULL) { + DEBUG(3, ("Could not open dir %s: %s\n", dirname, + strerror(errno))); + TALLOC_FREE(mem_ctx); + return -1; + } + + while ((dirent = readdir(dir)) != NULL) { + DATA_BLOB keyblob; + TDB_DATA key; + struct db_record *rec; + + if ((dirent->d_name[0] == '.') && + ((dirent->d_name[1] == '\0') || + ((dirent->d_name[1] == '.') && + (dirent->d_name[2] == '\0')))) { + continue; + } + + keyblob = strhex_to_data_blob(mem_ctx, dirent->d_name); + if (keyblob.data == NULL) { + DEBUG(5, ("strhex_to_data_blob failed\n")); + continue; + } + + key.dptr = keyblob.data; + key.dsize = keyblob.length; + + if ((ctx->locked_record != NULL) && + (key.dsize == ctx->locked_record->key.dsize) && + (memcmp(key.dptr, ctx->locked_record->key.dptr, + key.dsize) == 0)) { + count += 1; + if (fn(ctx->locked_record, + private_data) != 0) { + TALLOC_FREE(mem_ctx); + closedir(dir); + return count; + } + } + + rec = db_file_fetch_locked(db, mem_ctx, key); + if (rec == NULL) { + /* Someone might have deleted it */ + continue; + } + + if (rec->value.dptr == NULL) { + TALLOC_FREE(rec); + continue; + } + + count += 1; + + if (fn(rec, private_data) != 0) { + TALLOC_FREE(mem_ctx); + closedir(dir); + return count; + } + TALLOC_FREE(rec); + } + + closedir(dir); + } + + TALLOC_FREE(mem_ctx); + return count; +} + +struct db_context *db_open_file(TALLOC_CTX *mem_ctx, + struct messaging_context *msg_ctx, + const char *name, + int hash_size, int tdb_flags, + int open_flags, mode_t mode) +{ + struct db_context *result = NULL; + struct db_file_ctx *ctx; + + if (!(result = talloc_zero(mem_ctx, struct db_context))) { + DEBUG(0, ("talloc failed\n")); + return NULL; + } + + if (!(ctx = talloc(result, struct db_file_ctx))) { + DEBUG(0, ("talloc failed\n")); + TALLOC_FREE(result); + return NULL; + } + + result->private_data = ctx; + result->fetch_locked = db_file_fetch_locked; + result->traverse = db_file_traverse; + result->traverse_read = db_file_traverse; + result->persistent = ((tdb_flags & TDB_CLEAR_IF_FIRST) == 0); + + ctx->locked_record = NULL; + if (!(ctx->dirname = talloc_strdup(ctx, name))) { + DEBUG(0, ("talloc failed\n")); + TALLOC_FREE(result); + return NULL; + } + + if (open_flags & O_CREAT) { + int ret, i; + + mode |= (mode & S_IRUSR) ? S_IXUSR : 0; + mode |= (mode & S_IRGRP) ? S_IXGRP : 0; + mode |= (mode & S_IROTH) ? S_IXOTH : 0; + + ret = mkdir(name, mode); + if ((ret != 0) && (errno != EEXIST)) { + DEBUG(5, ("mkdir(%s,%o) failed: %s\n", name, mode, + strerror(errno))); + TALLOC_FREE(result); + return NULL; + } + + for (i=0; i<256; i++) { + char *path; + path = talloc_asprintf(result, "%s/%2.2X", name, i); + if (path == NULL) { + DEBUG(0, ("asprintf failed\n")); + TALLOC_FREE(result); + return NULL; + } + ret = mkdir(path, mode); + if ((ret != 0) && (errno != EEXIST)) { + DEBUG(5, ("mkdir(%s,%o) failed: %s\n", path, + mode, strerror(errno))); + TALLOC_FREE(result); + return NULL; + } + TALLOC_FREE(path); + } + } + + return result; +} diff --git a/source3/lib/dbwrap/dbwrap_open.c b/source3/lib/dbwrap/dbwrap_open.c index 8721b1b0f9..a5e15e305a 100644 --- a/source3/lib/dbwrap/dbwrap_open.c +++ b/source3/lib/dbwrap/dbwrap_open.c @@ -19,7 +19,7 @@ */ #include "includes.h" -#include "dbwrap.h" +#include "dbwrap/dbwrap.h" #include "dbwrap/dbwrap_private.h" #include "dbwrap/dbwrap_open.h" #include "util_tdb.h" diff --git a/source3/lib/dbwrap/dbwrap_rbt.c b/source3/lib/dbwrap/dbwrap_rbt.c new file mode 100644 index 0000000000..fa91d8cb95 --- /dev/null +++ b/source3/lib/dbwrap/dbwrap_rbt.c @@ -0,0 +1,420 @@ +/* + Unix SMB/CIFS implementation. + Database interface wrapper around red-black trees + Copyright (C) Volker Lendecke 2007, 2008 + + 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 "includes.h" +#include "dbwrap/dbwrap.h" +#include "../lib/util/rbtree.h" + +#define DBWRAP_RBT_ALIGN(_size_) (((_size_)+15)&~15) + +struct db_rbt_ctx { + struct rb_root tree; +}; + +struct db_rbt_rec { + struct db_rbt_ctx *db_ctx; + struct db_rbt_node *node; +}; + +/* The structure that ends up in the tree */ + +struct db_rbt_node { + struct rb_node rb_node; + size_t keysize, valuesize; + + /* + * key and value are appended implicitly, "data" is only here as a + * target for offsetof() + */ + + char data[1]; +}; + +/* + * Hide the ugly pointer calculations in a function + */ + +static struct db_rbt_node *db_rbt2node(struct rb_node *node) +{ + return (struct db_rbt_node *) + ((char *)node - offsetof(struct db_rbt_node, rb_node)); +} + +/* + * Compare two keys + */ + +static int db_rbt_compare(TDB_DATA a, TDB_DATA b) +{ + int res; + + res = memcmp(a.dptr, b.dptr, MIN(a.dsize, b.dsize)); + + if ((res < 0) || ((res == 0) && (a.dsize < b.dsize))) { + return -1; + } + if ((res > 0) || ((res == 0) && (a.dsize > b.dsize))) { + return 1; + } + return 0; +} + +/* + * dissect a db_rbt_node into its implicit key and value parts + */ + +static void db_rbt_parse_node(struct db_rbt_node *node, + TDB_DATA *key, TDB_DATA *value) +{ + key->dptr = ((uint8 *)node) + offsetof(struct db_rbt_node, data); + key->dsize = node->keysize; + value->dptr = key->dptr + node->keysize; + value->dsize = node->valuesize; +} + +static NTSTATUS db_rbt_store(struct db_record *rec, TDB_DATA data, int flag) +{ + struct db_rbt_rec *rec_priv = (struct db_rbt_rec *)rec->private_data; + struct db_rbt_node *node; + + struct rb_node ** p; + struct rb_node * parent; + + TDB_DATA this_key, this_val; + + if (rec_priv->node != NULL) { + + /* + * The record was around previously + */ + + db_rbt_parse_node(rec_priv->node, &this_key, &this_val); + + SMB_ASSERT(this_key.dsize == rec->key.dsize); + SMB_ASSERT(memcmp(this_key.dptr, rec->key.dptr, + this_key.dsize) == 0); + + if (this_val.dsize >= data.dsize) { + /* + * The new value fits into the old space + */ + memcpy(this_val.dptr, data.dptr, data.dsize); + rec_priv->node->valuesize = data.dsize; + return NT_STATUS_OK; + } + + /* + * We need to delete the key from the tree and start fresh, + * there's not enough space in the existing record + */ + + rb_erase(&rec_priv->node->rb_node, &rec_priv->db_ctx->tree); + + /* + * Keep the existing node around for a while: If the record + * existed before, we reference the key data in there. + */ + } + + node = (struct db_rbt_node *)talloc_size(rec_priv->db_ctx, + offsetof(struct db_rbt_node, data) + rec->key.dsize + + data.dsize); + + if (node == NULL) { + TALLOC_FREE(rec_priv->node); + return NT_STATUS_NO_MEMORY; + } + + ZERO_STRUCT(node->rb_node); + + node->keysize = rec->key.dsize; + node->valuesize = data.dsize; + + db_rbt_parse_node(node, &this_key, &this_val); + + memcpy(this_key.dptr, rec->key.dptr, node->keysize); + TALLOC_FREE(rec_priv->node); + + memcpy(this_val.dptr, data.dptr, node->valuesize); + + parent = NULL; + p = &rec_priv->db_ctx->tree.rb_node; + + while (*p) { + struct db_rbt_node *r; + TDB_DATA search_key, search_val; + int res; + + parent = (*p); + + r = db_rbt2node(*p); + + db_rbt_parse_node(r, &search_key, &search_val); + + res = db_rbt_compare(this_key, search_key); + + if (res == -1) { + p = &(*p)->rb_left; + } + else if (res == 1) { + p = &(*p)->rb_right; + } + else { + smb_panic("someone messed with the tree"); + } + } + + rb_link_node(&node->rb_node, parent, p); + rb_insert_color(&node->rb_node, &rec_priv->db_ctx->tree); + + return NT_STATUS_OK; +} + +static NTSTATUS db_rbt_delete(struct db_record *rec) +{ + struct db_rbt_rec *rec_priv = (struct db_rbt_rec *)rec->private_data; + + if (rec_priv->node == NULL) { + return NT_STATUS_OK; + } + + rb_erase(&rec_priv->node->rb_node, &rec_priv->db_ctx->tree); + TALLOC_FREE(rec_priv->node); + + return NT_STATUS_OK; +} + +static struct db_record *db_rbt_fetch_locked(struct db_context *db_ctx, + TALLOC_CTX *mem_ctx, + TDB_DATA key) +{ + struct db_rbt_ctx *ctx = talloc_get_type_abort( + db_ctx->private_data, struct db_rbt_ctx); + + struct db_rbt_rec *rec_priv; + struct db_record *result; + struct rb_node *n; + size_t size; + bool found = false; + struct db_rbt_node *r = NULL; + TDB_DATA search_key = tdb_null, search_val = tdb_null; + + n = ctx->tree.rb_node; + + while (n != NULL) { + int res; + + r = db_rbt2node(n); + + db_rbt_parse_node(r, &search_key, &search_val); + + res = db_rbt_compare(key, search_key); + + if (res == -1) { + n = n->rb_left; + } + else if (res == 1) { + n = n->rb_right; + } + else { + found = true; + break; + } + } + + /* + * In this low-level routine, play tricks to reduce the number of + * tallocs to one. Not recommened for general use, but here it pays + * off. + */ + + size = DBWRAP_RBT_ALIGN(sizeof(struct db_record)) + + sizeof(struct db_rbt_rec); + + if (!found) { + /* + * We need to keep the key around for later store + */ + size += key.dsize; + } + + result = (struct db_record *)talloc_size(mem_ctx, size); + if (result == NULL) { + return NULL; + } + + rec_priv = (struct db_rbt_rec *) + ((char *)result + DBWRAP_RBT_ALIGN(sizeof(struct db_record))); + rec_priv->db_ctx = ctx; + + result->store = db_rbt_store; + result->delete_rec = db_rbt_delete; + result->private_data = rec_priv; + + if (found) { + rec_priv->node = r; + result->key = search_key; + result->value = search_val; + } + else { + rec_priv->node = NULL; + result->key.dptr = (uint8 *) + ((char *)rec_priv + sizeof(*rec_priv)); + result->key.dsize = key.dsize; + memcpy(result->key.dptr, key.dptr, key.dsize); + + result->value = tdb_null; + } + + return result; +} + +static int db_rbt_fetch(struct db_context *db, TALLOC_CTX *mem_ctx, + TDB_DATA key, TDB_DATA *data) +{ + struct db_rbt_ctx *ctx = talloc_get_type_abort( + db->private_data, struct db_rbt_ctx); + + struct rb_node *n; + bool found = false; + struct db_rbt_node *r = NULL; + TDB_DATA search_key, search_val; + uint8_t *result; + + n = ctx->tree.rb_node; + + while (n != NULL) { + int res; + + r = db_rbt2node(n); + + db_rbt_parse_node(r, &search_key, &search_val); + + res = db_rbt_compare(key, search_key); + + if (res == -1) { + n = n->rb_left; + } + else if (res == 1) { + n = n->rb_right; + } + else { + found = true; + break; + } + } + + if (!found) { + *data = tdb_null; + return 0; + } + + result = (uint8 *)talloc_memdup(mem_ctx, search_val.dptr, + search_val.dsize); + if (result == NULL) { + return -1; + } + + data->dptr = result; + data->dsize = search_val.dsize; + return 0; +} + +static int db_rbt_traverse_internal(struct rb_node *n, + int (*f)(struct db_record *db, + void *private_data), + void *private_data) +{ + struct db_rbt_node *r; + struct db_record rec; + int ret; + + if (n == NULL) { + return 0; + } + + ret = db_rbt_traverse_internal(n->rb_left, f, private_data); + if (ret != 0) { + return ret; + } + + r = db_rbt2node(n); + ZERO_STRUCT(rec); + db_rbt_parse_node(r, &rec.key, &rec.value); + + ret = f(&rec, private_data); + if (ret != 0) { + return ret; + } + + return db_rbt_traverse_internal(n->rb_right, f, private_data); +} + +static int db_rbt_traverse(struct db_context *db, + int (*f)(struct db_record *db, + void *private_data), + void *private_data) +{ + struct db_rbt_ctx *ctx = talloc_get_type_abort( + db->private_data, struct db_rbt_ctx); + + return db_rbt_traverse_internal(ctx->tree.rb_node, f, private_data); +} + +static int db_rbt_get_seqnum(struct db_context *db) +{ + return 0; +} + +static int db_rbt_trans_dummy(struct db_context *db) +{ + /* + * Transactions are pretty pointless in-memory, just return success. + */ + return 0; +} + +struct db_context *db_open_rbt(TALLOC_CTX *mem_ctx) +{ + struct db_context *result; + + result = talloc(mem_ctx, struct db_context); + + if (result == NULL) { + return NULL; + } + + result->private_data = talloc_zero(result, struct db_rbt_ctx); + + if (result->private_data == NULL) { + TALLOC_FREE(result); + return NULL; + } + + result->fetch_locked = db_rbt_fetch_locked; + result->fetch = db_rbt_fetch; + result->traverse = db_rbt_traverse; + result->traverse_read = db_rbt_traverse; + result->get_seqnum = db_rbt_get_seqnum; + result->transaction_start = db_rbt_trans_dummy; + result->transaction_commit = db_rbt_trans_dummy; + result->transaction_cancel = db_rbt_trans_dummy; + + return result; +} diff --git a/source3/lib/dbwrap/dbwrap_tdb.c b/source3/lib/dbwrap/dbwrap_tdb.c new file mode 100644 index 0000000000..aca57b63ce --- /dev/null +++ b/source3/lib/dbwrap/dbwrap_tdb.c @@ -0,0 +1,379 @@ +/* + Unix SMB/CIFS implementation. + Database interface wrapper around tdb + Copyright (C) Volker Lendecke 2005-2007 + + 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 "includes.h" +#include "dbwrap/dbwrap.h" +#include "lib/util/tdb_wrap.h" + +struct db_tdb_ctx { + struct tdb_wrap *wtdb; +}; + +static NTSTATUS db_tdb_store(struct db_record *rec, TDB_DATA data, int flag); +static NTSTATUS db_tdb_delete(struct db_record *rec); + +static int db_tdb_record_destr(struct db_record* data) +{ + struct db_tdb_ctx *ctx = + talloc_get_type_abort(data->private_data, struct db_tdb_ctx); + + /* This hex_encode_talloc() call allocates memory on data context. By way how current + __talloc_free() code works, it is OK to allocate in the destructor as + the children of data will be freed after call to the destructor and this + new 'child' will be caught and freed correctly. + */ + DEBUG(10, (DEBUGLEVEL > 10 + ? "Unlocking key %s\n" : "Unlocking key %.20s\n", + hex_encode_talloc(data, (unsigned char *)data->key.dptr, + data->key.dsize))); + + tdb_chainunlock(ctx->wtdb->tdb, data->key); + return 0; +} + +struct tdb_fetch_locked_state { + TALLOC_CTX *mem_ctx; + struct db_record *result; +}; + +static int db_tdb_fetchlock_parse(TDB_DATA key, TDB_DATA data, + void *private_data) +{ + struct tdb_fetch_locked_state *state = + (struct tdb_fetch_locked_state *)private_data; + + state->result = (struct db_record *)talloc_size( + state->mem_ctx, + sizeof(struct db_record) + key.dsize + data.dsize); + + if (state->result == NULL) { + return 0; + } + + state->result->key.dsize = key.dsize; + state->result->key.dptr = ((uint8 *)state->result) + + sizeof(struct db_record); + memcpy(state->result->key.dptr, key.dptr, key.dsize); + + state->result->value.dsize = data.dsize; + + if (data.dsize > 0) { + state->result->value.dptr = state->result->key.dptr+key.dsize; + memcpy(state->result->value.dptr, data.dptr, data.dsize); + } + else { + state->result->value.dptr = NULL; + } + + return 0; +} + +static struct db_record *db_tdb_fetch_locked(struct db_context *db, + TALLOC_CTX *mem_ctx, TDB_DATA key) +{ + struct db_tdb_ctx *ctx = talloc_get_type_abort(db->private_data, + struct db_tdb_ctx); + struct tdb_fetch_locked_state state; + + /* Do not accidently allocate/deallocate w/o need when debug level is lower than needed */ + if(DEBUGLEVEL >= 10) { + char *keystr = hex_encode_talloc(talloc_tos(), (unsigned char*)key.dptr, key.dsize); + DEBUG(10, (DEBUGLEVEL > 10 + ? "Locking key %s\n" : "Locking key %.20s\n", + keystr)); + TALLOC_FREE(keystr); + } + + if (tdb_chainlock(ctx->wtdb->tdb, key) != 0) { + DEBUG(3, ("tdb_chainlock failed\n")); + return NULL; + } + + state.mem_ctx = mem_ctx; + state.result = NULL; + + tdb_parse_record(ctx->wtdb->tdb, key, db_tdb_fetchlock_parse, &state); + + if (state.result == NULL) { + db_tdb_fetchlock_parse(key, tdb_null, &state); + } + + if (state.result == NULL) { + tdb_chainunlock(ctx->wtdb->tdb, key); + return NULL; + } + + talloc_set_destructor(state.result, db_tdb_record_destr); + + state.result->private_data = talloc_reference(state.result, ctx); + state.result->store = db_tdb_store; + state.result->delete_rec = db_tdb_delete; + + DEBUG(10, ("Allocated locked data 0x%p\n", state.result)); + + return state.result; +} + +struct tdb_fetch_state { + TALLOC_CTX *mem_ctx; + int result; + TDB_DATA data; +}; + +static int db_tdb_fetch_parse(TDB_DATA key, TDB_DATA data, + void *private_data) +{ + struct tdb_fetch_state *state = + (struct tdb_fetch_state *)private_data; + + state->data.dptr = (uint8 *)talloc_memdup(state->mem_ctx, data.dptr, + data.dsize); + if (state->data.dptr == NULL) { + state->result = -1; + return 0; + } + + state->data.dsize = data.dsize; + return 0; +} + +static int db_tdb_fetch(struct db_context *db, TALLOC_CTX *mem_ctx, + TDB_DATA key, TDB_DATA *pdata) +{ + struct db_tdb_ctx *ctx = talloc_get_type_abort( + db->private_data, struct db_tdb_ctx); + + struct tdb_fetch_state state; + + state.mem_ctx = mem_ctx; + state.result = 0; + state.data = tdb_null; + + tdb_parse_record(ctx->wtdb->tdb, key, db_tdb_fetch_parse, &state); + + if (state.result == -1) { + return -1; + } + + *pdata = state.data; + return 0; +} + +static int db_tdb_parse(struct db_context *db, TDB_DATA key, + int (*parser)(TDB_DATA key, TDB_DATA data, + void *private_data), + void *private_data) +{ + struct db_tdb_ctx *ctx = talloc_get_type_abort( + db->private_data, struct db_tdb_ctx); + + return tdb_parse_record(ctx->wtdb->tdb, key, parser, private_data); +} + +static NTSTATUS db_tdb_store(struct db_record *rec, TDB_DATA data, int flag) +{ + struct db_tdb_ctx *ctx = talloc_get_type_abort(rec->private_data, + struct db_tdb_ctx); + + /* + * This has a bug: We need to replace rec->value for correct + * operation, but right now brlock and locking don't use the value + * anymore after it was stored. + */ + + return (tdb_store(ctx->wtdb->tdb, rec->key, data, flag) == 0) ? + NT_STATUS_OK : NT_STATUS_UNSUCCESSFUL; +} + +static NTSTATUS db_tdb_delete(struct db_record *rec) +{ + struct db_tdb_ctx *ctx = talloc_get_type_abort(rec->private_data, + struct db_tdb_ctx); + + if (tdb_delete(ctx->wtdb->tdb, rec->key) == 0) { + return NT_STATUS_OK; + } + + if (tdb_error(ctx->wtdb->tdb) == TDB_ERR_NOEXIST) { + return NT_STATUS_NOT_FOUND; + } + + return NT_STATUS_UNSUCCESSFUL; +} + +struct db_tdb_traverse_ctx { + struct db_context *db; + int (*f)(struct db_record *rec, void *private_data); + void *private_data; +}; + +static int db_tdb_traverse_func(TDB_CONTEXT *tdb, TDB_DATA kbuf, TDB_DATA dbuf, + void *private_data) +{ + struct db_tdb_traverse_ctx *ctx = + (struct db_tdb_traverse_ctx *)private_data; + struct db_record rec; + + rec.key = kbuf; + rec.value = dbuf; + rec.store = db_tdb_store; + rec.delete_rec = db_tdb_delete; + rec.private_data = ctx->db->private_data; + + return ctx->f(&rec, ctx->private_data); +} + +static int db_tdb_traverse(struct db_context *db, + int (*f)(struct db_record *rec, void *private_data), + void *private_data) +{ + struct db_tdb_ctx *db_ctx = + talloc_get_type_abort(db->private_data, struct db_tdb_ctx); + struct db_tdb_traverse_ctx ctx; + + ctx.db = db; + ctx.f = f; + ctx.private_data = private_data; + return tdb_traverse(db_ctx->wtdb->tdb, db_tdb_traverse_func, &ctx); +} + +static NTSTATUS db_tdb_store_deny(struct db_record *rec, TDB_DATA data, int flag) +{ + return NT_STATUS_MEDIA_WRITE_PROTECTED; +} + +static NTSTATUS db_tdb_delete_deny(struct db_record *rec) +{ + return NT_STATUS_MEDIA_WRITE_PROTECTED; +} + +static int db_tdb_traverse_read_func(TDB_CONTEXT *tdb, TDB_DATA kbuf, TDB_DATA dbuf, + void *private_data) +{ + struct db_tdb_traverse_ctx *ctx = + (struct db_tdb_traverse_ctx *)private_data; + struct db_record rec; + + rec.key = kbuf; + rec.value = dbuf; + rec.store = db_tdb_store_deny; + rec.delete_rec = db_tdb_delete_deny; + rec.private_data = ctx->db->private_data; + + return ctx->f(&rec, ctx->private_data); +} + +static int db_tdb_traverse_read(struct db_context *db, + int (*f)(struct db_record *rec, void *private_data), + void *private_data) +{ + struct db_tdb_ctx *db_ctx = + talloc_get_type_abort(db->private_data, struct db_tdb_ctx); + struct db_tdb_traverse_ctx ctx; + + ctx.db = db; + ctx.f = f; + ctx.private_data = private_data; + return tdb_traverse_read(db_ctx->wtdb->tdb, db_tdb_traverse_read_func, &ctx); +} + +static int db_tdb_get_seqnum(struct db_context *db) + +{ + struct db_tdb_ctx *db_ctx = + talloc_get_type_abort(db->private_data, struct db_tdb_ctx); + return tdb_get_seqnum(db_ctx->wtdb->tdb); +} + +static int db_tdb_get_flags(struct db_context *db) + +{ + struct db_tdb_ctx *db_ctx = + talloc_get_type_abort(db->private_data, struct db_tdb_ctx); + return tdb_get_flags(db_ctx->wtdb->tdb); +} + +static int db_tdb_transaction_start(struct db_context *db) +{ + struct db_tdb_ctx *db_ctx = + talloc_get_type_abort(db->private_data, struct db_tdb_ctx); + return tdb_transaction_start(db_ctx->wtdb->tdb); +} + +static int db_tdb_transaction_commit(struct db_context *db) +{ + struct db_tdb_ctx *db_ctx = + talloc_get_type_abort(db->private_data, struct db_tdb_ctx); + return tdb_transaction_commit(db_ctx->wtdb->tdb); +} + +static int db_tdb_transaction_cancel(struct db_context *db) +{ + struct db_tdb_ctx *db_ctx = + talloc_get_type_abort(db->private_data, struct db_tdb_ctx); + tdb_transaction_cancel(db_ctx->wtdb->tdb); + return 0; +} + +struct db_context *db_open_tdb(TALLOC_CTX *mem_ctx, + const char *name, + int hash_size, int tdb_flags, + int open_flags, mode_t mode) +{ + struct db_context *result = NULL; + struct db_tdb_ctx *db_tdb; + + result = talloc_zero(mem_ctx, struct db_context); + if (result == NULL) { + DEBUG(0, ("talloc failed\n")); + goto fail; + } + + result->private_data = db_tdb = talloc(result, struct db_tdb_ctx); + if (db_tdb == NULL) { + DEBUG(0, ("talloc failed\n")); + goto fail; + } + + db_tdb->wtdb = tdb_wrap_open(db_tdb, name, hash_size, tdb_flags, + open_flags, mode); + if (db_tdb->wtdb == NULL) { + DEBUG(3, ("Could not open tdb: %s\n", strerror(errno))); + goto fail; + } + + result->fetch_locked = db_tdb_fetch_locked; + result->fetch = db_tdb_fetch; + result->traverse = db_tdb_traverse; + result->traverse_read = db_tdb_traverse_read; + result->parse_record = db_tdb_parse; + result->get_seqnum = db_tdb_get_seqnum; + result->get_flags = db_tdb_get_flags; + result->persistent = ((tdb_flags & TDB_CLEAR_IF_FIRST) == 0); + result->transaction_start = db_tdb_transaction_start; + result->transaction_commit = db_tdb_transaction_commit; + result->transaction_cancel = db_tdb_transaction_cancel; + return result; + + fail: + if (result != NULL) { + TALLOC_FREE(result); + } + return NULL; +} diff --git a/source3/lib/dbwrap/dbwrap_util.c b/source3/lib/dbwrap/dbwrap_util.c new file mode 100644 index 0000000000..effcf40c6b --- /dev/null +++ b/source3/lib/dbwrap/dbwrap_util.c @@ -0,0 +1,569 @@ +/* + Unix SMB/CIFS implementation. + Utility functions for the dbwrap API + Copyright (C) Volker Lendecke 2007 + Copyright (C) Michael Adam 2009 + Copyright (C) Jim McDonough <jmcd@us.ibm.com> 2006 + + Major code contributions from Aleksey Fedoseev (fedoseev@ru.ibm.com) + + 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 2 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, write to the Free Software + Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. +*/ + +#include "includes.h" +#include "dbwrap.h" +#include "util_tdb.h" + +int32_t dbwrap_fetch_int32(struct db_context *db, const char *keystr) +{ + TDB_DATA dbuf; + int32 ret; + + if (db->fetch(db, NULL, string_term_tdb_data(keystr), &dbuf) != 0) { + return -1; + } + + if ((dbuf.dptr == NULL) || (dbuf.dsize != sizeof(int32_t))) { + TALLOC_FREE(dbuf.dptr); + return -1; + } + + ret = IVAL(dbuf.dptr, 0); + TALLOC_FREE(dbuf.dptr); + return ret; +} + +int dbwrap_store_int32(struct db_context *db, const char *keystr, int32_t v) +{ + struct db_record *rec; + int32 v_store; + NTSTATUS status; + + rec = db->fetch_locked(db, NULL, string_term_tdb_data(keystr)); + if (rec == NULL) { + return -1; + } + + SIVAL(&v_store, 0, v); + + status = rec->store(rec, make_tdb_data((const uint8 *)&v_store, + sizeof(v_store)), + TDB_REPLACE); + TALLOC_FREE(rec); + return NT_STATUS_IS_OK(status) ? 0 : -1; +} + +bool dbwrap_fetch_uint32(struct db_context *db, const char *keystr, + uint32_t *val) +{ + TDB_DATA dbuf; + + if (db->fetch(db, NULL, string_term_tdb_data(keystr), &dbuf) != 0) { + return false; + } + + if ((dbuf.dptr == NULL) || (dbuf.dsize != sizeof(uint32_t))) { + TALLOC_FREE(dbuf.dptr); + return false; + } + + *val = IVAL(dbuf.dptr, 0); + TALLOC_FREE(dbuf.dptr); + return true; +} + +int dbwrap_store_uint32(struct db_context *db, const char *keystr, uint32_t v) +{ + struct db_record *rec; + uint32 v_store; + NTSTATUS status; + + rec = db->fetch_locked(db, NULL, string_term_tdb_data(keystr)); + if (rec == NULL) { + return -1; + } + + SIVAL(&v_store, 0, v); + + status = rec->store(rec, make_tdb_data((const uint8 *)&v_store, + sizeof(v_store)), + TDB_REPLACE); + TALLOC_FREE(rec); + return NT_STATUS_IS_OK(status) ? 0 : -1; +} + +/** + * Atomic unsigned integer change (addition): + * + * if value does not exist yet in the db, use *oldval as initial old value. + * return old value in *oldval. + * store *oldval + change_val to db. + */ + +struct dbwrap_change_uint32_atomic_context { + const char *keystr; + uint32_t *oldval; + uint32_t change_val; +}; + +static NTSTATUS dbwrap_change_uint32_atomic_action(struct db_context *db, + void *private_data) +{ + struct db_record *rec; + uint32_t val = (uint32_t)-1; + uint32_t v_store; + NTSTATUS ret; + struct dbwrap_change_uint32_atomic_context *state; + + state = (struct dbwrap_change_uint32_atomic_context *)private_data; + + rec = db->fetch_locked(db, NULL, string_term_tdb_data(state->keystr)); + if (!rec) { + return NT_STATUS_UNSUCCESSFUL; + } + + if (rec->value.dptr == NULL) { + val = *(state->oldval); + } else if (rec->value.dsize == sizeof(val)) { + val = IVAL(rec->value.dptr, 0); + *(state->oldval) = val; + } else { + ret = NT_STATUS_UNSUCCESSFUL; + goto done; + } + + val += state->change_val; + + SIVAL(&v_store, 0, val); + + ret = rec->store(rec, + make_tdb_data((const uint8 *)&v_store, + sizeof(v_store)), + TDB_REPLACE); + +done: + TALLOC_FREE(rec); + return ret; +} + +NTSTATUS dbwrap_change_uint32_atomic(struct db_context *db, const char *keystr, + uint32_t *oldval, uint32_t change_val) +{ + NTSTATUS ret; + struct dbwrap_change_uint32_atomic_context state; + + state.keystr = keystr; + state.oldval = oldval; + state.change_val = change_val; + + ret = dbwrap_change_uint32_atomic_action(db, &state); + + return ret; +} + +NTSTATUS dbwrap_trans_change_uint32_atomic(struct db_context *db, + const char *keystr, + uint32_t *oldval, + uint32_t change_val) +{ + NTSTATUS ret; + struct dbwrap_change_uint32_atomic_context state; + + state.keystr = keystr; + state.oldval = oldval; + state.change_val = change_val; + + ret = dbwrap_trans_do(db, dbwrap_change_uint32_atomic_action, &state); + + return ret; +} + +/** + * Atomic integer change (addition): + * + * if value does not exist yet in the db, use *oldval as initial old value. + * return old value in *oldval. + * store *oldval + change_val to db. + */ + +struct dbwrap_change_int32_atomic_context { + const char *keystr; + int32_t *oldval; + int32_t change_val; +}; + +static NTSTATUS dbwrap_change_int32_atomic_action(struct db_context *db, + void *private_data) +{ + struct db_record *rec; + int32_t val = -1; + int32_t v_store; + NTSTATUS ret; + struct dbwrap_change_int32_atomic_context *state; + + state = (struct dbwrap_change_int32_atomic_context *)private_data; + + rec = db->fetch_locked(db, NULL, string_term_tdb_data(state->keystr)); + if (!rec) { + return NT_STATUS_UNSUCCESSFUL; + } + + if (rec->value.dptr == NULL) { + val = *(state->oldval); + } else if (rec->value.dsize == sizeof(val)) { + val = IVAL(rec->value.dptr, 0); + *(state->oldval) = val; + } else { + ret = NT_STATUS_UNSUCCESSFUL; + goto done; + } + + val += state->change_val; + + SIVAL(&v_store, 0, val); + + ret = rec->store(rec, + make_tdb_data((const uint8_t *)&v_store, + sizeof(v_store)), + TDB_REPLACE); + +done: + TALLOC_FREE(rec); + return ret; +} + +NTSTATUS dbwrap_change_int32_atomic(struct db_context *db, const char *keystr, + int32_t *oldval, int32_t change_val) +{ + NTSTATUS ret; + struct dbwrap_change_int32_atomic_context state; + + state.keystr = keystr; + state.oldval = oldval; + state.change_val = change_val; + + ret = dbwrap_change_int32_atomic_action(db, &state); + + return ret; +} + +NTSTATUS dbwrap_trans_change_int32_atomic(struct db_context *db, + const char *keystr, + int32_t *oldval, + int32_t change_val) +{ + NTSTATUS ret; + struct dbwrap_change_int32_atomic_context state; + + state.keystr = keystr; + state.oldval = oldval; + state.change_val = change_val; + + ret = dbwrap_trans_do(db, dbwrap_change_int32_atomic_action, &state); + + return ret; +} + +struct dbwrap_store_context { + TDB_DATA *key; + TDB_DATA *dbuf; + int flag; +}; + +static NTSTATUS dbwrap_store_action(struct db_context *db, void *private_data) +{ + struct db_record *rec = NULL; + NTSTATUS status; + struct dbwrap_store_context *store_ctx; + + store_ctx = (struct dbwrap_store_context *)private_data; + + rec = db->fetch_locked(db, talloc_tos(), *(store_ctx->key)); + if (rec == NULL) { + DEBUG(5, ("fetch_locked failed\n")); + return NT_STATUS_NO_MEMORY; + } + + status = rec->store(rec, *(store_ctx->dbuf), store_ctx->flag); + if (!NT_STATUS_IS_OK(status)) { + DEBUG(5, ("store returned %s\n", nt_errstr(status))); + } + + TALLOC_FREE(rec); + return status; +} + +NTSTATUS dbwrap_trans_store(struct db_context *db, TDB_DATA key, TDB_DATA dbuf, + int flag) +{ + NTSTATUS status; + struct dbwrap_store_context store_ctx; + + store_ctx.key = &key; + store_ctx.dbuf = &dbuf; + store_ctx.flag = flag; + + status = dbwrap_trans_do(db, dbwrap_store_action, &store_ctx); + + return status; +} + +static NTSTATUS dbwrap_delete_action(struct db_context * db, void *private_data) +{ + NTSTATUS status; + struct db_record *rec; + TDB_DATA *key = (TDB_DATA *)private_data; + + rec = db->fetch_locked(db, talloc_tos(), *key); + if (rec == NULL) { + DEBUG(5, ("fetch_locked failed\n")); + return NT_STATUS_NO_MEMORY; + } + + status = rec->delete_rec(rec); + if (!NT_STATUS_IS_OK(status)) { + DEBUG(5, ("delete_rec returned %s\n", nt_errstr(status))); + } + + talloc_free(rec); + return status; +} + +NTSTATUS dbwrap_trans_delete(struct db_context *db, TDB_DATA key) +{ + NTSTATUS status; + + status = dbwrap_trans_do(db, dbwrap_delete_action, &key); + + return status; +} + +NTSTATUS dbwrap_trans_store_int32(struct db_context *db, const char *keystr, + int32_t v) +{ + int32 v_store; + + SIVAL(&v_store, 0, v); + + return dbwrap_trans_store(db, string_term_tdb_data(keystr), + make_tdb_data((const uint8 *)&v_store, + sizeof(v_store)), + TDB_REPLACE); +} + +NTSTATUS dbwrap_trans_store_uint32(struct db_context *db, const char *keystr, + uint32_t v) +{ + uint32 v_store; + + SIVAL(&v_store, 0, v); + + return dbwrap_trans_store(db, string_term_tdb_data(keystr), + make_tdb_data((const uint8 *)&v_store, + sizeof(v_store)), + TDB_REPLACE); +} + +NTSTATUS dbwrap_trans_store_bystring(struct db_context *db, const char *key, + TDB_DATA data, int flags) +{ + return dbwrap_trans_store(db, string_term_tdb_data(key), data, flags); +} + +NTSTATUS dbwrap_trans_delete_bystring(struct db_context *db, const char *key) +{ + return dbwrap_trans_delete(db, string_term_tdb_data(key)); +} + +/** + * Wrap db action(s) into a transaction. + */ +NTSTATUS dbwrap_trans_do(struct db_context *db, + NTSTATUS (*action)(struct db_context *, void *), + void *private_data) +{ + int res; + NTSTATUS status; + + res = db->transaction_start(db); + if (res != 0) { + DEBUG(5, ("transaction_start failed\n")); + return NT_STATUS_INTERNAL_DB_CORRUPTION; + } + + status = action(db, private_data); + if (!NT_STATUS_IS_OK(status)) { + if (db->transaction_cancel(db) != 0) { + smb_panic("Cancelling transaction failed"); + } + return status; + } + + res = db->transaction_commit(db); + if (res == 0) { + return NT_STATUS_OK; + } + + DEBUG(2, ("transaction_commit failed\n")); + return NT_STATUS_INTERNAL_DB_CORRUPTION; +} + +struct dbwrap_trans_traverse_action_ctx { + int (*f)(struct db_record* rec, void* private_data); + void* private_data; +}; + + +static NTSTATUS dbwrap_trans_traverse_action(struct db_context* db, void* private_data) +{ + struct dbwrap_trans_traverse_action_ctx* ctx = + (struct dbwrap_trans_traverse_action_ctx*)private_data; + + int ret = db->traverse(db, ctx->f, ctx->private_data); + + return (ret < 0) ? NT_STATUS_INTERNAL_DB_CORRUPTION : NT_STATUS_OK; +} + +NTSTATUS dbwrap_trans_traverse(struct db_context *db, + int (*f)(struct db_record*, void*), + void *private_data) +{ + struct dbwrap_trans_traverse_action_ctx ctx = { + .f = f, + .private_data = private_data, + }; + return dbwrap_trans_do(db, dbwrap_trans_traverse_action, &ctx); +} + +NTSTATUS dbwrap_traverse(struct db_context *db, + int (*f)(struct db_record*, void*), + void *private_data) +{ + int ret = db->traverse(db, f, private_data); + return (ret < 0) ? NT_STATUS_INTERNAL_DB_CORRUPTION : NT_STATUS_OK; +} + + + +NTSTATUS dbwrap_delete(struct db_context *db, TDB_DATA key) +{ + struct db_record *rec; + NTSTATUS status; + + rec = db->fetch_locked(db, talloc_tos(), key); + if (rec == NULL) { + return NT_STATUS_NO_MEMORY; + } + status = rec->delete_rec(rec); + TALLOC_FREE(rec); + return status; +} + +NTSTATUS dbwrap_store(struct db_context *db, TDB_DATA key, + TDB_DATA data, int flags) +{ + struct db_record *rec; + NTSTATUS status; + + rec = db->fetch_locked(db, talloc_tos(), key); + if (rec == NULL) { + return NT_STATUS_NO_MEMORY; + } + + status = rec->store(rec, data, flags); + TALLOC_FREE(rec); + return status; +} + +TDB_DATA dbwrap_fetch(struct db_context *db, TALLOC_CTX *mem_ctx, + TDB_DATA key) +{ + TDB_DATA result; + + if (db->fetch(db, mem_ctx, key, &result) != 0) { + return make_tdb_data(NULL, 0); + } + + return result; +} + +NTSTATUS dbwrap_delete_bystring(struct db_context *db, const char *key) +{ + return dbwrap_delete(db, string_term_tdb_data(key)); +} + +NTSTATUS dbwrap_store_bystring(struct db_context *db, const char *key, + TDB_DATA data, int flags) +{ + return dbwrap_store(db, string_term_tdb_data(key), data, flags); +} + +TDB_DATA dbwrap_fetch_bystring(struct db_context *db, TALLOC_CTX *mem_ctx, + const char *key) +{ + return dbwrap_fetch(db, mem_ctx, string_term_tdb_data(key)); +} + + + +NTSTATUS dbwrap_delete_bystring_upper(struct db_context *db, const char *key) +{ + char *key_upper; + NTSTATUS status; + + key_upper = talloc_strdup_upper(talloc_tos(), key); + if (key_upper == NULL) { + return NT_STATUS_NO_MEMORY; + } + + status = dbwrap_delete_bystring(db, key_upper); + + talloc_free(key_upper); + return status; +} + +NTSTATUS dbwrap_store_bystring_upper(struct db_context *db, const char *key, + TDB_DATA data, int flags) +{ + char *key_upper; + NTSTATUS status; + + key_upper = talloc_strdup_upper(talloc_tos(), key); + if (key_upper == NULL) { + return NT_STATUS_NO_MEMORY; + } + + status = dbwrap_store_bystring(db, key_upper, data, flags); + + talloc_free(key_upper); + return status; +} + +TDB_DATA dbwrap_fetch_bystring_upper(struct db_context *db, TALLOC_CTX *mem_ctx, + const char *key) +{ + char *key_upper; + TDB_DATA result; + + key_upper = talloc_strdup_upper(talloc_tos(), key); + if (key_upper == NULL) { + return make_tdb_data(NULL, 0); + } + + result = dbwrap_fetch_bystring(db, mem_ctx, key_upper); + + talloc_free(key_upper); + return result; +} |