From 4c185a6077322ba64231d23dda25a6b6d44d0b12 Mon Sep 17 00:00:00 2001 From: Volker Lendecke Date: Thu, 10 May 2007 10:42:13 +0000 Subject: r22775: For the cluster code I've developed a wrapper around tdb to put different database backends in place dynamically. The main abstractions are db_context and db_record, it should be mainly self-describing, see include/dbwrap.h. You open the db just as you would open a tdb, this time with db_open(). If you want to fetch a record, just do the db->fetch() call, if you want to do operations on it, you need to get it with fetch_locked(). I added dbwrap_file.c (not heavily tested lately) as an example for what can be done with that abstraction, uses a file per key. So if anybody is willing to shape that up, we might have a chance on reiserfs again.... :-) This abstraction works fine for brlock.tdb, locking.tdb, connections.tdb and sessionid.tdb. It should work fine for the others as well, I just did not yet get around to convert them. If nobody loudly screams NO, then I will import the code that uses this soon. Volker (This used to be commit e9d7484ca246cfca4a1fd23be35edc2783136ebe) --- source3/lib/dbwrap_tdb.c | 227 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 227 insertions(+) create mode 100644 source3/lib/dbwrap_tdb.c (limited to 'source3/lib/dbwrap_tdb.c') diff --git a/source3/lib/dbwrap_tdb.c b/source3/lib/dbwrap_tdb.c new file mode 100644 index 0000000000..238ba51761 --- /dev/null +++ b/source3/lib/dbwrap_tdb.c @@ -0,0 +1,227 @@ +/* + 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 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" + +struct db_tdb_ctx { + TDB_CONTEXT *tdb; +}; + +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); + + DEBUG(10, ("Unlocking key %s\n", + hex_encode(data, (unsigned char *)data->key.dptr, + data->key.dsize))); + + if (tdb_chainunlock(ctx->tdb, data->key) != 0) { + DEBUG(0, ("tdb_chainunlock failed\n")); + return -1; + } + 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 db_record *result; + TDB_DATA value; + + result = TALLOC_P(mem_ctx, struct db_record); + if (result == NULL) { + DEBUG(0, ("talloc failed\n")); + return NULL; + } + + 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->value.dptr = NULL; + result->value.dsize = 0; + result->private_data = talloc_reference(result, ctx); + result->store = db_tdb_store; + result->delete_rec = db_tdb_delete; + + { + char *keystr = hex_encode(NULL, (unsigned char *)key.dptr, + key.dsize); + DEBUG(10, ("Locking key %s\n", keystr)); + TALLOC_FREE(keystr); + } + + if (tdb_chainlock(ctx->tdb, key) != 0) { + DEBUG(3, ("tdb_chainlock failed\n")); + TALLOC_FREE(result); + return NULL; + } + + talloc_set_destructor(result, db_tdb_record_destr); + + value = tdb_fetch(ctx->tdb, key); + + if (value.dptr == NULL) { + return result; + } + + result->value.dsize = value.dsize; + result->value.dptr = (uint8 *)talloc_memdup(result, value.dptr, + value.dsize); + if (result->value.dptr == NULL) { + DEBUG(3, ("talloc failed\n")); + TALLOC_FREE(result); + return NULL; + } + + SAFE_FREE(value.dptr); + + DEBUG(10, ("Allocated locked data 0x%p\n", result)); + + return result; +} + +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->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); + + return (tdb_delete(ctx->tdb, rec->key) == 0) ? + NT_STATUS_OK : 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->tdb, db_tdb_traverse_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->tdb); +} + +static int db_tdb_ctx_destr(struct db_tdb_ctx *ctx) +{ + if (tdb_close(ctx->tdb) != 0) { + DEBUG(0, ("Failed to close tdb: %s\n", strerror(errno))); + return -1; + } + + 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_P(mem_ctx, struct db_context); + if (result == NULL) { + DEBUG(0, ("talloc failed\n")); + goto fail; + } + + result->private_data = db_tdb = TALLOC_P(result, struct db_tdb_ctx); + if (db_tdb == NULL) { + DEBUG(0, ("talloc failed\n")); + goto fail; + } + + db_tdb->tdb = tdb_open_log(name, hash_size, tdb_flags, + open_flags, mode); + if (db_tdb->tdb == NULL) { + DEBUG(3, ("Could not open tdb: %s\n", strerror(errno))); + goto fail; + } + + talloc_set_destructor(db_tdb, db_tdb_ctx_destr); + result->fetch_locked = db_tdb_fetch_locked; + result->traverse = db_tdb_traverse; + result->get_seqnum = db_tdb_get_seqnum; + return result; + + fail: + if (result != NULL) { + TALLOC_FREE(result); + } + return NULL; +} -- cgit From 7e27c984c4033d4f3cbeec9c18627ef9d8aa5f90 Mon Sep 17 00:00:00 2001 From: Volker Lendecke Date: Mon, 28 May 2007 11:08:58 +0000 Subject: r23170: Add map_nt_error_from_tdb() (This used to be commit 02beae81c8ecef7cfe300a29852d74813c9409bf) --- source3/lib/dbwrap_tdb.c | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) (limited to 'source3/lib/dbwrap_tdb.c') diff --git a/source3/lib/dbwrap_tdb.c b/source3/lib/dbwrap_tdb.c index 238ba51761..2b09e3ccab 100644 --- a/source3/lib/dbwrap_tdb.c +++ b/source3/lib/dbwrap_tdb.c @@ -127,9 +127,15 @@ 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); + int res; + + res = tdb_delete(ctx->tdb, rec->key); - return (tdb_delete(ctx->tdb, rec->key) == 0) ? - NT_STATUS_OK : NT_STATUS_UNSUCCESSFUL; + if (res == 0) { + return NT_STATUS_OK; + } + + return map_nt_error_from_tdb(tdb_error(ctx->tdb)); } struct db_tdb_traverse_ctx { -- cgit From a9503016859dc4bc3f68eb302c1adf37bf311187 Mon Sep 17 00:00:00 2001 From: Volker Lendecke Date: Tue, 29 May 2007 18:04:38 +0000 Subject: r23220: Add traverse_read to dbwrap (This used to be commit b38dc5ffdfe9fdc2879c57dc181815f06b4747fe) --- source3/lib/dbwrap_tdb.c | 41 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) (limited to 'source3/lib/dbwrap_tdb.c') diff --git a/source3/lib/dbwrap_tdb.c b/source3/lib/dbwrap_tdb.c index 2b09e3ccab..028bad36e6 100644 --- a/source3/lib/dbwrap_tdb.c +++ b/source3/lib/dbwrap_tdb.c @@ -174,6 +174,46 @@ static int db_tdb_traverse(struct db_context *db, return tdb_traverse(db_ctx->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->tdb, db_tdb_traverse_read_func, &ctx); +} + static int db_tdb_get_seqnum(struct db_context *db) { @@ -222,6 +262,7 @@ struct db_context *db_open_tdb(TALLOC_CTX *mem_ctx, talloc_set_destructor(db_tdb, db_tdb_ctx_destr); result->fetch_locked = db_tdb_fetch_locked; result->traverse = db_tdb_traverse; + result->traverse_read = db_tdb_traverse_read; result->get_seqnum = db_tdb_get_seqnum; return result; -- cgit From 37569f60f3af50cce034e00e0dc5af026427dbd5 Mon Sep 17 00:00:00 2001 From: Volker Lendecke Date: Sun, 3 Jun 2007 06:54:51 +0000 Subject: r23313: Janitor for tridge: we need to use tdb_wrap_open in both these backends to allow for multiple opens. This is done for notify.tdb. Otherwise we die when a 2nd share with notify is setup (This used to be commit 40dcccfcf91737ba658fd9e733a431001649d255) --- source3/lib/dbwrap_tdb.c | 37 +++++++++++++------------------------ 1 file changed, 13 insertions(+), 24 deletions(-) (limited to 'source3/lib/dbwrap_tdb.c') diff --git a/source3/lib/dbwrap_tdb.c b/source3/lib/dbwrap_tdb.c index 028bad36e6..8997f9f040 100644 --- a/source3/lib/dbwrap_tdb.c +++ b/source3/lib/dbwrap_tdb.c @@ -21,7 +21,7 @@ #include "includes.h" struct db_tdb_ctx { - TDB_CONTEXT *tdb; + struct tdb_wrap *wtdb; }; static NTSTATUS db_tdb_store(struct db_record *rec, TDB_DATA data, int flag); @@ -36,7 +36,7 @@ static int db_tdb_record_destr(struct db_record* data) hex_encode(data, (unsigned char *)data->key.dptr, data->key.dsize))); - if (tdb_chainunlock(ctx->tdb, data->key) != 0) { + if (tdb_chainunlock(ctx->wtdb->tdb, data->key) != 0) { DEBUG(0, ("tdb_chainunlock failed\n")); return -1; } @@ -78,7 +78,7 @@ static struct db_record *db_tdb_fetch_locked(struct db_context *db, TALLOC_FREE(keystr); } - if (tdb_chainlock(ctx->tdb, key) != 0) { + if (tdb_chainlock(ctx->wtdb->tdb, key) != 0) { DEBUG(3, ("tdb_chainlock failed\n")); TALLOC_FREE(result); return NULL; @@ -86,7 +86,7 @@ static struct db_record *db_tdb_fetch_locked(struct db_context *db, talloc_set_destructor(result, db_tdb_record_destr); - value = tdb_fetch(ctx->tdb, key); + value = tdb_fetch(ctx->wtdb->tdb, key); if (value.dptr == NULL) { return result; @@ -119,7 +119,7 @@ static NTSTATUS db_tdb_store(struct db_record *rec, TDB_DATA data, int flag) * anymore after it was stored. */ - return (tdb_store(ctx->tdb, rec->key, data, flag) == 0) ? + return (tdb_store(ctx->wtdb->tdb, rec->key, data, flag) == 0) ? NT_STATUS_OK : NT_STATUS_UNSUCCESSFUL; } @@ -129,13 +129,13 @@ static NTSTATUS db_tdb_delete(struct db_record *rec) struct db_tdb_ctx); int res; - res = tdb_delete(ctx->tdb, rec->key); + res = tdb_delete(ctx->wtdb->tdb, rec->key); if (res == 0) { return NT_STATUS_OK; } - return map_nt_error_from_tdb(tdb_error(ctx->tdb)); + return map_nt_error_from_tdb(tdb_error(ctx->wtdb->tdb)); } struct db_tdb_traverse_ctx { @@ -171,7 +171,7 @@ static int db_tdb_traverse(struct db_context *db, ctx.db = db; ctx.f = f; ctx.private_data = private_data; - return tdb_traverse(db_ctx->tdb, db_tdb_traverse_func, &ctx); + 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) @@ -211,7 +211,7 @@ static int db_tdb_traverse_read(struct db_context *db, ctx.db = db; ctx.f = f; ctx.private_data = private_data; - return tdb_traverse_read(db_ctx->tdb, db_tdb_traverse_read_func, &ctx); + return tdb_traverse_read(db_ctx->wtdb->tdb, db_tdb_traverse_read_func, &ctx); } static int db_tdb_get_seqnum(struct db_context *db) @@ -219,17 +219,7 @@ 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->tdb); -} - -static int db_tdb_ctx_destr(struct db_tdb_ctx *ctx) -{ - if (tdb_close(ctx->tdb) != 0) { - DEBUG(0, ("Failed to close tdb: %s\n", strerror(errno))); - return -1; - } - - return 0; + return tdb_get_seqnum(db_ctx->wtdb->tdb); } struct db_context *db_open_tdb(TALLOC_CTX *mem_ctx, @@ -252,14 +242,13 @@ struct db_context *db_open_tdb(TALLOC_CTX *mem_ctx, goto fail; } - db_tdb->tdb = tdb_open_log(name, hash_size, tdb_flags, - open_flags, mode); - if (db_tdb->tdb == NULL) { + 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; } - talloc_set_destructor(db_tdb, db_tdb_ctx_destr); result->fetch_locked = db_tdb_fetch_locked; result->traverse = db_tdb_traverse; result->traverse_read = db_tdb_traverse_read; -- cgit From d824b98f80ba186030cbb70b3a1e5daf80469ecd Mon Sep 17 00:00:00 2001 From: Jeremy Allison Date: Mon, 9 Jul 2007 19:25:36 +0000 Subject: r23779: Change from v2 or later to v3 or later. Jeremy. (This used to be commit 407e6e695b8366369b7c76af1ff76869b45347b3) --- source3/lib/dbwrap_tdb.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'source3/lib/dbwrap_tdb.c') diff --git a/source3/lib/dbwrap_tdb.c b/source3/lib/dbwrap_tdb.c index 8997f9f040..5ae13fddc7 100644 --- a/source3/lib/dbwrap_tdb.c +++ b/source3/lib/dbwrap_tdb.c @@ -5,7 +5,7 @@ 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 + 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, -- cgit From 5e54558c6dea67b56bbfaba5698f3a434d3dffb6 Mon Sep 17 00:00:00 2001 From: Andrew Tridgell Date: Tue, 10 Jul 2007 00:52:41 +0000 Subject: r23784: use the GPLv3 boilerplate as recommended by the FSF and the license text (This used to be commit b0132e94fc5fef936aa766fb99a306b3628e9f07) --- source3/lib/dbwrap_tdb.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) (limited to 'source3/lib/dbwrap_tdb.c') diff --git a/source3/lib/dbwrap_tdb.c b/source3/lib/dbwrap_tdb.c index 5ae13fddc7..5c958b8afd 100644 --- a/source3/lib/dbwrap_tdb.c +++ b/source3/lib/dbwrap_tdb.c @@ -14,8 +14,7 @@ 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. + along with this program. If not, see . */ #include "includes.h" -- cgit From a116d7c7d9ddaf6bc6798ed98b3c62369e924076 Mon Sep 17 00:00:00 2001 From: Volker Lendecke Date: Wed, 29 Aug 2007 11:46:44 +0000 Subject: r24773: Fix a ctdb connection lockup The lockup could happen when packet_read_sync() gets two packets in a row, the first one being an async message, and the second one being the response to a ctdb request. Also add some debug msg to ctdb_conn.c, and cut off the "locking key" messages to only dump 20 hex chars at debug level 10. >10 will dump everything. (This used to be commit 0a55880a240b619810371a19144dd0a75208adfe) --- source3/lib/dbwrap_tdb.c | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) (limited to 'source3/lib/dbwrap_tdb.c') diff --git a/source3/lib/dbwrap_tdb.c b/source3/lib/dbwrap_tdb.c index 5c958b8afd..be691dc7ec 100644 --- a/source3/lib/dbwrap_tdb.c +++ b/source3/lib/dbwrap_tdb.c @@ -31,7 +31,8 @@ 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); - DEBUG(10, ("Unlocking key %s\n", + DEBUG(10, (DEBUGLEVEL > 10 + ? "Unlocking key %s\n" : "Unlocking key %20s\n", hex_encode(data, (unsigned char *)data->key.dptr, data->key.dsize))); @@ -70,10 +71,12 @@ static struct db_record *db_tdb_fetch_locked(struct db_context *db, result->store = db_tdb_store; result->delete_rec = db_tdb_delete; - { + if (DEBUGLEVEL > 10) { char *keystr = hex_encode(NULL, (unsigned char *)key.dptr, key.dsize); - DEBUG(10, ("Locking key %s\n", keystr)); + DEBUG(10, (DEBUGLEVEL > 10 + ? "Locking key %s\n" : "Locking key %20s\n", + keystr)); TALLOC_FREE(keystr); } -- cgit From e5a951325a6cac8567af3a66de6d2df577508ae4 Mon Sep 17 00:00:00 2001 From: "Gerald (Jerry) Carter" Date: Wed, 10 Oct 2007 15:34:30 -0500 Subject: [GLUE] Rsync SAMBA_3_2_0 SVN r25598 in order to create the v3-2-test branch. (This used to be commit 5c6c8e1fe93f340005110a7833946191659d88ab) --- source3/lib/dbwrap_tdb.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) (limited to 'source3/lib/dbwrap_tdb.c') diff --git a/source3/lib/dbwrap_tdb.c b/source3/lib/dbwrap_tdb.c index be691dc7ec..24462d6789 100644 --- a/source3/lib/dbwrap_tdb.c +++ b/source3/lib/dbwrap_tdb.c @@ -72,8 +72,7 @@ static struct db_record *db_tdb_fetch_locked(struct db_context *db, result->delete_rec = db_tdb_delete; if (DEBUGLEVEL > 10) { - char *keystr = hex_encode(NULL, (unsigned char *)key.dptr, - key.dsize); + char *keystr = hex_encode(NULL, key.dptr, key.dsize); DEBUG(10, (DEBUGLEVEL > 10 ? "Locking key %s\n" : "Locking key %20s\n", keystr)); -- cgit From 95b9e23095a51615bc51abb55adcd6330b57b66e Mon Sep 17 00:00:00 2001 From: Volker Lendecke Date: Wed, 7 Nov 2007 19:06:30 +0100 Subject: Fix dbwrap debug output (This used to be commit 9f9c933c16abacb2d0aa7bc7faa5b1ddac61b0e5) --- source3/lib/dbwrap_tdb.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'source3/lib/dbwrap_tdb.c') diff --git a/source3/lib/dbwrap_tdb.c b/source3/lib/dbwrap_tdb.c index 24462d6789..593a8ac1d6 100644 --- a/source3/lib/dbwrap_tdb.c +++ b/source3/lib/dbwrap_tdb.c @@ -32,7 +32,7 @@ static int db_tdb_record_destr(struct db_record* data) talloc_get_type_abort(data->private_data, struct db_tdb_ctx); DEBUG(10, (DEBUGLEVEL > 10 - ? "Unlocking key %s\n" : "Unlocking key %20s\n", + ? "Unlocking key %s\n" : "Unlocking key %.20s\n", hex_encode(data, (unsigned char *)data->key.dptr, data->key.dsize))); @@ -71,10 +71,10 @@ static struct db_record *db_tdb_fetch_locked(struct db_context *db, result->store = db_tdb_store; result->delete_rec = db_tdb_delete; - if (DEBUGLEVEL > 10) { + if (DEBUGLEVEL >= 10) { char *keystr = hex_encode(NULL, key.dptr, key.dsize); DEBUG(10, (DEBUGLEVEL > 10 - ? "Locking key %s\n" : "Locking key %20s\n", + ? "Locking key %s\n" : "Locking key %.20s\n", keystr)); TALLOC_FREE(keystr); } -- cgit From 18c90ed7d8b2328c3fb35660ffb4c1db793504b4 Mon Sep 17 00:00:00 2001 From: Volker Lendecke Date: Sat, 24 Nov 2007 20:21:19 +0100 Subject: Add db_tdb_fetch (This used to be commit efa8764c21edfbd09f535900ba37359d72903bf8) --- source3/lib/dbwrap_tdb.c | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) (limited to 'source3/lib/dbwrap_tdb.c') diff --git a/source3/lib/dbwrap_tdb.c b/source3/lib/dbwrap_tdb.c index 593a8ac1d6..b24fd0618a 100644 --- a/source3/lib/dbwrap_tdb.c +++ b/source3/lib/dbwrap_tdb.c @@ -109,6 +109,32 @@ static struct db_record *db_tdb_fetch_locked(struct db_context *db, return result; } +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); + + TDB_DATA data; + + data = tdb_fetch(ctx->wtdb->tdb, key); + + if (data.dptr == NULL) { + pdata->dptr = NULL; + pdata->dsize = 0; + return 0; + } + + pdata->dptr = (uint8 *)talloc_memdup(mem_ctx, data.dptr, data.dsize); + SAFE_FREE(data.dptr); + + if (pdata->dptr == NULL) { + return -1; + } + pdata->dsize = data.dsize; + return 0; +} + 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, @@ -251,6 +277,7 @@ struct db_context *db_open_tdb(TALLOC_CTX *mem_ctx, } 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->get_seqnum = db_tdb_get_seqnum; -- cgit From 6dc988c918d7909b6eec052e6c3511467e9be697 Mon Sep 17 00:00:00 2001 From: Volker Lendecke Date: Mon, 7 Jan 2008 00:14:24 +0100 Subject: Change db_tdb_fetch_locked to use only one talloc (This used to be commit 921c8657e2eeb71d5b9ae2675255a852b26cc30d) --- source3/lib/dbwrap_tdb.c | 88 +++++++++++++++++++++++++++++------------------- 1 file changed, 53 insertions(+), 35 deletions(-) (limited to 'source3/lib/dbwrap_tdb.c') diff --git a/source3/lib/dbwrap_tdb.c b/source3/lib/dbwrap_tdb.c index b24fd0618a..cce987d789 100644 --- a/source3/lib/dbwrap_tdb.c +++ b/source3/lib/dbwrap_tdb.c @@ -43,33 +43,50 @@ static int db_tdb_record_destr(struct db_record* data) 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 { + TALLOC_CTX *mem_ctx; struct db_record *result; - TDB_DATA value; +}; - result = TALLOC_P(mem_ctx, struct db_record); - if (result == NULL) { - DEBUG(0, ("talloc failed\n")); - return NULL; +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; } - 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; + 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; +} - result->value.dptr = NULL; - result->value.dsize = 0; - result->private_data = talloc_reference(result, ctx); - result->store = db_tdb_store; - result->delete_rec = db_tdb_delete; +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; + int res; if (DEBUGLEVEL >= 10) { char *keystr = hex_encode(NULL, key.dptr, key.dsize); @@ -81,32 +98,33 @@ static struct db_record *db_tdb_fetch_locked(struct db_context *db, if (tdb_chainlock(ctx->wtdb->tdb, key) != 0) { DEBUG(3, ("tdb_chainlock failed\n")); - TALLOC_FREE(result); return NULL; } - talloc_set_destructor(result, db_tdb_record_destr); + state.mem_ctx = mem_ctx; + state.result = NULL; - value = tdb_fetch(ctx->wtdb->tdb, key); + res = tdb_parse_record(ctx->wtdb->tdb, key, db_tdb_fetchlock_parse, + &state); - if (value.dptr == NULL) { - return result; + if (state.result == NULL) { + db_tdb_fetchlock_parse(key, tdb_null, &state); } - result->value.dsize = value.dsize; - result->value.dptr = (uint8 *)talloc_memdup(result, value.dptr, - value.dsize); - if (result->value.dptr == NULL) { - DEBUG(3, ("talloc failed\n")); - TALLOC_FREE(result); + if (state.result == NULL) { + tdb_chainunlock(ctx->wtdb->tdb, key); return NULL; } - SAFE_FREE(value.dptr); + talloc_set_destructor(state.result, db_tdb_record_destr); - DEBUG(10, ("Allocated locked data 0x%p\n", result)); + state.result->private_data = talloc_reference(state.result, ctx); + state.result->store = db_tdb_store; + state.result->delete_rec = db_tdb_delete; - return result; + DEBUG(10, ("Allocated locked data 0x%p\n", state.result)); + + return state.result; } static int db_tdb_fetch(struct db_context *db, TALLOC_CTX *mem_ctx, -- cgit From c3c19615c16a2a2dc7d3620d7117b9f261e5c35a Mon Sep 17 00:00:00 2001 From: Volker Lendecke Date: Mon, 7 Jan 2008 00:41:26 +0100 Subject: make db_tdb_fetch use tdb_parse_record (This used to be commit 88d82d0623e71ae1ef4f8fdefba10e3a230ea526) --- source3/lib/dbwrap_tdb.c | 46 +++++++++++++++++++++++++++++++++------------- 1 file changed, 33 insertions(+), 13 deletions(-) (limited to 'source3/lib/dbwrap_tdb.c') diff --git a/source3/lib/dbwrap_tdb.c b/source3/lib/dbwrap_tdb.c index cce987d789..83a0d111aa 100644 --- a/source3/lib/dbwrap_tdb.c +++ b/source3/lib/dbwrap_tdb.c @@ -70,7 +70,7 @@ static int db_tdb_fetchlock_parse(TDB_DATA key, TDB_DATA data, state->result->value.dsize = data.dsize; if (data.dsize > 0) { - state->result->value.dptr = state->result->key.dptr + key.dsize; + state->result->value.dptr = state->result->key.dptr+key.dsize; memcpy(state->result->value.dptr, data.dptr, data.dsize); } else { @@ -127,29 +127,49 @@ static struct db_record *db_tdb_fetch_locked(struct db_context *db, 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); - TDB_DATA data; - - data = tdb_fetch(ctx->wtdb->tdb, key); + struct tdb_fetch_state state; - if (data.dptr == NULL) { - pdata->dptr = NULL; - pdata->dsize = 0; - return 0; - } + state.mem_ctx = mem_ctx; + state.result = 0; + state.data.dptr = NULL; + state.data.dsize = 0; - pdata->dptr = (uint8 *)talloc_memdup(mem_ctx, data.dptr, data.dsize); - SAFE_FREE(data.dptr); + tdb_parse_record(ctx->wtdb->tdb, key, db_tdb_fetch_parse, &state); - if (pdata->dptr == NULL) { + if (state.result == -1) { return -1; } - pdata->dsize = data.dsize; + + *pdata = state.data; return 0; } -- cgit From b7d222c690a8b5e2b15b694c95015f735eff9cda Mon Sep 17 00:00:00 2001 From: Volker Lendecke Date: Thu, 10 Jan 2008 13:22:51 +0100 Subject: Tiny cosmetic fix (This used to be commit c82c1d462be6ddccd6e395b4a9630df91dacbda2) --- source3/lib/dbwrap_tdb.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) (limited to 'source3/lib/dbwrap_tdb.c') diff --git a/source3/lib/dbwrap_tdb.c b/source3/lib/dbwrap_tdb.c index 83a0d111aa..710e45de6b 100644 --- a/source3/lib/dbwrap_tdb.c +++ b/source3/lib/dbwrap_tdb.c @@ -160,8 +160,7 @@ static int db_tdb_fetch(struct db_context *db, TALLOC_CTX *mem_ctx, state.mem_ctx = mem_ctx; state.result = 0; - state.data.dptr = NULL; - state.data.dsize = 0; + state.data = tdb_null; tdb_parse_record(ctx->wtdb->tdb, key, db_tdb_fetch_parse, &state); -- cgit From 68694369fc96354452979b07425f3f48c4f73bbe Mon Sep 17 00:00:00 2001 From: Alexander Bokovoy Date: Wed, 16 Jan 2008 12:09:48 +0300 Subject: Merge CTDB-related fixes from samba-ctdb 3.0 branch (http://samba.org/~tridge/3_0-ctdb) Signed-off-by: Alexander Bokovoy (This used to be commit 0c8e23afbbb2d081fc23908bafcad04650bfacea) --- source3/lib/dbwrap_tdb.c | 21 +++++++++++---------- 1 file changed, 11 insertions(+), 10 deletions(-) (limited to 'source3/lib/dbwrap_tdb.c') diff --git a/source3/lib/dbwrap_tdb.c b/source3/lib/dbwrap_tdb.c index 710e45de6b..e87ceb428f 100644 --- a/source3/lib/dbwrap_tdb.c +++ b/source3/lib/dbwrap_tdb.c @@ -31,6 +31,11 @@ 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() 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(data, (unsigned char *)data->key.dptr, @@ -88,8 +93,9 @@ static struct db_record *db_tdb_fetch_locked(struct db_context *db, struct tdb_fetch_locked_state state; int res; - if (DEBUGLEVEL >= 10) { - char *keystr = hex_encode(NULL, key.dptr, key.dsize); + /* Do not accidently allocate/deallocate w/o need when debug level is lower than needed */ + if(DEBUGLEVEL >= 10) { + char *keystr = hex_encode(NULL, (unsigned char*)key.dptr, key.dsize); DEBUG(10, (DEBUGLEVEL > 10 ? "Locking key %s\n" : "Locking key %.20s\n", keystr)); @@ -191,15 +197,9 @@ 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); - int res; - - res = tdb_delete(ctx->wtdb->tdb, rec->key); - if (res == 0) { - return NT_STATUS_OK; - } - - return map_nt_error_from_tdb(tdb_error(ctx->wtdb->tdb)); + return (tdb_delete(ctx->wtdb->tdb, rec->key) == 0) ? + NT_STATUS_OK : NT_STATUS_UNSUCCESSFUL; } struct db_tdb_traverse_ctx { @@ -318,6 +318,7 @@ struct db_context *db_open_tdb(TALLOC_CTX *mem_ctx, result->traverse = db_tdb_traverse; result->traverse_read = db_tdb_traverse_read; result->get_seqnum = db_tdb_get_seqnum; + result->persistent = ((tdb_flags & TDB_CLEAR_IF_FIRST) == 0); return result; fail: -- cgit From 33f3eeaa00974860dfc45962d5fd34cf05396c76 Mon Sep 17 00:00:00 2001 From: Volker Lendecke Date: Sun, 20 Jan 2008 17:35:25 +0100 Subject: Fix some "set but never used" warnings (This used to be commit 4a6dadc5178f4861e9c032321939db3b639734b5) --- source3/lib/dbwrap_tdb.c | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) (limited to 'source3/lib/dbwrap_tdb.c') diff --git a/source3/lib/dbwrap_tdb.c b/source3/lib/dbwrap_tdb.c index e87ceb428f..18f9495931 100644 --- a/source3/lib/dbwrap_tdb.c +++ b/source3/lib/dbwrap_tdb.c @@ -91,7 +91,6 @@ static struct db_record *db_tdb_fetch_locked(struct db_context *db, struct db_tdb_ctx *ctx = talloc_get_type_abort(db->private_data, struct db_tdb_ctx); struct tdb_fetch_locked_state state; - int res; /* Do not accidently allocate/deallocate w/o need when debug level is lower than needed */ if(DEBUGLEVEL >= 10) { @@ -110,8 +109,7 @@ static struct db_record *db_tdb_fetch_locked(struct db_context *db, state.mem_ctx = mem_ctx; state.result = NULL; - res = tdb_parse_record(ctx->wtdb->tdb, key, db_tdb_fetchlock_parse, - &state); + tdb_parse_record(ctx->wtdb->tdb, key, db_tdb_fetchlock_parse, &state); if (state.result == NULL) { db_tdb_fetchlock_parse(key, tdb_null, &state); -- cgit From fa5391a919cf729134d9ec0368fced9cc028d02e Mon Sep 17 00:00:00 2001 From: Volker Lendecke Date: Thu, 24 Jan 2008 19:06:25 +0100 Subject: Return NOTFOUND from db_tdb_delete if the record does not exist (This used to be commit 1ff924c4360952eb1d714a2f2ec3760b380c7a24) --- source3/lib/dbwrap_tdb.c | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) (limited to 'source3/lib/dbwrap_tdb.c') diff --git a/source3/lib/dbwrap_tdb.c b/source3/lib/dbwrap_tdb.c index 18f9495931..da55049e52 100644 --- a/source3/lib/dbwrap_tdb.c +++ b/source3/lib/dbwrap_tdb.c @@ -196,8 +196,15 @@ 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); - return (tdb_delete(ctx->wtdb->tdb, rec->key) == 0) ? - NT_STATUS_OK : NT_STATUS_UNSUCCESSFUL; + 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 { -- cgit From 541b8dec4e21e0a88ccc0d85bc01433b66eb3588 Mon Sep 17 00:00:00 2001 From: Volker Lendecke Date: Mon, 10 Mar 2008 10:17:05 +0100 Subject: Add transactions to the dbwrap API Only filled in for tdb so far, for rbt it's pointless, and ctdb itself needs to be extended (This used to be commit 0a55e018dd68af06d84332d54148bbfb0b510b22) --- source3/lib/dbwrap_tdb.c | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) (limited to 'source3/lib/dbwrap_tdb.c') diff --git a/source3/lib/dbwrap_tdb.c b/source3/lib/dbwrap_tdb.c index da55049e52..7bdadd3770 100644 --- a/source3/lib/dbwrap_tdb.c +++ b/source3/lib/dbwrap_tdb.c @@ -291,6 +291,27 @@ static int db_tdb_get_seqnum(struct db_context *db) return tdb_get_seqnum(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); + return tdb_transaction_cancel(db_ctx->wtdb->tdb); +} + struct db_context *db_open_tdb(TALLOC_CTX *mem_ctx, const char *name, int hash_size, int tdb_flags, @@ -324,6 +345,9 @@ struct db_context *db_open_tdb(TALLOC_CTX *mem_ctx, result->traverse_read = db_tdb_traverse_read; result->get_seqnum = db_tdb_get_seqnum; 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: -- cgit