1
0
mirror of https://github.com/samba-team/samba.git synced 2025-03-22 02:50:28 +03:00

fix conflict in merge from metze

Merge commit 'metze/master-tdb-check'

Conflicts:

	server/ctdb_vacuum.c

(This used to be ctdb commit 82421f20dcad4b0e25f109583879a26c0ee9c465)
This commit is contained in:
Ronnie Sahlberg 2009-12-16 18:34:40 +11:00
commit 95eaa9f236
74 changed files with 4812 additions and 1966 deletions

View File

@ -1497,6 +1497,44 @@ int ctdb_ctrl_getdbname(struct ctdb_context *ctdb, struct timeval timeout, uint3
return 0;
}
/*
get the health status of a db
*/
int ctdb_ctrl_getdbhealth(struct ctdb_context *ctdb,
struct timeval timeout,
uint32_t destnode,
uint32_t dbid, TALLOC_CTX *mem_ctx,
const char **reason)
{
int ret;
int32_t res;
TDB_DATA data;
data.dptr = (uint8_t *)&dbid;
data.dsize = sizeof(dbid);
ret = ctdb_control(ctdb, destnode, 0,
CTDB_CONTROL_DB_GET_HEALTH, 0, data,
mem_ctx, &data, &res, &timeout, NULL);
if (ret != 0 || res != 0) {
return -1;
}
if (data.dsize == 0) {
(*reason) = NULL;
return 0;
}
(*reason) = talloc_strndup(mem_ctx, (const char *)data.dptr, data.dsize);
if ((*reason) == NULL) {
return -1;
}
talloc_free(data.dptr);
return 0;
}
/*
create a database
*/
@ -1849,7 +1887,7 @@ int ctdb_traverse(struct ctdb_db_context *ctdb_db, ctdb_traverse_func fn, void *
/*
called on each key during a catdb
*/
static int dumpdb_fn(struct ctdb_context *ctdb, TDB_DATA key, TDB_DATA data, void *p)
int ctdb_dumpdb_record(struct ctdb_context *ctdb, TDB_DATA key, TDB_DATA data, void *p)
{
int i;
FILE *f = (FILE *)p;
@ -1888,7 +1926,7 @@ static int dumpdb_fn(struct ctdb_context *ctdb, TDB_DATA key, TDB_DATA data, voi
*/
int ctdb_dump_db(struct ctdb_db_context *ctdb_db, FILE *f)
{
return ctdb_traverse(ctdb_db, dumpdb_fn, f);
return ctdb_traverse(ctdb_db, ctdb_dumpdb_record, f);
}
/*

View File

@ -103,13 +103,46 @@ build_ctdb_options () {
maybe_set "--lvs --single-public-ip" "$CTDB_LVS_PUBLIC_IP"
maybe_set "--script-log-level" "$CTDB_SCRIPT_LOG_LEVEL"
maybe_set "--syslog" "$CTDB_SYSLOG" "yes"
maybe_set "--max-persistent-check-errors" "$CTDB_MAX_PERSISTENT_CHECK_ERRORS"
}
check_tdb () {
local PDBASE=$1
local TDBTOOL_HAS_CHECK=`echo "help" | /usr/bin/tdbtool | grep check | wc -l`
test x"$TDBTOOL_HAS_CHECK" = x"1" && {
#
# Note tdbtool always exits with 0
#
local OK=`/usr/bin/tdbtool $PDBASE check | grep "Database integrity is OK" | wc -l`
test x"$OK" = x"1" || {
return 1;
}
return 0;
}
/usr/bin/tdbdump $PDBASE >/dev/null 2>/dev/null || {
return $?;
}
return 0;
}
check_persistent_databases () {
PERSISTENT_DB_DIR="${CTDB_DBDIR:-/var/ctdb}/persistent"
mkdir -p $PERSISTENT_DB_DIR 2>/dev/null
local ERRCOUNT=$CTDB_MAX_PERSISTENT_CHECK_ERRORS
test -z "$ERRCOUNT" && {
ERRCOUNT="0"
}
test x"$ERRCOUNT" != x"0" && {
return 0;
}
for PDBASE in `ls $PERSISTENT_DB_DIR/*.tdb.[0-9] 2>/dev/null`; do
/usr/bin/tdbdump $PDBASE >/dev/null 2>/dev/null || {
check_tdb $PDBASE || {
echo "Persistent database $PDBASE is corrupted! CTDB will not start."
return 1
}

View File

@ -200,6 +200,14 @@ CTDB_DEBUGLEVEL=ERR
# The default is "no".
# CTDB_RUN_TIMEOUT_MONITOR=no
# Should ctdbd start with corrupted/unhealthy persistent databases?
# This parameter specifies the max error count for persistent health
# checks before the "startup" event. The value must be a positive
# interger value, "0" or "-1".
# The default is "0", which means ctdbd will not start.
# "-1" means wait forever.
# CTDB_MAX_PERSISTENT_CHECK_ERRORS=0
# set any default tuning options for ctdb
# use CTDB_SET_XXXX=value where XXXX is the name of the tuning
# variable

View File

@ -10,6 +10,7 @@
}
. $CTDB_BASE/functions
loadconfig ctdb
loadconfig nfs
[ -z "$STATD_SHARED_DIRECTORY" ] && {

File diff suppressed because it is too large Load Diff

View File

@ -1,6 +1,6 @@
<html><head><meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"><title>ctdb</title><meta name="generator" content="DocBook XSL Stylesheets V1.73.2"></head><body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF"><div class="refentry" lang="en"><a name="ctdb.1"></a><div class="titlepage"></div><div class="refnamediv"><h2>Name</h2><p>ctdb &#8212; clustered tdb database management utility</p></div><div class="refsynopsisdiv"><h2>Synopsis</h2><div class="cmdsynopsis"><p><code class="command">ctdb [ OPTIONS ] COMMAND ...</code> </p></div><div class="cmdsynopsis"><p><code class="command">ctdb</code> [-n &lt;node&gt;] [-Y] [-t &lt;timeout&gt;] [-T &lt;timelimit&gt;] [-? --help] [--usage] [-d --debug=&lt;INTEGER&gt;] [--socket=&lt;filename&gt;]</p></div></div><div class="refsect1" lang="en"><a name="id2478395"></a><h2>DESCRIPTION</h2><p>
<html><head><meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"><title>ctdb</title><meta name="generator" content="DocBook XSL Stylesheets V1.75.1"></head><body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF"><div class="refentry" title="ctdb"><a name="ctdb.1"></a><div class="titlepage"></div><div class="refnamediv"><h2>Name</h2><p>ctdb &#8212; clustered tdb database management utility</p></div><div class="refsynopsisdiv" title="Synopsis"><h2>Synopsis</h2><div class="cmdsynopsis"><p><code class="command">ctdb [ OPTIONS ] COMMAND ...</code> </p></div><div class="cmdsynopsis"><p><code class="command">ctdb</code> [-n &lt;node&gt;] [-Y] [-t &lt;timeout&gt;] [-T &lt;timelimit&gt;] [-? --help] [--usage] [-d --debug=&lt;INTEGER&gt;] [--socket=&lt;filename&gt;]</p></div></div><div class="refsect1" title="DESCRIPTION"><a name="id401775"></a><h2>DESCRIPTION</h2><p>
ctdb is a utility to view and manage a ctdb cluster.
</p></div><div class="refsect1" lang="en"><a name="id2478405"></a><h2>OPTIONS</h2><div class="variablelist"><dl><dt><span class="term">-n &lt;pnn&gt;</span></dt><dd><p>
</p></div><div class="refsect1" title="OPTIONS"><a name="id401785"></a><h2>OPTIONS</h2><div class="variablelist"><dl><dt><span class="term">-n &lt;pnn&gt;</span></dt><dd><p>
This specifies the physical node number on which to execute the
command. Default is to run the command on the deamon running on
the local host.
@ -28,13 +28,13 @@
You only need to specify this parameter if you run multiple ctdb
daemons on the same physical host and thus can not use the default
name for the domain socket.
</p></dd></dl></div></div><div class="refsect1" lang="en"><a name="id2479674"></a><h2>Administrative Commands</h2><p>
</p></dd></dl></div></div><div class="refsect1" title="Administrative Commands"><a name="id401909"></a><h2>Administrative Commands</h2><p>
These are commands used to monitor and administrate a CTDB cluster.
</p><div class="refsect2" lang="en"><a name="id2479683"></a><h3>pnn</h3><p>
</p><div class="refsect2" title="pnn"><a name="id401917"></a><h3>pnn</h3><p>
This command displays the pnn of the current node.
</p></div><div class="refsect2" lang="en"><a name="id2479693"></a><h3>status</h3><p>
</p></div><div class="refsect2" title="status"><a name="id401926"></a><h3>status</h3><p>
This command shows the current status of the ctdb node.
</p><div class="refsect3" lang="en"><a name="id2479702"></a><h4>node status</h4><p>
</p><div class="refsect3" title="node status"><a name="id401934"></a><h4>node status</h4><p>
Node status reflects the current status of the node. There are five possible states:
</p><p>
OK - This node is fully functional.
@ -52,7 +52,7 @@
RECMASTER or NATGW.
This node does not perticipate in the CTDB cluster but can still be
communicated with. I.e. ctdb commands can be sent to it.
</p></div><div class="refsect3" lang="en"><a name="id2479763"></a><h4>generation</h4><p>
</p></div><div class="refsect3" title="generation"><a name="id401978"></a><h4>generation</h4><p>
The generation id is a number that indicates the current generation
of a cluster instance. Each time a cluster goes through a
reconfiguration or a recovery its generation id will be changed.
@ -73,10 +73,10 @@
All nodes start with generation "INVALID" and are not assigned a real
generation id until they have successfully been merged with a cluster
through a recovery.
</p></div><div class="refsect3" lang="en"><a name="id2479798"></a><h4>VNNMAP</h4><p>
</p></div><div class="refsect3" title="VNNMAP"><a name="id402003"></a><h4>VNNMAP</h4><p>
The list of Virtual Node Numbers. This is a list of all nodes that actively participates in the cluster and that share the workload of hosting the Clustered TDB database records.
Only nodes that are participating in the vnnmap can become lmaster or dmaster for a database record.
</p></div><div class="refsect3" lang="en"><a name="id2479812"></a><h4>Recovery mode</h4><p>
</p></div><div class="refsect3" title="Recovery mode"><a name="id402015"></a><h4>Recovery mode</h4><p>
This is the current recovery mode of the cluster. There are two possible modes:
</p><p>
NORMAL - The cluster is fully operational.
@ -96,7 +96,7 @@
have been recovered, the node mode will change into NORMAL mode
and the databases will be "thawed", allowing samba to access the
databases again.
</p></div><div class="refsect3" lang="en"><a name="id2528501"></a><h4>Recovery master</h4><p>
</p></div><div class="refsect3" title="Recovery master"><a name="id402046"></a><h4>Recovery master</h4><p>
This is the cluster node that is currently designated as the recovery master. This node is responsible of monitoring the consistency of the cluster and to perform the actual recovery process when reqired.
</p><p>
Only one node at a time can be the designated recovery master. Which
@ -118,9 +118,9 @@ hash:2 lmaster:2
hash:3 lmaster:3
Recovery mode:NORMAL (0)
Recovery master:0
</pre></div><div class="refsect2" lang="en"><a name="id2528538"></a><h3>recmaster</h3><p>
</pre></div><div class="refsect2" title="recmaster"><a name="id402078"></a><h3>recmaster</h3><p>
This command shows the pnn of the node which is currently the recmaster.
</p></div><div class="refsect2" lang="en"><a name="id2528549"></a><h3>uptime</h3><p>
</p></div><div class="refsect2" title="uptime"><a name="id402088"></a><h3>uptime</h3><p>
This command shows the uptime for the ctdb daemon. When the last recovery or ip-failover completed and how long it took. If the "duration" is shown as a negative number, this indicates that there is a recovery/failover in progress and it started that many seconds ago.
</p><p>
Example: ctdb uptime
@ -129,7 +129,7 @@ Current time of node : Thu Oct 29 10:38:54 2009
Ctdbd start time : (000 16:54:28) Wed Oct 28 17:44:26 2009
Time of last recovery/failover: (000 16:53:31) Wed Oct 28 17:45:23 2009
Duration of last recovery/failover: 2.248552 seconds
</pre></div><div class="refsect2" lang="en"><a name="id2528580"></a><h3>listnodes</h3><p>
</pre></div><div class="refsect2" title="listnodes"><a name="id402114"></a><h3>listnodes</h3><p>
This command shows lists the ip addresses of all the nodes in the cluster.
</p><p>
Example: ctdb listnodes
@ -138,7 +138,7 @@ Duration of last recovery/failover: 2.248552 seconds
10.0.0.72
10.0.0.73
10.0.0.74
</pre></div><div class="refsect2" lang="en"><a name="id2528604"></a><h3>ping</h3><p>
</pre></div><div class="refsect2" title="ping"><a name="id402136"></a><h3>ping</h3><p>
This command will "ping" all CTDB daemons in the cluster to verify that they are processing commands correctly.
</p><p>
Example: ctdb ping
@ -149,7 +149,7 @@ response from 0 time=0.000054 sec (3 clients)
response from 1 time=0.000144 sec (2 clients)
response from 2 time=0.000105 sec (2 clients)
response from 3 time=0.000114 sec (2 clients)
</pre></div><div class="refsect2" lang="en"><a name="id2528630"></a><h3>ip</h3><p>
</pre></div><div class="refsect2" title="ip"><a name="id402161"></a><h3>ip</h3><p>
This command will display the list of public addresses that are provided by the cluster and which physical node is currently serving this ip. By default this command will ONLY show those public addresses that are known to the node itself. To see the full list of all public ips across the cluster you must use "ctdb ip -n all".
</p><p>
Example: ctdb ip
@ -161,7 +161,7 @@ Number of addresses:4
12.1.1.2 1
12.1.1.3 2
12.1.1.4 3
</pre></div><div class="refsect2" lang="en"><a name="id2528658"></a><h3>scriptstatus</h3><p>
</pre></div><div class="refsect2" title="scriptstatus"><a name="id402186"></a><h3>scriptstatus</h3><p>
This command displays which scripts where run in the previous monitoring cycle and the result of each script. If a script failed with an error, causing the node to become unhealthy, the output from that script is also shown.
</p><p>
Example: ctdb scriptstatus
@ -178,15 +178,15 @@ Number of addresses:4
41.httpd Status:OK Duration:0.039 Tue Mar 24 18:56:57 2009
50.samba Status:ERROR Duration:0.082 Tue Mar 24 18:56:57 2009
OUTPUT:ERROR: Samba tcp port 445 is not responding
</pre></div><div class="refsect2" lang="en"><a name="id2528694"></a><h3>disablescript &lt;script&gt;</h3><p>
</pre></div><div class="refsect2" title="disablescript &lt;script&gt;"><a name="id402215"></a><h3>disablescript &lt;script&gt;</h3><p>
This command is used to disable an eventscript.
</p><p>
This will take effect the next time the eventscripts are being executed so it can take a short while until this is reflected in 'scriptstatus'.
</p></div><div class="refsect2" lang="en"><a name="id2528711"></a><h3>enablescript &lt;script&gt;</h3><p>
</p></div><div class="refsect2" title="enablescript &lt;script&gt;"><a name="id402229"></a><h3>enablescript &lt;script&gt;</h3><p>
This command is used to enable an eventscript.
</p><p>
This will take effect the next time the eventscripts are being executed so it can take a short while until this is reflected in 'scriptstatus'.
</p></div><div class="refsect2" lang="en"><a name="id2528727"></a><h3>getvar &lt;name&gt;</h3><p>
</p></div><div class="refsect2" title="getvar &lt;name&gt;"><a name="id402243"></a><h3>getvar &lt;name&gt;</h3><p>
Get the runtime value of a tuneable variable.
</p><p>
Example: ctdb getvar MaxRedirectCount
@ -194,33 +194,60 @@ Number of addresses:4
Example output:
</p><pre class="screen">
MaxRedirectCount = 3
</pre></div><div class="refsect2" lang="en"><a name="id2528750"></a><h3>setvar &lt;name&gt; &lt;value&gt;</h3><p>
</pre></div><div class="refsect2" title="setvar &lt;name&gt; &lt;value&gt;"><a name="id402264"></a><h3>setvar &lt;name&gt; &lt;value&gt;</h3><p>
Set the runtime value of a tuneable variable.
</p><p>
Example: ctdb setvar MaxRedirectCount 5
</p></div><div class="refsect2" lang="en"><a name="id2528765"></a><h3>listvars</h3><p>
</p></div><div class="refsect2" title="listvars"><a name="id402276"></a><h3>listvars</h3><p>
List all tuneable variables.
</p><p>
Example: ctdb listvars
</p><p>
Example output:
</p><pre class="screen">
MaxRedirectCount = 5
SeqnumFrequency = 1
MaxRedirectCount = 3
SeqnumInterval = 1000
ControlTimeout = 60
TraverseTimeout = 20
KeepaliveInterval = 2
KeepaliveLimit = 3
KeepaliveInterval = 5
KeepaliveLimit = 5
MaxLACount = 7
RecoverTimeout = 5
RecoverTimeout = 20
RecoverInterval = 1
ElectionTimeout = 3
TakeoverTimeout = 5
MonitorInterval = 15
EventScriptTimeout = 20
RecoveryGracePeriod = 60
TickleUpdateInterval = 20
EventScriptTimeout = 30
EventScriptBanCount = 10
EventScriptUnhealthyOnTimeout = 0
RecoveryGracePeriod = 120
RecoveryBanPeriod = 300
</pre></div><div class="refsect2" lang="en"><a name="id2528793"></a><h3>lvsmaster</h3><p>
DatabaseHashSize = 10000
DatabaseMaxDead = 5
RerecoveryTimeout = 10
EnableBans = 1
DeterministicIPs = 1
DisableWhenUnhealthy = 0
ReclockPingPeriod = 60
NoIPFailback = 0
VerboseMemoryNames = 0
RecdPingTimeout = 60
RecdFailCount = 10
LogLatencyMs = 0
RecLockLatencyMs = 1000
RecoveryDropAllIPs = 60
VerifyRecoveryLock = 1
VacuumDefaultInterval = 300
VacuumMaxRunTime = 30
RepackLimit = 10000
VacuumLimit = 5000
VacuumMinInterval = 60
VacuumMaxInterval = 600
MaxQueueDropMsg = 1000
UseStatusEvents = 0
AllowUnhealthyDBRead = 0
</pre></div><div class="refsect2" title="lvsmaster"><a name="id358916"></a><h3>lvsmaster</h3><p>
This command shows which node is currently the LVSMASTER. The
LVSMASTER is the node in the cluster which drives the LVS system and
which receives all incoming traffic from clients.
@ -231,7 +258,7 @@ RecoveryBanPeriod = 300
evenly onto the other nodes in the cluster. This is an alternative to using
public ip addresses. See the manpage for ctdbd for more information
about LVS.
</p></div><div class="refsect2" lang="en"><a name="id2528815"></a><h3>lvs</h3><p>
</p></div><div class="refsect2" title="lvs"><a name="id358934"></a><h3>lvs</h3><p>
This command shows which nodes in the cluster are currently active in the
LVS configuration. I.e. which nodes we are currently loadbalancing
the single ip address across.
@ -246,7 +273,7 @@ RecoveryBanPeriod = 300
</p><pre class="screen">
2:10.0.0.13
3:10.0.0.14
</pre></div><div class="refsect2" lang="en"><a name="id2528845"></a><h3>getcapabilities</h3><p>
</pre></div><div class="refsect2" title="getcapabilities"><a name="id358960"></a><h3>getcapabilities</h3><p>
This command shows the capabilities of the current node.
Please see manpage for ctdbd for a full list of all capabilities and
more detailed description.
@ -265,7 +292,7 @@ RecoveryBanPeriod = 300
RECMASTER: YES
LMASTER: YES
LVS: NO
</pre></div><div class="refsect2" lang="en"><a name="id2528881"></a><h3>statistics</h3><p>
</pre></div><div class="refsect2" title="statistics"><a name="id358990"></a><h3>statistics</h3><p>
Collect statistics from the CTDB daemon about how many calls it has served.
</p><p>
Example: ctdb statistics
@ -307,23 +334,23 @@ CTDB version 1
max_hop_count 0
max_call_latency 4.948321 sec
max_lockwait_latency 0.000000 sec
</pre></div><div class="refsect2" lang="en"><a name="id2528925"></a><h3>statisticsreset</h3><p>
</pre></div><div class="refsect2" title="statisticsreset"><a name="id359033"></a><h3>statisticsreset</h3><p>
This command is used to clear all statistics counters in a node.
</p><p>
Example: ctdb statisticsreset
</p></div><div class="refsect2" lang="en"><a name="id2528939"></a><h3>getreclock</h3><p>
</p></div><div class="refsect2" title="getreclock"><a name="id359046"></a><h3>getreclock</h3><p>
This command is used to show the filename of the reclock file that is used.
</p><p>
Example output:
</p><pre class="screen">
Reclock file:/gpfs/.ctdb/shared
</pre></div><div class="refsect2" lang="en"><a name="id2528959"></a><h3>setreclock [filename]</h3><p>
</pre></div><div class="refsect2" title="setreclock [filename]"><a name="id359064"></a><h3>setreclock [filename]</h3><p>
This command is used to modify, or clear, the file that is used as the reclock file at runtime. When this command is used, the reclock file checks are disabled. To re-enable the checks the administrator needs to activate the "VerifyRecoveryLock" tunable using "ctdb setvar".
</p><p>
If run with no parameter this will remove the reclock file completely. If run with a parameter the parameter specifies the new filename to use for the recovery lock.
</p><p>
This command only affects the runtime settings of a ctdb node and will be lost when ctdb is restarted. For persistent changes to the reclock file setting you must edit /etc/sysconfig/ctdb.
</p></div><div class="refsect2" lang="en"><a name="id2528985"></a><h3>getdebug</h3><p>
</p></div><div class="refsect2" title="getdebug"><a name="id359085"></a><h3>getdebug</h3><p>
Get the current debug level for the node. the debug level controls what information is written to the log file.
</p><p>
The debug levels are mapped to the corresponding syslog levels.
@ -333,42 +360,42 @@ Reclock file:/gpfs/.ctdb/shared
The list of debug levels from highest to lowest are :
</p><p>
EMERG ALERT CRIT ERR WARNING NOTICE INFO DEBUG
</p></div><div class="refsect2" lang="en"><a name="id2529011"></a><h3>setdebug &lt;debuglevel&gt;</h3><p>
</p></div><div class="refsect2" title="setdebug &lt;debuglevel&gt;"><a name="id359106"></a><h3>setdebug &lt;debuglevel&gt;</h3><p>
Set the debug level of a node. This controls what information will be logged.
</p><p>
The debuglevel is one of EMERG ALERT CRIT ERR WARNING NOTICE INFO DEBUG
</p></div><div class="refsect2" lang="en"><a name="id2529027"></a><h3>getpid</h3><p>
</p></div><div class="refsect2" title="getpid"><a name="id359119"></a><h3>getpid</h3><p>
This command will return the process id of the ctdb daemon.
</p></div><div class="refsect2" lang="en"><a name="id2529037"></a><h3>disable</h3><p>
</p></div><div class="refsect2" title="disable"><a name="id359128"></a><h3>disable</h3><p>
This command is used to administratively disable a node in the cluster.
A disabled node will still participate in the cluster and host
clustered TDB records but its public ip address has been taken over by
a different node and it no longer hosts any services.
</p></div><div class="refsect2" lang="en"><a name="id2529051"></a><h3>enable</h3><p>
</p></div><div class="refsect2" title="enable"><a name="id359140"></a><h3>enable</h3><p>
Re-enable a node that has been administratively disabled.
</p></div><div class="refsect2" lang="en"><a name="id2529061"></a><h3>stop</h3><p>
</p></div><div class="refsect2" title="stop"><a name="id359148"></a><h3>stop</h3><p>
This command is used to administratively STOP a node in the cluster.
A STOPPED node is connected to the cluster but will not host any
public ip addresse, nor does it participate in the VNNMAP.
The difference between a DISABLED node and a STOPPED node is that
a STOPPED node does not host any parts of the database which means
that a recovery is required to stop/continue nodes.
</p></div><div class="refsect2" lang="en"><a name="id2529076"></a><h3>continue</h3><p>
</p></div><div class="refsect2" title="continue"><a name="id359161"></a><h3>continue</h3><p>
Re-start a node that has been administratively stopped.
</p></div><div class="refsect2" lang="en"><a name="id2529087"></a><h3>addip &lt;public_ip/mask&gt; &lt;iface&gt;</h3><p>
</p></div><div class="refsect2" title="addip &lt;public_ip/mask&gt; &lt;iface&gt;"><a name="id359169"></a><h3>addip &lt;public_ip/mask&gt; &lt;iface&gt;</h3><p>
This command is used to add a new public ip to a node during runtime.
This allows public addresses to be added to a cluster without having
to restart the ctdb daemons.
</p><p>
Note that this only updates the runtime instance of ctdb. Any changes will be lost next time ctdb is restarted and the public addresses file is re-read.
If you want this change to be permanent you must also update the public addresses file manually.
</p></div><div class="refsect2" lang="en"><a name="id2529107"></a><h3>delip &lt;public_ip&gt;</h3><p>
</p></div><div class="refsect2" title="delip &lt;public_ip&gt;"><a name="id359186"></a><h3>delip &lt;public_ip&gt;</h3><p>
This command is used to remove a public ip from a node during runtime.
If this public ip is currently hosted by the node it being removed from, the ip will first be failed over to another node, if possible, before it is removed.
</p><p>
Note that this only updates the runtime instance of ctdb. Any changes will be lost next time ctdb is restarted and the public addresses file is re-read.
If you want this change to be permanent you must also update the public addresses file manually.
</p></div><div class="refsect2" lang="en"><a name="id2529132"></a><h3>moveip &lt;public_ip&gt; &lt;node&gt;</h3><p>
</p></div><div class="refsect2" title="moveip &lt;public_ip&gt; &lt;node&gt;"><a name="id359202"></a><h3>moveip &lt;public_ip&gt; &lt;node&gt;</h3><p>
This command can be used to manually fail a public ip address to a
specific node.
</p><p>
@ -379,14 +406,14 @@ Reclock file:/gpfs/.ctdb/shared
DeterministicIPs = 0
</p><p>
NoIPFailback = 1
</p></div><div class="refsect2" lang="en"><a name="id2529157"></a><h3>shutdown</h3><p>
</p></div><div class="refsect2" title="shutdown"><a name="id359224"></a><h3>shutdown</h3><p>
This command will shutdown a specific CTDB daemon.
</p></div><div class="refsect2" lang="en"><a name="id2529167"></a><h3>recover</h3><p>
</p></div><div class="refsect2" title="recover"><a name="id359233"></a><h3>recover</h3><p>
This command will trigger the recovery daemon to do a cluster
recovery.
</p></div><div class="refsect2" lang="en"><a name="id2529178"></a><h3>ipreallocate</h3><p>
</p></div><div class="refsect2" title="ipreallocate"><a name="id359243"></a><h3>ipreallocate</h3><p>
This command will force the recovery master to perform a full ip reallocation process and redistribute all ip addresses. This is useful to "reset" the allocations back to its default state if they have been changed using the "moveip" command. While a "recover" will also perform this reallocation, a recovery is much more hevyweight since it will also rebuild all the databases.
</p></div><div class="refsect2" lang="en"><a name="id2529193"></a><h3>setlmasterrole &lt;on|off&gt;</h3><p>
</p></div><div class="refsect2" title="setlmasterrole &lt;on|off&gt;"><a name="id359255"></a><h3>setlmasterrole &lt;on|off&gt;</h3><p>
This command is used ot enable/disable the LMASTER capability for a node at runtime. This capability determines whether or not a node can be used as an LMASTER for records in the database. A node that does not have the LMASTER capability will not show up in the vnnmap.
</p><p>
Nodes will by default have this capability, but it can be stripped off nodes by the setting in the sysconfig file or by using this command.
@ -394,21 +421,21 @@ Reclock file:/gpfs/.ctdb/shared
Once this setting has been enabled/disabled, you need to perform a recovery for it to take effect.
</p><p>
See also "ctdb getcapabilities"
</p></div><div class="refsect2" lang="en"><a name="id2529222"></a><h3>setrecmasterrole &lt;on|off&gt;</h3><p>
</p></div><div class="refsect2" title="setrecmasterrole &lt;on|off&gt;"><a name="id359279"></a><h3>setrecmasterrole &lt;on|off&gt;</h3><p>
This command is used ot enable/disable the RECMASTER capability for a node at runtime. This capability determines whether or not a node can be used as an RECMASTER for the cluster. A node that does not have the RECMASTER capability can not win a recmaster election. A node that already is the recmaster for the cluster when the capability is stripped off the node will remain the recmaster until the next cluster election.
</p><p>
Nodes will by default have this capability, but it can be stripped off nodes by the setting in the sysconfig file or by using this command.
</p><p>
See also "ctdb getcapabilities"
</p></div><div class="refsect2" lang="en"><a name="id2529249"></a><h3>killtcp &lt;srcip:port&gt; &lt;dstip:port&gt;</h3><p>
</p></div><div class="refsect2" title="killtcp &lt;srcip:port&gt; &lt;dstip:port&gt;"><a name="id359299"></a><h3>killtcp &lt;srcip:port&gt; &lt;dstip:port&gt;</h3><p>
This command will kill the specified TCP connection by issuing a
TCP RST to the srcip:port endpoint. This is a command used by the
ctdb eventscripts.
</p></div><div class="refsect2" lang="en"><a name="id2529261"></a><h3>gratiousarp &lt;ip&gt; &lt;interface&gt;</h3><p>
</p></div><div class="refsect2" title="gratiousarp &lt;ip&gt; &lt;interface&gt;"><a name="id359310"></a><h3>gratiousarp &lt;ip&gt; &lt;interface&gt;</h3><p>
This command will send out a gratious arp for the specified interface
through the specified interface. This command is mainly used by the
ctdb eventscripts.
</p></div><div class="refsect2" lang="en"><a name="id2529274"></a><h3>reloadnodes</h3><p>
</p></div><div class="refsect2" title="reloadnodes"><a name="id359320"></a><h3>reloadnodes</h3><p>
This command is used when adding new nodes, or removing existing nodes from an existing cluster.
</p><p>
Procedure to add a node:
@ -442,7 +469,7 @@ Reclock file:/gpfs/.ctdb/shared
</p><p>
5, Use 'ctdb status' on all nodes and verify that the deleted node no longer shows up in the list..
</p><p>
</p></div><div class="refsect2" lang="en"><a name="id2529357"></a><h3>tickle &lt;srcip:port&gt; &lt;dstip:port&gt;</h3><p>
</p></div><div class="refsect2" title="tickle &lt;srcip:port&gt; &lt;dstip:port&gt;"><a name="id359386"></a><h3>tickle &lt;srcip:port&gt; &lt;dstip:port&gt;</h3><p>
This command will will send a TCP tickle to the source host for the
specified TCP connection.
A TCP tickle is a TCP ACK packet with an invalid sequence and
@ -454,10 +481,10 @@ Reclock file:/gpfs/.ctdb/shared
TCP connection has been disrupted and that the client will need
to reestablish. This greatly speeds up the time it takes for a client
to detect and reestablish after an IP failover in the ctdb cluster.
</p></div><div class="refsect2" lang="en"><a name="id2529382"></a><h3>gettickles &lt;ip&gt;</h3><p>
</p></div><div class="refsect2" title="gettickles &lt;ip&gt;"><a name="id359405"></a><h3>gettickles &lt;ip&gt;</h3><p>
This command is used to show which TCP connections are registered with
CTDB to be "tickled" if there is a failover.
</p></div><div class="refsect2" lang="en"><a name="id2529394"></a><h3>repack [max_freelist]</h3><p>
</p></div><div class="refsect2" title="repack [max_freelist]"><a name="id359415"></a><h3>repack [max_freelist]</h3><p>
Over time, when records are created and deleted in a TDB, the TDB list of free space will become fragmented. This can lead to a slowdown in accessing TDB records.
This command is used to defragment a TDB database and pruning the freelist.
</p><p>
@ -472,7 +499,7 @@ Reclock file:/gpfs/.ctdb/shared
Example: ctdb repack 1000
</p><p>
By default, this operation is issued from the 00.ctdb event script every 5 minutes.
</p></div><div class="refsect2" lang="en"><a name="id2529440"></a><h3>vacuum [max_records]</h3><p>
</p></div><div class="refsect2" title="vacuum [max_records]"><a name="id359454"></a><h3>vacuum [max_records]</h3><p>
Over time CTDB databases will fill up with empty deleted records which will lead to a progressive slow down of CTDB database access.
This command is used to prune all databases and delete all empty records from the cluster.
</p><p>
@ -488,18 +515,30 @@ Reclock file:/gpfs/.ctdb/shared
Example: ctdb vacuum
</p><p>
By default, this operation is issued from the 00.ctdb event script every 5 minutes.
</p></div><div class="refsect2" lang="en"><a name="id2529476"></a><h3>backupdb &lt;database&gt; &lt;file&gt;</h3><p>
</p></div><div class="refsect2" title="backupdb &lt;dbname&gt; &lt;file&gt;"><a name="id359483"></a><h3>backupdb &lt;dbname&gt; &lt;file&gt;</h3><p>
This command can be used to copy the entire content of a database out to a file. This file can later be read back into ctdb using the restoredb command.
This is mainly useful for backing up persistent databases such as secrets.tdb and similar.
</p></div><div class="refsect2" lang="en"><a name="id2529490"></a><h3>restoredb &lt;file&gt;</h3><p>
</p></div><div class="refsect2" title="restoredb &lt;file&gt;"><a name="id359495"></a><h3>restoredb &lt;file&gt;</h3><p>
This command restores a persistent database that was previously backed up using backupdb.
</p></div></div><div class="refsect1" lang="en"><a name="id2529502"></a><h2>Debugging Commands</h2><p>
</p></div><div class="refsect2" title="wipedb &lt;dbname&gt;"><a name="id403114"></a><h3>wipedb &lt;dbname&gt;</h3><p>
This command can be used to remove all content of a database.
</p></div></div><div class="refsect1" title="Debugging Commands"><a name="id403124"></a><h2>Debugging Commands</h2><p>
These commands are primarily used for CTDB development and testing and
should not be used for normal administration.
</p><div class="refsect2" lang="en"><a name="id2529512"></a><h3>process-exists &lt;pid&gt;</h3><p>
</p><div class="refsect2" title="process-exists &lt;pid&gt;"><a name="id403133"></a><h3>process-exists &lt;pid&gt;</h3><p>
This command checks if a specific process exists on the CTDB host. This is mainly used by Samba to check if remote instances of samba are still running or not.
</p></div><div class="refsect2" lang="en"><a name="id2529524"></a><h3>getdbmap</h3><p>
</p></div><div class="refsect2" title="getdbmap"><a name="id403143"></a><h3>getdbmap</h3><p>
This command lists all clustered TDB databases that the CTDB daemon has attached to. Some databases are flagged as PERSISTENT, this means that the database stores data persistently and the data will remain across reboots. One example of such a database is secrets.tdb where information about how the cluster was joined to the domain is stored.
</p><p>
If a PERSISTENT database is not in a healthy state the database is
flagged as UNHEALTHY. If there's at least one completely healthy node running in
the cluster, it's possible that the content is restored by a recovery
run automaticly. Otherwise an administrator needs to analyze the
problem.
</p><p>
See also "ctdb getdbstatus", "ctdb backupdb", "ctdb restoredb",
"ctdb dumpbackup", "ctdb wipedb", "ctdb setvar AllowUnhealthyDBRead 1"
and (if samba or tdb-utils are installed) "tdbtool check".
</p><p>
Most databases are not persistent and only store the state information that the currently running samba daemons need. These databases are always wiped when ctdb/samba starts and when a node is rebooted.
</p><p>
@ -517,25 +556,60 @@ dbid:0x2672a57f name:idmap2.tdb path:/var/ctdb/persistent/idmap2.tdb.0 PERSISTEN
dbid:0xb775fff6 name:secrets.tdb path:/var/ctdb/persistent/secrets.tdb.0 PERSISTENT
dbid:0xe98e08b6 name:group_mapping.tdb path:/var/ctdb/persistent/group_mapping.tdb.0 PERSISTENT
dbid:0x7bbbd26c name:passdb.tdb path:/var/ctdb/persistent/passdb.tdb.0 PERSISTENT
</pre></div><div class="refsect2" lang="en"><a name="id2529558"></a><h3>catdb &lt;dbname&gt;</h3><p>
</pre><p>
Example output for an unhealthy database:
</p><pre class="screen">
Number of databases:1
dbid:0xb775fff6 name:secrets.tdb path:/var/ctdb/persistent/secrets.tdb.0 PERSISTENT UNHEALTHY
</pre><p>
Example output for a healthy database as machinereadable output -Y:
</p><pre class="screen">
:ID:Name:Path:Persistent:Unhealthy:
:0x7bbbd26c:passdb.tdb:/var/ctdb/persistent/passdb.tdb.0:1:0:
</pre></div><div class="refsect2" title="getdbstatus &lt;dbname&gt;"><a name="id403208"></a><h3>getdbstatus &lt;dbname&gt;</h3><p>
This command displays more details about a database.
</p><p>
Example: ctdb getdbstatus test.tdb.0
</p><p>
Example output:
</p><pre class="screen">
dbid: 0x122224da
name: test.tdb
path: /var/ctdb/test.tdb.0
PERSISTENT: no
HEALTH: OK
</pre><p>
Example: ctdb getdbstatus registry.tdb (with a corrupted TDB)
</p><p>
Example output:
</p><pre class="screen">
dbid: 0xf2a58948
name: registry.tdb
path: /var/ctdb/persistent/registry.tdb.0
PERSISTENT: yes
HEALTH: NO-HEALTHY-NODES - ERROR - Backup of corrupted TDB in '/var/ctdb/persistent/registry.tdb.0.corrupted.20091208091949.0Z'
</pre></div><div class="refsect2" title="catdb &lt;dbname&gt;"><a name="id403243"></a><h3>catdb &lt;dbname&gt;</h3><p>
This command will dump a clustered TDB database to the screen. This is a debugging command.
</p></div><div class="refsect2" lang="en"><a name="id2476137"></a><h3>getmonmode</h3><p>
</p></div><div class="refsect2" title="dumpdbbackup &lt;backup-file&gt;"><a name="id403252"></a><h3>dumpdbbackup &lt;backup-file&gt;</h3><p>
This command will dump the content of database backup to the screen
(similar to ctdb catdb). This is a debugging command.
</p></div><div class="refsect2" title="getmonmode"><a name="id403262"></a><h3>getmonmode</h3><p>
This command returns the monutoring mode of a node. The monitoring mode is either ACTIVE or DISABLED. Normally a node will continously monitor that all other nodes that are expected are in fact connected and that they respond to commands.
</p><p>
ACTIVE - This is the normal mode. The node is actively monitoring all other nodes, both that the transport is connected and also that the node responds to commands. If a node becomes unavailable, it will be marked as DISCONNECTED and a recovery is initiated to restore the cluster.
</p><p>
DISABLED - This node is not monitoring that other nodes are available. In this mode a node failure will not be detected and no recovery will be performed. This mode is useful when for debugging purposes one wants to attach GDB to a ctdb process but wants to prevent the rest of the cluster from marking this node as DISCONNECTED and do a recovery.
</p></div><div class="refsect2" lang="en"><a name="id2476168"></a><h3>setmonmode &lt;0|1&gt;</h3><p>
</p></div><div class="refsect2" title="setmonmode &lt;0|1&gt;"><a name="id403285"></a><h3>setmonmode &lt;0|1&gt;</h3><p>
This command can be used to explicitely disable/enable monitoring mode on a node. The main purpose is if one wants to attach GDB to a running ctdb daemon but wants to prevent the other nodes from marking it as DISCONNECTED and issuing a recovery. To do this, set monitoring mode to 0 on all nodes before attaching with GDB. Remember to set monitoring mode back to 1 afterwards.
</p></div><div class="refsect2" lang="en"><a name="id2476183"></a><h3>attach &lt;dbname&gt;</h3><p>
</p></div><div class="refsect2" title="attach &lt;dbname&gt;"><a name="id403297"></a><h3>attach &lt;dbname&gt;</h3><p>
This is a debugging command. This command will make the CTDB daemon create a new CTDB database and attach to it.
</p></div><div class="refsect2" lang="en"><a name="id2476195"></a><h3>dumpmemory</h3><p>
</p></div><div class="refsect2" title="dumpmemory"><a name="id403307"></a><h3>dumpmemory</h3><p>
This is a debugging command. This command will make the ctdb
daemon to write a fill memory allocation map to standard output.
</p></div><div class="refsect2" lang="en"><a name="id2476206"></a><h3>rddumpmemory</h3><p>
</p></div><div class="refsect2" title="rddumpmemory"><a name="id403317"></a><h3>rddumpmemory</h3><p>
This is a debugging command. This command will dump the talloc memory
allocation tree for the recovery daemon to standard output.
</p></div><div class="refsect2" lang="en"><a name="id2476218"></a><h3>freeze</h3><p>
</p></div><div class="refsect2" title="freeze"><a name="id403327"></a><h3>freeze</h3><p>
This command will lock all the local TDB databases causing clients
that are accessing these TDBs such as samba3 to block until the
databases are thawed.
@ -543,26 +617,26 @@ dbid:0x7bbbd26c name:passdb.tdb path:/var/ctdb/persistent/passdb.tdb.0 PERSISTEN
This is primarily used by the recovery daemon to stop all samba
daemons from accessing any databases while the database is recovered
and rebuilt.
</p></div><div class="refsect2" lang="en"><a name="id2476236"></a><h3>thaw</h3><p>
</p></div><div class="refsect2" title="thaw"><a name="id403342"></a><h3>thaw</h3><p>
Thaw a previously frozen node.
</p></div><div class="refsect2" lang="en"><a name="id2476246"></a><h3>eventscript &lt;arguments&gt;</h3><p>
</p></div><div class="refsect2" title="eventscript &lt;arguments&gt;"><a name="id403350"></a><h3>eventscript &lt;arguments&gt;</h3><p>
This is a debugging command. This command can be used to manually
invoke and run the eventscritps with arbitrary arguments.
</p></div><div class="refsect2" lang="en"><a name="id2476257"></a><h3>ban &lt;bantime|0&gt;</h3><p>
</p></div><div class="refsect2" title="ban &lt;bantime|0&gt;"><a name="id403360"></a><h3>ban &lt;bantime|0&gt;</h3><p>
Administratively ban a node for bantime seconds. A bantime of 0 means that the node should be permanently banned.
</p><p>
A banned node does not participate in the cluster and does not host any records for the clustered TDB. Its ip address has been taken over by an other node and no services are hosted.
</p><p>
Nodes are automatically banned if they are the cause of too many
cluster recoveries.
</p></div><div class="refsect2" lang="en"><a name="id2476281"></a><h3>unban</h3><p>
</p></div><div class="refsect2" title="unban"><a name="id403379"></a><h3>unban</h3><p>
This command is used to unban a node that has either been
administratively banned using the ban command or has been automatically
banned by the recovery daemon.
</p></div></div><div class="refsect1" lang="en"><a name="id2476294"></a><h2>SEE ALSO</h2><p>
</p></div></div><div class="refsect1" title="SEE ALSO"><a name="id403390"></a><h2>SEE ALSO</h2><p>
ctdbd(1), onnode(1)
<a class="ulink" href="http://ctdb.samba.org/" target="_top">http://ctdb.samba.org/</a>
</p></div><div class="refsect1" lang="en"><a name="id2476307"></a><h2>COPYRIGHT/LICENSE</h2><div class="literallayout"><p><br>
</p></div><div class="refsect1" title="COPYRIGHT/LICENSE"><a name="id403403"></a><h2>COPYRIGHT/LICENSE</h2><div class="literallayout"><p><br>
Copyright (C) Andrew Tridgell 2007<br>
Copyright (C) Ronnie sahlberg 2007<br>
<br>

View File

@ -5,6 +5,8 @@
<refmeta>
<refentrytitle>ctdb</refentrytitle>
<manvolnum>1</manvolnum>
<refmiscinfo class="source"> </refmiscinfo>
<refmiscinfo class="manual"> </refmiscinfo>
</refmeta>
@ -417,21 +419,48 @@ MaxRedirectCount = 3
Example output:
</para>
<screen format="linespecific">
MaxRedirectCount = 5
SeqnumFrequency = 1
MaxRedirectCount = 3
SeqnumInterval = 1000
ControlTimeout = 60
TraverseTimeout = 20
KeepaliveInterval = 2
KeepaliveLimit = 3
KeepaliveInterval = 5
KeepaliveLimit = 5
MaxLACount = 7
RecoverTimeout = 5
RecoverTimeout = 20
RecoverInterval = 1
ElectionTimeout = 3
TakeoverTimeout = 5
MonitorInterval = 15
EventScriptTimeout = 20
RecoveryGracePeriod = 60
TickleUpdateInterval = 20
EventScriptTimeout = 30
EventScriptBanCount = 10
EventScriptUnhealthyOnTimeout = 0
RecoveryGracePeriod = 120
RecoveryBanPeriod = 300
DatabaseHashSize = 10000
DatabaseMaxDead = 5
RerecoveryTimeout = 10
EnableBans = 1
DeterministicIPs = 1
DisableWhenUnhealthy = 0
ReclockPingPeriod = 60
NoIPFailback = 0
VerboseMemoryNames = 0
RecdPingTimeout = 60
RecdFailCount = 10
LogLatencyMs = 0
RecLockLatencyMs = 1000
RecoveryDropAllIPs = 60
VerifyRecoveryLock = 1
VacuumDefaultInterval = 300
VacuumMaxRunTime = 30
RepackLimit = 10000
VacuumLimit = 5000
VacuumMinInterval = 60
VacuumMaxInterval = 600
MaxQueueDropMsg = 1000
UseStatusEvents = 0
AllowUnhealthyDBRead = 0
</screen>
</refsect2>
@ -900,7 +929,7 @@ Reclock file:/gpfs/.ctdb/shared
</para>
</refsect2>
<refsect2><title>backupdb &lt;database&gt; &lt;file&gt;</title>
<refsect2><title>backupdb &lt;dbname&gt; &lt;file&gt;</title>
<para>
This command can be used to copy the entire content of a database out to a file. This file can later be read back into ctdb using the restoredb command.
This is mainly useful for backing up persistent databases such as secrets.tdb and similar.
@ -913,6 +942,11 @@ This is mainly useful for backing up persistent databases such as secrets.tdb an
</para>
</refsect2>
<refsect2><title>wipedb &lt;dbname&gt;</title>
<para>
This command can be used to remove all content of a database.
</para>
</refsect2>
</refsect1>
@ -930,6 +964,18 @@ This is mainly useful for backing up persistent databases such as secrets.tdb an
<refsect2><title>getdbmap</title>
<para>
This command lists all clustered TDB databases that the CTDB daemon has attached to. Some databases are flagged as PERSISTENT, this means that the database stores data persistently and the data will remain across reboots. One example of such a database is secrets.tdb where information about how the cluster was joined to the domain is stored.
</para>
<para>
If a PERSISTENT database is not in a healthy state the database is
flagged as UNHEALTHY. If there's at least one completely healthy node running in
the cluster, it's possible that the content is restored by a recovery
run automaticly. Otherwise an administrator needs to analyze the
problem.
</para>
<para>
See also "ctdb getdbstatus", "ctdb backupdb", "ctdb restoredb",
"ctdb dumpbackup", "ctdb wipedb", "ctdb setvar AllowUnhealthyDBRead 1"
and (if samba or tdb-utils are installed) "tdbtool check".
</para>
<para>
Most databases are not persistent and only store the state information that the currently running samba daemons need. These databases are always wiped when ctdb/samba starts and when a node is rebooted.
@ -952,6 +998,53 @@ dbid:0xb775fff6 name:secrets.tdb path:/var/ctdb/persistent/secrets.tdb.0 PERSIST
dbid:0xe98e08b6 name:group_mapping.tdb path:/var/ctdb/persistent/group_mapping.tdb.0 PERSISTENT
dbid:0x7bbbd26c name:passdb.tdb path:/var/ctdb/persistent/passdb.tdb.0 PERSISTENT
</screen>
<para>
Example output for an unhealthy database:
</para>
<screen format="linespecific">
Number of databases:1
dbid:0xb775fff6 name:secrets.tdb path:/var/ctdb/persistent/secrets.tdb.0 PERSISTENT UNHEALTHY
</screen>
<para>
Example output for a healthy database as machinereadable output -Y:
</para>
<screen format="linespecific">
:ID:Name:Path:Persistent:Unhealthy:
:0x7bbbd26c:passdb.tdb:/var/ctdb/persistent/passdb.tdb.0:1:0:
</screen>
</refsect2>
<refsect2><title>getdbstatus &lt;dbname&gt;</title>
<para>
This command displays more details about a database.
</para>
<para>
Example: ctdb getdbstatus test.tdb.0
</para>
<para>
Example output:
</para>
<screen format="linespecific">
dbid: 0x122224da
name: test.tdb
path: /var/ctdb/test.tdb.0
PERSISTENT: no
HEALTH: OK
</screen>
<para>
Example: ctdb getdbstatus registry.tdb (with a corrupted TDB)
</para>
<para>
Example output:
</para>
<screen format="linespecific">
dbid: 0xf2a58948
name: registry.tdb
path: /var/ctdb/persistent/registry.tdb.0
PERSISTENT: yes
HEALTH: NO-HEALTHY-NODES - ERROR - Backup of corrupted TDB in '/var/ctdb/persistent/registry.tdb.0.corrupted.20091208091949.0Z'
</screen>
</refsect2>
<refsect2><title>catdb &lt;dbname&gt;</title>
@ -960,6 +1053,13 @@ dbid:0x7bbbd26c name:passdb.tdb path:/var/ctdb/persistent/passdb.tdb.0 PERSISTEN
</para>
</refsect2>
<refsect2><title>dumpdbbackup &lt;backup-file&gt;</title>
<para>
This command will dump the content of database backup to the screen
(similar to ctdb catdb). This is a debugging command.
</para>
</refsect2>
<refsect2><title>getmonmode</title>
<para>
This command returns the monutoring mode of a node. The monitoring mode is either ACTIVE or DISABLED. Normally a node will continously monitor that all other nodes that are expected are in fact connected and that they respond to commands.

View File

@ -1,13 +1,13 @@
'\" t
.\" Title: onnode
.\" Author: [FIXME: author] [see http://docbook.sf.net/el/author]
.\" Generator: DocBook XSL Stylesheets v1.75.2 <http://docbook.sf.net/>
.\" Date: 10/22/2009
.\" Manual: [FIXME: manual]
.\" Source: [FIXME: source]
.\" Generator: DocBook XSL Stylesheets v1.75.1 <http://docbook.sf.net/>
.\" Date: 12/08/2009
.\" Manual:
.\" Source:
.\" Language: English
.\"
.TH "ONNODE" "1" "10/22/2009" "[FIXME: source]" "[FIXME: manual]"
.TH "ONNODE" "1" "12/08/2009" "" ""
.\" -----------------------------------------------------------------
.\" * set default formatting
.\" -----------------------------------------------------------------

View File

@ -1,4 +1,4 @@
<html><head><meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"><title>onnode</title><meta name="generator" content="DocBook XSL Stylesheets V1.75.2"></head><body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF"><div class="refentry" title="onnode"><a name="onnode.1"></a><div class="titlepage"></div><div class="refnamediv"><h2>Name</h2><p>onnode &#8212; run commands on ctdb nodes</p></div><div class="refsynopsisdiv" title="Synopsis"><h2>Synopsis</h2><div class="cmdsynopsis"><p><code class="command">onnode [OPTION] ... NODES COMMAND ...</code> </p></div></div><div class="refsect1" title="DESCRIPTION"><a name="id2777487"></a><h2>DESCRIPTION</h2><p>
<html><head><meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"><title>onnode</title><meta name="generator" content="DocBook XSL Stylesheets V1.75.1"></head><body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF"><div class="refentry" title="onnode"><a name="onnode.1"></a><div class="titlepage"></div><div class="refnamediv"><h2>Name</h2><p>onnode &#8212; run commands on ctdb nodes</p></div><div class="refsynopsisdiv" title="Synopsis"><h2>Synopsis</h2><div class="cmdsynopsis"><p><code class="command">onnode [OPTION] ... NODES COMMAND ...</code> </p></div></div><div class="refsect1" title="DESCRIPTION"><a name="id268574"></a><h2>DESCRIPTION</h2><p>
onnode is a utility to run commands on a specific node of a CTDB
cluster, or on all nodes.
</p><p>
@ -13,7 +13,7 @@
</p><p>
The COMMAND can be any shell command. The onnode utility uses
ssh or rsh to connect to the remote nodes and run the command.
</p></div><div class="refsect1" title="DESCRIPTIVE NODE SPECIFICATIONS"><a name="id2739103"></a><h2>DESCRIPTIVE NODE SPECIFICATIONS</h2><p>
</p></div><div class="refsect1" title="DESCRIPTIVE NODE SPECIFICATIONS"><a name="id268596"></a><h2>DESCRIPTIVE NODE SPECIFICATIONS</h2><p>
The following descriptive node specification can be used in
place of numeric node numbers:
</p><div class="variablelist"><dl><dt><span class="term">all</span></dt><dd><p>
@ -32,7 +32,7 @@
The current NAT gateway.
</p></dd><dt><span class="term">rm | recmaster</span></dt><dd><p>
The current recovery master.
</p></dd></dl></div></div><div class="refsect1" title="OPTIONS"><a name="id2742732"></a><h2>OPTIONS</h2><div class="variablelist"><dl><dt><span class="term">-c</span></dt><dd><p>
</p></dd></dl></div></div><div class="refsect1" title="OPTIONS"><a name="id310004"></a><h2>OPTIONS</h2><div class="variablelist"><dl><dt><span class="term">-c</span></dt><dd><p>
Execute COMMAND in the current working directory on the
specified nodes.
</p></dd><dt><span class="term">-o &lt;prefix&gt;</span></dt><dd><p>
@ -60,7 +60,7 @@
more than one node is specified.
</p></dd><dt><span class="term">-h, --help</span></dt><dd><p>
Show a short usage guide.
</p></dd></dl></div></div><div class="refsect1" title="EXAMPLES"><a name="id2742865"></a><h2>EXAMPLES</h2><p>
</p></dd></dl></div></div><div class="refsect1" title="EXAMPLES"><a name="id310128"></a><h2>EXAMPLES</h2><p>
The following command would show the process ID of ctdb on all nodes
</p><pre class="screen">
onnode all pidof ctdbd
@ -78,10 +78,10 @@
directory, in parallel, on nodes 0, 2, 3 and 4.
</p><pre class="screen">
onnode -c -p 0,2-4 ./foo
</pre></div><div class="refsect1" title="ENVIRONMENT"><a name="id2742916"></a><h2>ENVIRONMENT</h2><div class="variablelist"><dl><dt><span class="term"><code class="envar">CTDB_NODES_FILE</code></span></dt><dd><p>
</pre></div><div class="refsect1" title="ENVIRONMENT"><a name="id310172"></a><h2>ENVIRONMENT</h2><div class="variablelist"><dl><dt><span class="term"><code class="envar">CTDB_NODES_FILE</code></span></dt><dd><p>
Name of alternative nodes file to use instead of
<code class="filename">/etc/ctdb/nodes</code>.
</p></dd></dl></div></div><div class="refsect1" title="FILES"><a name="id2742944"></a><h2>FILES</h2><div class="variablelist"><dl><dt><span class="term"><code class="filename">/etc/ctdb/nodes</code></span></dt><dd><p>
</p></dd></dl></div></div><div class="refsect1" title="FILES"><a name="id310200"></a><h2>FILES</h2><div class="variablelist"><dl><dt><span class="term"><code class="filename">/etc/ctdb/nodes</code></span></dt><dd><p>
Default file containing a list of each node's IP address
or hostname.
</p></dd><dt><span class="term"><code class="filename">/etc/ctdb/onnode.conf</code></span></dt><dd><p>
@ -90,9 +90,9 @@
something other than "ssh". In this case the -t option is
ignored. For example, the administrator may choose to use
use rsh instead of ssh.
</p></dd></dl></div></div><div class="refsect1" title="SEE ALSO"><a name="id2791501"></a><h2>SEE ALSO</h2><p>
</p></dd></dl></div></div><div class="refsect1" title="SEE ALSO"><a name="id310243"></a><h2>SEE ALSO</h2><p>
ctdbd(1), ctdb(1), <a class="ulink" href="http://ctdb.samba.org/" target="_top">http://ctdb.samba.org/</a>
</p></div><div class="refsect1" title="COPYRIGHT/LICENSE"><a name="id2791514"></a><h2>COPYRIGHT/LICENSE</h2><div class="literallayout"><p><br>
</p></div><div class="refsect1" title="COPYRIGHT/LICENSE"><a name="id310256"></a><h2>COPYRIGHT/LICENSE</h2><div class="literallayout"><p><br>
Copyright (C) Andrew Tridgell 2007<br>
Copyright (C) Ronnie sahlberg 2007<br>
Copyright (C) Martin Schwenke 2008<br>

View File

@ -5,6 +5,8 @@
<refmeta>
<refentrytitle>onnode</refentrytitle>
<manvolnum>1</manvolnum>
<refmiscinfo class="source"> </refmiscinfo>
<refmiscinfo class="manual"> </refmiscinfo>
</refmeta>

View File

@ -178,6 +178,7 @@ int ctdb_set_transport(struct ctdb_context *ctdb, const char *transport);
*/
int ctdb_set_tdb_dir(struct ctdb_context *ctdb, const char *dir);
int ctdb_set_tdb_dir_persistent(struct ctdb_context *ctdb, const char *dir);
int ctdb_set_tdb_dir_state(struct ctdb_context *ctdb, const char *dir);
/*
set some flags
@ -384,6 +385,11 @@ int ctdb_ctrl_copydb(struct ctdb_context *ctdb,
int ctdb_ctrl_getdbpath(struct ctdb_context *ctdb, struct timeval timeout, uint32_t destnode, uint32_t dbid, TALLOC_CTX *mem_ctx, const char **path);
int ctdb_ctrl_getdbname(struct ctdb_context *ctdb, struct timeval timeout, uint32_t destnode, uint32_t dbid, TALLOC_CTX *mem_ctx, const char **name);
int ctdb_ctrl_getdbhealth(struct ctdb_context *ctdb,
struct timeval timeout,
uint32_t destnode,
uint32_t dbid, TALLOC_CTX *mem_ctx,
const char **reason);
int ctdb_ctrl_createdb(struct ctdb_context *ctdb, struct timeval timeout, uint32_t destnode, TALLOC_CTX *mem_ctx, const char *name, bool persistent);
int ctdb_ctrl_process_exists(struct ctdb_context *ctdb, uint32_t destnode, pid_t pid);
@ -468,6 +474,7 @@ int ctdb_set_logfile(struct ctdb_context *ctdb, const char *logfile, bool use_sy
typedef int (*ctdb_traverse_func)(struct ctdb_context *, TDB_DATA, TDB_DATA, void *);
int ctdb_traverse(struct ctdb_db_context *ctdb_db, ctdb_traverse_func fn, void *private_data);
int ctdb_dumpdb_record(struct ctdb_context *ctdb, TDB_DATA key, TDB_DATA data, void *p);
int ctdb_dump_db(struct ctdb_db_context *ctdb_db, FILE *f);
/*

View File

@ -129,6 +129,7 @@ struct ctdb_tunable {
uint32_t vacuum_max_interval;
uint32_t max_queue_depth_drop_msg;
uint32_t use_status_events_for_monitoring;
uint32_t allow_unhealthy_db_read;
};
/*
@ -405,6 +406,11 @@ struct ctdb_context {
const char *name;
const char *db_directory;
const char *db_directory_persistent;
const char *db_directory_state;
struct tdb_wrap *db_persistent_health;
uint32_t db_persistent_startup_generation;
uint64_t db_persistent_check_errors;
uint64_t max_persistent_check_errors;
const char *transport;
char *recovery_lock_file;
int recovery_lock_fd;
@ -477,6 +483,7 @@ struct ctdb_db_context {
struct ctdb_traverse_local_handle *traverse;
bool transaction_active;
struct ctdb_vacuum_handle *vacuum_handle;
char *unhealthy_reason;
};
@ -626,6 +633,8 @@ enum ctdb_controls {CTDB_CONTROL_PROCESS_EXISTS = 0,
CTDB_CONTROL_CLEAR_LOG = 118,
CTDB_CONTROL_TRANS3_COMMIT = 119,
CTDB_CONTROL_GET_DB_SEQNUM = 120,
CTDB_CONTROL_DB_SET_HEALTHY = 121,
CTDB_CONTROL_DB_GET_HEALTH = 122,
};
/*
@ -1415,7 +1424,7 @@ int32_t ctdb_control_get_server_id_list(struct ctdb_context *ctdb,
int32_t ctdb_control_uptime(struct ctdb_context *ctdb,
TDB_DATA *outdata);
int ctdb_attach_persistent(struct ctdb_context *ctdb);
int ctdb_attach_databases(struct ctdb_context *ctdb);
int32_t ctdb_control_persistent_store(struct ctdb_context *ctdb,
struct ctdb_req_control *c,
@ -1435,6 +1444,10 @@ int32_t ctdb_control_transaction_start(struct ctdb_context *ctdb, uint32_t id);
int32_t ctdb_control_transaction_commit(struct ctdb_context *ctdb, uint32_t id);
int32_t ctdb_control_transaction_cancel(struct ctdb_context *ctdb);
int32_t ctdb_control_wipe_database(struct ctdb_context *ctdb, TDB_DATA indata);
int32_t ctdb_control_db_set_healthy(struct ctdb_context *ctdb, TDB_DATA indata);
int32_t ctdb_control_db_get_health(struct ctdb_context *ctdb,
TDB_DATA indata,
TDB_DATA *outdata);
int ctdb_vacuum(struct ctdb_context *ctdb, int argc, const char **argv);
@ -1541,4 +1554,12 @@ int32_t ctdb_control_get_db_seqnum(struct ctdb_context *ctdb,
TDB_DATA indata,
TDB_DATA *outdata);
int ctdb_load_persistent_health(struct ctdb_context *ctdb,
struct ctdb_db_context *ctdb_db);
int ctdb_update_persistent_health(struct ctdb_context *ctdb,
struct ctdb_db_context *ctdb_db,
const char *reason,/* NULL means healthy */
int num_healthy_nodes);
int ctdb_recheck_persistent_health(struct ctdb_context *ctdb);
#endif

View File

@ -12,6 +12,8 @@ libdir = @libdir@
VPATH = @srcdir@:@libreplacedir@
srcdir = @srcdir@
builddir = @builddir@
sharedbuilddir = @sharedbuilddir@
INSTALLCMD = @INSTALL@
CPPFLAGS = @CPPFLAGS@ -I$(srcdir)/include -Iinclude
CFLAGS = $(CPPFLAGS) @CFLAGS@
LDFLAGS = @LDFLAGS@
@ -21,113 +23,52 @@ SHLD_FLAGS = @SHLD_FLAGS@
PACKAGE_VERSION = @PACKAGE_VERSION@
PICFLAG = @PICFLAG@
SHLIBEXT = @SHLIBEXT@
SWIG = swig
.PHONY: test
PROGS = bin/tdbtool$(EXEEXT) bin/tdbdump$(EXEEXT) bin/tdbbackup$(EXEEXT)
PROGS_NOINSTALL = bin/tdbtest$(EXEEXT) bin/tdbtorture$(EXEEXT)
ALL_PROGS = $(PROGS) $(PROGS_NOINSTALL)
PYTHON = @PYTHON@
PYTHON_CONFIG = @PYTHON_CONFIG@
PYTHON_BUILD_TARGET = @PYTHON_BUILD_TARGET@
PYTHON_INSTALL_TARGET = @PYTHON_INSTALL_TARGET@
PYTHON_CHECK_TARGET = @PYTHON_CHECK_TARGET@
LIB_PATH_VAR = @LIB_PATH_VAR@
tdbdir = @tdbdir@
TDB_OBJ = @TDB_OBJ@ @LIBREPLACEOBJ@
DIRS = bin common tools
SONAMEFLAG = @SONAMEFLAG@
VERSIONSCRIPT = @VERSIONSCRIPT@
EXPORTSFILE = @EXPORTSFILE@
SONAME = libtdb.$(SHLIBEXT).1
SOLIB = libtdb.$(SHLIBEXT).$(PACKAGE_VERSION)
default: all
all: showflags dirs $(PROGS) $(SOLIB) libtdb.a
include $(tdbdir)/tdb.mk
include $(tdbdir)/rules.mk
showflags:
@echo 'tdb will be compiled with flags:'
@echo ' CFLAGS = $(CFLAGS)'
@echo ' CPPFLAGS = $(CPPFLAGS)'
@echo ' LDFLAGS = $(LDFLAGS)'
@echo ' LIBS = $(LIBS)'
all:: showflags dirs $(PROGS) $(TDB_SOLIB) libtdb.a $(PYTHON_BUILD_TARGET)
.SUFFIXES: .c .o
install:: all
$(TDB_SOLIB): $(TDB_OBJ)
$(SHLD) $(SHLD_FLAGS) -o $@ $(TDB_OBJ) $(VERSIONSCRIPT) $(EXPORTSFILE) $(SONAMEFLAG)$(TDB_SONAME)
.c.o:
@echo Compiling $*.c
@mkdir -p `dirname $@`
@$(CC) $(PICFLAG) $(CFLAGS) -c $< -o $@
shared-build: all
${INSTALLCMD} -d $(sharedbuilddir)/lib
${INSTALLCMD} -m 644 libtdb.a $(sharedbuilddir)/lib
${INSTALLCMD} -m 755 $(TDB_SOLIB) $(sharedbuilddir)/lib
ln -sf $(TDB_SOLIB) $(sharedbuilddir)/lib/$(TDB_SONAME)
ln -sf $(TDB_SOLIB) $(sharedbuilddir)/lib/libtdb.so
${INSTALLCMD} -d $(sharedbuilddir)/include
${INSTALLCMD} -m 644 $(srcdir)/include/tdb.h $(sharedbuilddir)/include
dirs:
@mkdir -p $(DIRS)
check: test
install: all
mkdir -p $(DESTDIR)$(bindir)
mkdir -p $(DESTDIR)$(includedir)
mkdir -p $(DESTDIR)$(libdir)
mkdir -p $(DESTDIR)$(libdir)/pkgconfig
cp $(PROGS) $(DESTDIR)$(bindir)
cp $(srcdir)/include/tdb.h $(DESTDIR)$(includedir)
cp tdb.pc $(DESTDIR)$(libdir)/pkgconfig
cp libtdb.a $(SOLIB) $(DESTDIR)$(libdir)
test:: $(PYTHON_CHECK_TARGET)
installcheck:: test install
libtdb.a: $(TDB_OBJ)
ar -rv libtdb.a $(TDB_OBJ)
clean::
rm -f *.o *.a */*.o
rm -fr abi
libtdb.$(SHLIBEXT): $(SOLIB)
ln -fs $< $@
$(SONAME): $(SOLIB)
ln -fs $< $@
$(SOLIB): $(TDB_OBJ)
$(SHLD) $(SHLD_FLAGS) -o $@ $(TDB_OBJ) @SONAMEFLAG@$(SONAME)
TDB_LIB = libtdb.a
bin/tdbtest$(EXEEXT): tools/tdbtest.o $(TDB_LIB)
$(CC) $(CFLAGS) $(LDFLAGS) -o bin/tdbtest tools/tdbtest.o -L. -ltdb -lgdbm
bin/tdbtool$(EXEEXT): tools/tdbtool.o $(TDB_LIB)
$(CC) $(CFLAGS) $(LDFLAGS) -o bin/tdbtool tools/tdbtool.o -L. -ltdb
bin/tdbtorture$(EXEEXT): tools/tdbtorture.o $(TDB_LIB)
$(CC) $(CFLAGS) $(LDFLAGS) -o bin/tdbtorture tools/tdbtorture.o -L. -ltdb
bin/tdbdump$(EXEEXT): tools/tdbdump.o $(TDB_LIB)
$(CC) $(CFLAGS) $(LDFLAGS) -o bin/tdbdump tools/tdbdump.o -L. -ltdb
bin/tdbbackup$(EXEEXT): tools/tdbbackup.o $(TDB_LIB)
$(CC) $(CFLAGS) $(LDFLAGS) -o bin/tdbbackup tools/tdbbackup.o -L. -ltdb
test: bin/tdbtorture$(EXEEXT)
bin/tdbtorture$(EXEEXT)
installcheck: test install
clean:
rm -f $(ALL_PROGS) *.o *.a common/*.o tools/*.o tdb.pc
rm -f test.db test.tdb torture.tdb test.gdbm
rm -f $(SONAME) $(SOLIB) libtdb.a libtdb.$(SHLIBEXT)
distclean: clean
rm -f *~ */*~
distclean:: clean
rm -f config.log config.status include/config.h config.cache
rm -f Makefile
realdistclean: distclean
realdistclean:: distclean
rm -f configure include/config.h.in
tdb_wrap.c tdb.py: tdb.i
$(SWIG) -O -Wall -python -keyword tdb.i
build-python: libtdb.$(SHLIBEXT) tdb_wrap.c tdb.py
./setup.py build
install-python:
./setup.py install --prefix=$(prefix)
check-python: build-python
# FIXME: Should be more portable:
LD_LIBRARY_PATH=. PYTHONPATH=.:build/lib.linux-i686-2.4 trial python/tests/simple.py
install-swig:
mkdir -p $(DESTDIR)`$(SWIG) -swiglib`
cp tdb.i $(DESTDIR)`$(SWIG) -swiglib`
clean-python:
./setup.py clean

View File

@ -9,8 +9,6 @@ autoheader $IPATHS || exit 1
rm -rf autom4te.cache
swig -O -Wall -python -keyword tdb.i # Ignore errors for now
echo "Now run ./configure and then make."
exit 0

View File

@ -0,0 +1,14 @@
AC_DEFUN(BUILD_WITH_SHARED_BUILD_DIR,
[ AC_ARG_WITH([shared-build-dir],
[AC_HELP_STRING([--with-shared-build-dir=DIR],
[temporary build directory where libraries are installed [$srcdir/sharedbuild]])])
sharedbuilddir="$srcdir/sharedbuild"
if test x"$with_shared_build_dir" != x; then
sharedbuilddir=$with_shared_build_dir
CFLAGS="$CFLAGS -I$with_shared_build_dir/include"
LDFLAGS="$LDFLAGS -L$with_shared_build_dir/lib"
fi
AC_SUBST(sharedbuilddir)
])

423
ctdb/lib/tdb/common/check.c Normal file
View File

@ -0,0 +1,423 @@
/*
Unix SMB/CIFS implementation.
trivial database library
Copyright (C) Rusty Russell 2009
** NOTE! The following LGPL license applies to the tdb
** library. This does NOT imply that all of Samba is released
** under the LGPL
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 3 of the License, or (at your option) any later version.
This library 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
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, see <http://www.gnu.org/licenses/>.
*/
#include "tdb_private.h"
/* Since we opened it, these shouldn't fail unless it's recent corruption. */
static bool tdb_check_header(struct tdb_context *tdb, tdb_off_t *recovery)
{
struct tdb_header hdr;
if (tdb->methods->tdb_read(tdb, 0, &hdr, sizeof(hdr), DOCONV()) == -1)
return false;
if (strcmp(hdr.magic_food, TDB_MAGIC_FOOD) != 0)
goto corrupt;
CONVERT(hdr);
if (hdr.version != TDB_VERSION)
goto corrupt;
if (hdr.rwlocks != 0)
goto corrupt;
if (hdr.hash_size == 0)
goto corrupt;
if (hdr.hash_size != tdb->header.hash_size)
goto corrupt;
if (hdr.recovery_start != 0 &&
hdr.recovery_start < TDB_DATA_START(tdb->header.hash_size))
goto corrupt;
*recovery = hdr.recovery_start;
return true;
corrupt:
tdb->ecode = TDB_ERR_CORRUPT;
TDB_LOG((tdb, TDB_DEBUG_ERROR, "Header is corrupt\n"));
return false;
}
/* Generic record header check. */
static bool tdb_check_record(struct tdb_context *tdb,
tdb_off_t off,
const struct tdb_record *rec)
{
tdb_off_t tailer;
/* Check rec->next: 0 or points to record offset, aligned. */
if (rec->next > 0 && rec->next < TDB_DATA_START(tdb->header.hash_size)){
TDB_LOG((tdb, TDB_DEBUG_ERROR,
"Record offset %d too small next %d\n",
off, rec->next));
goto corrupt;
}
if (rec->next + sizeof(*rec) < rec->next) {
TDB_LOG((tdb, TDB_DEBUG_ERROR,
"Record offset %d too large next %d\n",
off, rec->next));
goto corrupt;
}
if ((rec->next % TDB_ALIGNMENT) != 0) {
TDB_LOG((tdb, TDB_DEBUG_ERROR,
"Record offset %d misaligned next %d\n",
off, rec->next));
goto corrupt;
}
if (tdb->methods->tdb_oob(tdb, rec->next+sizeof(*rec), 0))
goto corrupt;
/* Check rec_len: similar to rec->next, implies next record. */
if ((rec->rec_len % TDB_ALIGNMENT) != 0) {
TDB_LOG((tdb, TDB_DEBUG_ERROR,
"Record offset %d misaligned length %d\n",
off, rec->rec_len));
goto corrupt;
}
/* Must fit tailer. */
if (rec->rec_len < sizeof(tailer)) {
TDB_LOG((tdb, TDB_DEBUG_ERROR,
"Record offset %d too short length %d\n",
off, rec->rec_len));
goto corrupt;
}
/* OOB allows "right at the end" access, so this works for last rec. */
if (tdb->methods->tdb_oob(tdb, off+sizeof(*rec)+rec->rec_len, 0))
goto corrupt;
/* Check tailer. */
if (tdb_ofs_read(tdb, off+sizeof(*rec)+rec->rec_len-sizeof(tailer),
&tailer) == -1)
goto corrupt;
if (tailer != sizeof(*rec) + rec->rec_len) {
TDB_LOG((tdb, TDB_DEBUG_ERROR,
"Record offset %d invalid tailer\n", off));
goto corrupt;
}
return true;
corrupt:
tdb->ecode = TDB_ERR_CORRUPT;
return false;
}
/* Grab some bytes: may copy if can't use mmap.
Caller has already done bounds check. */
static TDB_DATA get_bytes(struct tdb_context *tdb,
tdb_off_t off, tdb_len_t len)
{
TDB_DATA d;
d.dsize = len;
if (tdb->transaction == NULL && tdb->map_ptr != NULL)
d.dptr = (unsigned char *)tdb->map_ptr + off;
else
d.dptr = tdb_alloc_read(tdb, off, d.dsize);
return d;
}
/* Frees data if we're not able to simply use mmap. */
static void put_bytes(struct tdb_context *tdb, TDB_DATA d)
{
if (tdb->transaction == NULL && tdb->map_ptr != NULL)
return;
free(d.dptr);
}
/* We use the excellent Jenkins lookup3 hash; this is based on hash_word2.
* See: http://burtleburtle.net/bob/c/lookup3.c
*/
#define rot(x,k) (((x)<<(k)) | ((x)>>(32-(k))))
static void hash(uint32_t key, uint32_t *pc, uint32_t *pb)
{
uint32_t a,b,c;
/* Set up the internal state */
a = b = c = 0xdeadbeef + *pc;
c += *pb;
a += key;
c ^= b; c -= rot(b,14);
a ^= c; a -= rot(c,11);
b ^= a; b -= rot(a,25);
c ^= b; c -= rot(b,16);
a ^= c; a -= rot(c,4);
b ^= a; b -= rot(a,14);
c ^= b; c -= rot(b,24);
*pc=c; *pb=b;
}
/*
We want to check that all free records are in the free list
(only once), and all free list entries are free records. Similarly
for each hash chain of used records.
Doing that naively (without walking hash chains, since we want to be
linear) means keeping a list of records which have been seen in each
hash chain, and another of records pointed to (ie. next pointers
from records and the initial hash chain heads). These two lists
should be equal. This will take 8 bytes per record, and require
sorting at the end.
So instead, we record each offset in a bitmap such a way that
recording it twice will cancel out. Since each offset should appear
exactly twice, the bitmap should be zero at the end.
The approach was inspired by Bloom Filters (see Wikipedia). For
each value, we flip K bits in a bitmap of size N. The number of
distinct arrangements is:
N! / (K! * (N-K)!)
Of course, not all arrangements are actually distinct, but testing
shows this formula to be close enough.
So, if K == 8 and N == 256, the probability of two things flipping the same
bits is 1 in 409,663,695,276,000.
Given that ldb uses a hash size of 10000, using 32 bytes per hash chain
(320k) seems reasonable.
*/
#define NUM_HASHES 8
#define BITMAP_BITS 256
static void bit_flip(unsigned char bits[], unsigned int idx)
{
bits[idx / CHAR_BIT] ^= (1 << (idx % CHAR_BIT));
}
/* We record offsets in a bitmap for the particular chain it should be in. */
static void record_offset(unsigned char bits[], tdb_off_t off)
{
uint32_t h1 = off, h2 = 0;
unsigned int i;
/* We get two good hash values out of jhash2, so we use both. Then
* we keep going to produce further hash values. */
for (i = 0; i < NUM_HASHES / 2; i++) {
hash(off, &h1, &h2);
bit_flip(bits, h1 % BITMAP_BITS);
bit_flip(bits, h2 % BITMAP_BITS);
h2++;
}
}
/* Check that an in-use record is valid. */
static bool tdb_check_used_record(struct tdb_context *tdb,
tdb_off_t off,
const struct tdb_record *rec,
unsigned char **hashes,
int (*check)(TDB_DATA, TDB_DATA, void *),
void *private_data)
{
TDB_DATA key, data;
if (!tdb_check_record(tdb, off, rec))
return false;
/* key + data + tailer must fit in record */
if (rec->key_len + rec->data_len + sizeof(tdb_off_t) > rec->rec_len) {
TDB_LOG((tdb, TDB_DEBUG_ERROR,
"Record offset %d too short for contents\n", off));
return false;
}
key = get_bytes(tdb, off + sizeof(*rec), rec->key_len);
if (!key.dptr)
return false;
if (tdb->hash_fn(&key) != rec->full_hash) {
TDB_LOG((tdb, TDB_DEBUG_ERROR,
"Record offset %d has incorrect hash\n", off));
goto fail_put_key;
}
/* Mark this offset as a known value for this hash bucket. */
record_offset(hashes[BUCKET(rec->full_hash)+1], off);
/* And similarly if the next pointer is valid. */
if (rec->next)
record_offset(hashes[BUCKET(rec->full_hash)+1], rec->next);
/* If they supply a check function and this record isn't dead,
get data and feed it. */
if (check && rec->magic != TDB_DEAD_MAGIC) {
data = get_bytes(tdb, off + sizeof(*rec) + rec->key_len,
rec->data_len);
if (!data.dptr)
goto fail_put_key;
if (check(key, data, private_data) == -1)
goto fail_put_data;
put_bytes(tdb, data);
}
put_bytes(tdb, key);
return true;
fail_put_data:
put_bytes(tdb, data);
fail_put_key:
put_bytes(tdb, key);
return false;
}
/* Check that an unused record is valid. */
static bool tdb_check_free_record(struct tdb_context *tdb,
tdb_off_t off,
const struct tdb_record *rec,
unsigned char **hashes)
{
if (!tdb_check_record(tdb, off, rec))
return false;
/* Mark this offset as a known value for the free list. */
record_offset(hashes[0], off);
/* And similarly if the next pointer is valid. */
if (rec->next)
record_offset(hashes[0], rec->next);
return true;
}
int tdb_check(struct tdb_context *tdb,
int (*check)(TDB_DATA key, TDB_DATA data, void *private_data),
void *private_data)
{
unsigned int h;
unsigned char **hashes;
tdb_off_t off, recovery_start;
struct tdb_record rec;
bool found_recovery = false;
if (tdb_lockall(tdb) == -1)
return -1;
/* Make sure we know true size of the underlying file. */
tdb->methods->tdb_oob(tdb, tdb->map_size + 1, 1);
/* Header must be OK: also gets us the recovery ptr, if any. */
if (!tdb_check_header(tdb, &recovery_start))
goto unlock;
/* We should have the whole header, too. */
if (tdb->map_size < TDB_DATA_START(tdb->header.hash_size)) {
tdb->ecode = TDB_ERR_CORRUPT;
TDB_LOG((tdb, TDB_DEBUG_ERROR, "File too short for hashes\n"));
goto unlock;
}
/* One big malloc: pointers then bit arrays. */
hashes = (unsigned char **)calloc(
1, sizeof(hashes[0]) * (1+tdb->header.hash_size)
+ BITMAP_BITS / CHAR_BIT * (1+tdb->header.hash_size));
if (!hashes) {
tdb->ecode = TDB_ERR_OOM;
goto unlock;
}
/* Initialize pointers */
hashes[0] = (unsigned char *)(&hashes[1+tdb->header.hash_size]);
for (h = 1; h < 1+tdb->header.hash_size; h++)
hashes[h] = hashes[h-1] + BITMAP_BITS / CHAR_BIT;
/* Freelist and hash headers are all in a row: read them. */
for (h = 0; h < 1+tdb->header.hash_size; h++) {
if (tdb_ofs_read(tdb, FREELIST_TOP + h*sizeof(tdb_off_t),
&off) == -1)
goto free;
if (off)
record_offset(hashes[h], off);
}
/* For each record, read it in and check it's ok. */
for (off = TDB_DATA_START(tdb->header.hash_size);
off < tdb->map_size;
off += sizeof(rec) + rec.rec_len) {
if (tdb->methods->tdb_read(tdb, off, &rec, sizeof(rec),
DOCONV()) == -1)
goto free;
switch (rec.magic) {
case TDB_MAGIC:
case TDB_DEAD_MAGIC:
if (!tdb_check_used_record(tdb, off, &rec, hashes,
check, private_data))
goto free;
break;
case TDB_FREE_MAGIC:
if (!tdb_check_free_record(tdb, off, &rec, hashes))
goto free;
break;
case TDB_RECOVERY_MAGIC:
case 0: /* Used for invalid (or in-progress) recovery area. */
if (recovery_start != off) {
TDB_LOG((tdb, TDB_DEBUG_ERROR,
"Unexpected recovery record at offset %d\n",
off));
goto free;
}
found_recovery = true;
break;
default:
tdb->ecode = TDB_ERR_CORRUPT;
TDB_LOG((tdb, TDB_DEBUG_ERROR,
"Bad magic 0x%x at offset %d\n",
rec.magic, off));
goto free;
}
}
/* Now, hashes should all be empty: each record exists and is referred
* to by one other. */
for (h = 0; h < 1+tdb->header.hash_size; h++) {
unsigned int i;
for (i = 0; i < BITMAP_BITS / CHAR_BIT; i++) {
if (hashes[h][i] != 0) {
tdb->ecode = TDB_ERR_CORRUPT;
TDB_LOG((tdb, TDB_DEBUG_ERROR,
"Hashes do not match records\n"));
goto free;
}
}
}
/* We must have found recovery area if there was one. */
if (recovery_start != 0 && !found_recovery) {
TDB_LOG((tdb, TDB_DEBUG_ERROR,
"Expected %s recovery area, got %s\n",
recovery_start ? "a" : "no",
found_recovery ? "one" : "none"));
goto free;
}
free(hashes);
tdb_unlockall(tdb);
return 0;
free:
free(hashes);
unlock:
tdb_unlockall(tdb);
return -1;
}

View File

@ -30,7 +30,7 @@
static tdb_off_t tdb_dump_record(struct tdb_context *tdb, int hash,
tdb_off_t offset)
{
struct list_struct rec;
struct tdb_record rec;
tdb_off_t tailer_ofs, tailer;
if (tdb->methods->tdb_read(tdb, offset, (char *)&rec,
@ -95,7 +95,7 @@ int tdb_printfreelist(struct tdb_context *tdb)
int ret;
long total_free = 0;
tdb_off_t offset, rec_ptr;
struct list_struct rec;
struct tdb_record rec;
if ((ret = tdb_lock(tdb, -1, F_WRLCK)) != 0)
return ret;

View File

@ -34,7 +34,7 @@
#define USE_RIGHT_MERGES 0
/* read a freelist record and check for simple errors */
int tdb_rec_free_read(struct tdb_context *tdb, tdb_off_t off, struct list_struct *rec)
int tdb_rec_free_read(struct tdb_context *tdb, tdb_off_t off, struct tdb_record *rec)
{
if (tdb->methods->tdb_read(tdb, off, rec, sizeof(*rec),DOCONV()) == -1)
return -1;
@ -54,7 +54,7 @@ int tdb_rec_free_read(struct tdb_context *tdb, tdb_off_t off, struct list_struct
tdb->ecode = TDB_ERR_CORRUPT;
TDB_LOG((tdb, TDB_DEBUG_WARNING, "tdb_rec_free_read bad magic 0x%x at offset=%d\n",
rec->magic, off));
return TDB_ERRCODE(TDB_ERR_CORRUPT, -1);
return -1;
}
if (tdb->methods->tdb_oob(tdb, rec->next+sizeof(*rec), 0) != 0)
return -1;
@ -78,15 +78,16 @@ static int remove_from_freelist(struct tdb_context *tdb, tdb_off_t off, tdb_off_
/* Follow chain (next offset is at start of record) */
last_ptr = i;
}
tdb->ecode = TDB_ERR_CORRUPT;
TDB_LOG((tdb, TDB_DEBUG_FATAL,"remove_from_freelist: not on list at off=%d\n", off));
return TDB_ERRCODE(TDB_ERR_CORRUPT, -1);
return -1;
}
#endif
/* update a record tailer (must hold allocation lock) */
static int update_tailer(struct tdb_context *tdb, tdb_off_t offset,
const struct list_struct *rec)
const struct tdb_record *rec)
{
tdb_off_t totalsize;
@ -98,7 +99,7 @@ static int update_tailer(struct tdb_context *tdb, tdb_off_t offset,
/* Add an element into the freelist. Merge adjacent records if
neccessary. */
int tdb_free(struct tdb_context *tdb, tdb_off_t offset, struct list_struct *rec)
int tdb_free(struct tdb_context *tdb, tdb_off_t offset, struct tdb_record *rec)
{
/* Allocation and tailer lock */
if (tdb_lock(tdb, -1, F_WRLCK) != 0)
@ -114,7 +115,7 @@ int tdb_free(struct tdb_context *tdb, tdb_off_t offset, struct list_struct *rec)
/* Look right first (I'm an Australian, dammit) */
if (offset + sizeof(*rec) + rec->rec_len + sizeof(*rec) <= tdb->map_size) {
tdb_off_t right = offset + sizeof(*rec) + rec->rec_len;
struct list_struct r;
struct tdb_record r;
if (tdb->methods->tdb_read(tdb, right, &r, sizeof(r), DOCONV()) == -1) {
TDB_LOG((tdb, TDB_DEBUG_FATAL, "tdb_free: right read failed at %u\n", right));
@ -140,7 +141,7 @@ left:
/* Look left */
if (offset - sizeof(tdb_off_t) > TDB_DATA_START(tdb->header.hash_size)) {
tdb_off_t left = offset - sizeof(tdb_off_t);
struct list_struct l;
struct tdb_record l;
tdb_off_t leftsize;
/* Read in tailer and jump back to header */
@ -219,9 +220,9 @@ update:
*/
static tdb_off_t tdb_allocate_ofs(struct tdb_context *tdb,
tdb_len_t length, tdb_off_t rec_ptr,
struct list_struct *rec, tdb_off_t last_ptr)
struct tdb_record *rec, tdb_off_t last_ptr)
{
#define MIN_REC_SIZE (sizeof(struct list_struct) + sizeof(tdb_off_t) + 8)
#define MIN_REC_SIZE (sizeof(struct tdb_record) + sizeof(tdb_off_t) + 8)
if (rec->rec_len < length + MIN_REC_SIZE) {
/* we have to grab the whole record */
@ -267,12 +268,12 @@ static tdb_off_t tdb_allocate_ofs(struct tdb_context *tdb,
}
/* allocate some space from the free list. The offset returned points
to a unconnected list_struct within the database with room for at
to a unconnected tdb_record within the database with room for at
least length bytes of total data
0 is returned if the space could not be allocated
*/
tdb_off_t tdb_allocate(struct tdb_context *tdb, tdb_len_t length, struct list_struct *rec)
tdb_off_t tdb_allocate(struct tdb_context *tdb, tdb_len_t length, struct tdb_record *rec)
{
tdb_off_t rec_ptr, last_ptr, newrec_ptr;
struct {

View File

@ -46,7 +46,7 @@ static int seen_insert(struct tdb_context *mem_tdb, tdb_off_t rec_ptr)
int tdb_validate_freelist(struct tdb_context *tdb, int *pnum_entries)
{
struct tdb_context *mem_tdb = NULL;
struct list_struct rec;
struct tdb_record rec;
tdb_off_t rec_ptr, last_ptr;
int ret = -1;
@ -67,7 +67,8 @@ int tdb_validate_freelist(struct tdb_context *tdb, int *pnum_entries)
/* Store the FREELIST_TOP record. */
if (seen_insert(mem_tdb, last_ptr) == -1) {
ret = TDB_ERRCODE(TDB_ERR_CORRUPT, -1);
tdb->ecode = TDB_ERR_CORRUPT;
ret = -1;
goto fail;
}
@ -83,7 +84,8 @@ int tdb_validate_freelist(struct tdb_context *tdb, int *pnum_entries)
be corrupt. */
if (seen_insert(mem_tdb, rec_ptr)) {
ret = TDB_ERRCODE(TDB_ERR_CORRUPT, -1);
tdb->ecode = TDB_ERR_CORRUPT;
ret = -1;
goto fail;
}

View File

@ -45,11 +45,12 @@ static int tdb_oob(struct tdb_context *tdb, tdb_off_t len, int probe)
TDB_LOG((tdb, TDB_DEBUG_FATAL,"tdb_oob len %d beyond internal malloc size %d\n",
(int)len, (int)tdb->map_size));
}
return TDB_ERRCODE(TDB_ERR_IO, -1);
return -1;
}
if (fstat(tdb->fd, &st) == -1) {
return TDB_ERRCODE(TDB_ERR_IO, -1);
tdb->ecode = TDB_ERR_IO;
return -1;
}
if (st.st_size < (size_t)len) {
@ -59,12 +60,14 @@ static int tdb_oob(struct tdb_context *tdb, tdb_off_t len, int probe)
TDB_LOG((tdb, TDB_DEBUG_FATAL,"tdb_oob len %d beyond eof at %d\n",
(int)len, (int)st.st_size));
}
return TDB_ERRCODE(TDB_ERR_IO, -1);
return -1;
}
/* Unmap, update size, remap */
if (tdb_munmap(tdb) == -1)
return TDB_ERRCODE(TDB_ERR_IO, -1);
if (tdb_munmap(tdb) == -1) {
tdb->ecode = TDB_ERR_IO;
return -1;
}
tdb->map_size = st.st_size;
tdb_mmap(tdb);
return 0;
@ -92,27 +95,27 @@ static int tdb_write(struct tdb_context *tdb, tdb_off_t off,
ssize_t written = pwrite(tdb->fd, buf, len, off);
if ((written != (ssize_t)len) && (written != -1)) {
/* try once more */
tdb->ecode = TDB_ERR_IO;
TDB_LOG((tdb, TDB_DEBUG_FATAL, "tdb_write: wrote only "
"%d of %d bytes at %d, trying once more\n",
(int)written, len, off));
errno = ENOSPC;
written = pwrite(tdb->fd, (const void *)((const char *)buf+written),
written = pwrite(tdb->fd, (const char *)buf+written,
len-written,
off+written);
}
if (written == -1) {
/* Ensure ecode is set for log fn. */
tdb->ecode = TDB_ERR_IO;
/* Ensure ecode is set for log fn. */
tdb->ecode = TDB_ERR_IO;
TDB_LOG((tdb, TDB_DEBUG_FATAL,"tdb_write failed at %d "
"len=%d (%s)\n", off, len, strerror(errno)));
return TDB_ERRCODE(TDB_ERR_IO, -1);
return -1;
} else if (written != (ssize_t)len) {
tdb->ecode = TDB_ERR_IO;
TDB_LOG((tdb, TDB_DEBUG_FATAL, "tdb_write: failed to "
"write %d bytes at %d in two attempts\n",
len, off));
errno = ENOSPC;
return TDB_ERRCODE(TDB_ERR_IO, -1);
}
return -1;
}
}
return 0;
}
@ -146,7 +149,7 @@ static int tdb_read(struct tdb_context *tdb, tdb_off_t off, void *buf,
"len=%d ret=%d (%s) map_size=%d\n",
(int)off, (int)len, (int)ret, strerror(errno),
(int)tdb->map_size));
return TDB_ERRCODE(TDB_ERR_IO, -1);
return -1;
}
}
if (cv) {
@ -189,7 +192,9 @@ int tdb_munmap(struct tdb_context *tdb)
#ifdef HAVE_MMAP
if (tdb->map_ptr) {
int ret = munmap(tdb->map_ptr, tdb->map_size);
int ret;
ret = munmap(tdb->map_ptr, tdb->map_size);
if (ret != 0)
return ret;
}
@ -293,7 +298,7 @@ static int tdb_expand_file(struct tdb_context *tdb, tdb_off_t size, tdb_off_t ad
file and doing the mmap again if necessary */
int tdb_expand(struct tdb_context *tdb, tdb_off_t size)
{
struct list_struct rec;
struct tdb_record rec;
tdb_off_t offset, new_size;
if (tdb_lock(tdb, -1, F_WRLCK) == -1) {
@ -387,7 +392,7 @@ unsigned char *tdb_alloc_read(struct tdb_context *tdb, tdb_off_t offset, tdb_len
tdb->ecode = TDB_ERR_OOM;
TDB_LOG((tdb, TDB_DEBUG_ERROR,"tdb_alloc_read malloc failed len=%d (%s)\n",
len, strerror(errno)));
return TDB_ERRCODE(TDB_ERR_OOM, buf);
return NULL;
}
if (tdb->methods->tdb_read(tdb, offset, buf, len, 0) == -1) {
SAFE_FREE(buf);
@ -431,7 +436,7 @@ int tdb_parse_data(struct tdb_context *tdb, TDB_DATA key,
}
/* read/write a record */
int tdb_rec_read(struct tdb_context *tdb, tdb_off_t offset, struct list_struct *rec)
int tdb_rec_read(struct tdb_context *tdb, tdb_off_t offset, struct tdb_record *rec)
{
if (tdb->methods->tdb_read(tdb, offset, rec, sizeof(*rec),DOCONV()) == -1)
return -1;
@ -439,14 +444,14 @@ int tdb_rec_read(struct tdb_context *tdb, tdb_off_t offset, struct list_struct *
/* Ensure ecode is set for log fn. */
tdb->ecode = TDB_ERR_CORRUPT;
TDB_LOG((tdb, TDB_DEBUG_FATAL,"tdb_rec_read bad magic 0x%x at offset=%d\n", rec->magic, offset));
return TDB_ERRCODE(TDB_ERR_CORRUPT, -1);
return -1;
}
return tdb->methods->tdb_oob(tdb, rec->next+sizeof(*rec), 0);
}
int tdb_rec_write(struct tdb_context *tdb, tdb_off_t offset, struct list_struct *rec)
int tdb_rec_write(struct tdb_context *tdb, tdb_off_t offset, struct tdb_record *rec)
{
struct list_struct r = *rec;
struct tdb_record r = *rec;
return tdb->methods->tdb_write(tdb, offset, CONVERT(r), sizeof(r));
}

View File

@ -75,16 +75,15 @@ int tdb_brlock(struct tdb_context *tdb, tdb_off_t offset,
} while (ret == -1 && errno == EINTR);
if (ret == -1) {
tdb->ecode = TDB_ERR_LOCK;
/* Generic lock error. errno set by fcntl.
* EAGAIN is an expected return from non-blocking
* locks. */
if (!probe && lck_type != F_SETLK) {
/* Ensure error code is set for log fun to examine. */
tdb->ecode = TDB_ERR_LOCK;
TDB_LOG((tdb, TDB_DEBUG_TRACE,"tdb_brlock failed (fd=%d) at offset %d rw_type=%d lck_type=%d len=%d\n",
tdb->fd, offset, rw_type, lck_type, (int)len));
}
return TDB_ERRCODE(TDB_ERR_LOCK, -1);
return -1;
}
return 0;
}
@ -133,10 +132,12 @@ static int _tdb_lock(struct tdb_context *tdb, int list, int ltype, int op)
}
if (tdb->global_lock.count) {
return TDB_ERRCODE(TDB_ERR_LOCK, -1);
tdb->ecode = TDB_ERR_LOCK;
return -1;
}
if (list < -1 || list >= (int)tdb->header.hash_size) {
tdb->ecode = TDB_ERR_LOCK;
TDB_LOG((tdb, TDB_DEBUG_ERROR,"tdb_lock: invalid list %d for ltype=%d\n",
list, ltype));
return -1;
@ -228,7 +229,8 @@ int tdb_unlock(struct tdb_context *tdb, int list, int ltype)
}
if (tdb->global_lock.count) {
return TDB_ERRCODE(TDB_ERR_LOCK, -1);
tdb->ecode = TDB_ERR_LOCK;
return -1;
}
if (tdb->flags & TDB_NOLOCK)
@ -350,8 +352,10 @@ static int _tdb_lockall(struct tdb_context *tdb, int ltype, int op)
ltype &= ~TDB_MARK_LOCK;
/* There are no locks on read-only dbs */
if (tdb->read_only || tdb->traverse_read)
return TDB_ERRCODE(TDB_ERR_LOCK, -1);
if (tdb->read_only || tdb->traverse_read) {
tdb->ecode = TDB_ERR_LOCK;
return -1;
}
if (tdb->global_lock.count && tdb->global_lock.ltype == ltype) {
tdb->global_lock.count++;
@ -360,12 +364,14 @@ static int _tdb_lockall(struct tdb_context *tdb, int ltype, int op)
if (tdb->global_lock.count) {
/* a global lock of a different type exists */
return TDB_ERRCODE(TDB_ERR_LOCK, -1);
tdb->ecode = TDB_ERR_LOCK;
return -1;
}
if (tdb->num_locks != 0) {
/* can't combine global and chain locks */
return TDB_ERRCODE(TDB_ERR_LOCK, -1);
tdb->ecode = TDB_ERR_LOCK;
return -1;
}
if (!mark_lock &&
@ -394,11 +400,13 @@ static int _tdb_unlockall(struct tdb_context *tdb, int ltype)
/* There are no locks on read-only dbs */
if (tdb->read_only || tdb->traverse_read) {
return TDB_ERRCODE(TDB_ERR_LOCK, -1);
tdb->ecode = TDB_ERR_LOCK;
return -1;
}
if (tdb->global_lock.ltype != ltype || tdb->global_lock.count == 0) {
return TDB_ERRCODE(TDB_ERR_LOCK, -1);
tdb->ecode = TDB_ERR_LOCK;
return -1;
}
if (tdb->global_lock.count > 1) {
@ -422,48 +430,58 @@ static int _tdb_unlockall(struct tdb_context *tdb, int ltype)
/* lock entire database with write lock */
int tdb_lockall(struct tdb_context *tdb)
{
tdb_trace(tdb, "tdb_lockall");
return _tdb_lockall(tdb, F_WRLCK, F_SETLKW);
}
/* lock entire database with write lock - mark only */
int tdb_lockall_mark(struct tdb_context *tdb)
{
tdb_trace(tdb, "tdb_lockall_mark");
return _tdb_lockall(tdb, F_WRLCK | TDB_MARK_LOCK, F_SETLKW);
}
/* unlock entire database with write lock - unmark only */
int tdb_lockall_unmark(struct tdb_context *tdb)
{
tdb_trace(tdb, "tdb_lockall_unmark");
return _tdb_unlockall(tdb, F_WRLCK | TDB_MARK_LOCK);
}
/* lock entire database with write lock - nonblocking varient */
int tdb_lockall_nonblock(struct tdb_context *tdb)
{
return _tdb_lockall(tdb, F_WRLCK, F_SETLK);
int ret = _tdb_lockall(tdb, F_WRLCK, F_SETLK);
tdb_trace_ret(tdb, "tdb_lockall_nonblock", ret);
return ret;
}
/* unlock entire database with write lock */
int tdb_unlockall(struct tdb_context *tdb)
{
tdb_trace(tdb, "tdb_unlockall");
return _tdb_unlockall(tdb, F_WRLCK);
}
/* lock entire database with read lock */
int tdb_lockall_read(struct tdb_context *tdb)
{
tdb_trace(tdb, "tdb_lockall_read");
return _tdb_lockall(tdb, F_RDLCK, F_SETLKW);
}
/* lock entire database with read lock - nonblock varient */
int tdb_lockall_read_nonblock(struct tdb_context *tdb)
{
return _tdb_lockall(tdb, F_RDLCK, F_SETLK);
int ret = _tdb_lockall(tdb, F_RDLCK, F_SETLK);
tdb_trace_ret(tdb, "tdb_lockall_read_nonblock", ret);
return ret;
}
/* unlock entire database with read lock */
int tdb_unlockall_read(struct tdb_context *tdb)
{
tdb_trace(tdb, "tdb_unlockall_read");
return _tdb_unlockall(tdb, F_RDLCK);
}
@ -471,7 +489,9 @@ int tdb_unlockall_read(struct tdb_context *tdb)
contention - it cannot guarantee how many records will be locked */
int tdb_chainlock(struct tdb_context *tdb, TDB_DATA key)
{
return tdb_lock(tdb, BUCKET(tdb->hash_fn(&key)), F_WRLCK);
int ret = tdb_lock(tdb, BUCKET(tdb->hash_fn(&key)), F_WRLCK);
tdb_trace_1rec(tdb, "tdb_chainlock", key);
return ret;
}
/* lock/unlock one hash chain, non-blocking. This is meant to be used
@ -479,33 +499,43 @@ int tdb_chainlock(struct tdb_context *tdb, TDB_DATA key)
locked */
int tdb_chainlock_nonblock(struct tdb_context *tdb, TDB_DATA key)
{
return tdb_lock_nonblock(tdb, BUCKET(tdb->hash_fn(&key)), F_WRLCK);
int ret = tdb_lock_nonblock(tdb, BUCKET(tdb->hash_fn(&key)), F_WRLCK);
tdb_trace_1rec_ret(tdb, "tdb_chainlock_nonblock", key, ret);
return ret;
}
/* mark a chain as locked without actually locking it. Warning! use with great caution! */
int tdb_chainlock_mark(struct tdb_context *tdb, TDB_DATA key)
{
return tdb_lock(tdb, BUCKET(tdb->hash_fn(&key)), F_WRLCK | TDB_MARK_LOCK);
int ret = tdb_lock(tdb, BUCKET(tdb->hash_fn(&key)), F_WRLCK | TDB_MARK_LOCK);
tdb_trace_1rec(tdb, "tdb_chainlock_mark", key);
return ret;
}
/* unmark a chain as locked without actually locking it. Warning! use with great caution! */
int tdb_chainlock_unmark(struct tdb_context *tdb, TDB_DATA key)
{
tdb_trace_1rec(tdb, "tdb_chainlock_unmark", key);
return tdb_unlock(tdb, BUCKET(tdb->hash_fn(&key)), F_WRLCK | TDB_MARK_LOCK);
}
int tdb_chainunlock(struct tdb_context *tdb, TDB_DATA key)
{
tdb_trace_1rec(tdb, "tdb_chainunlock", key);
return tdb_unlock(tdb, BUCKET(tdb->hash_fn(&key)), F_WRLCK);
}
int tdb_chainlock_read(struct tdb_context *tdb, TDB_DATA key)
{
return tdb_lock(tdb, BUCKET(tdb->hash_fn(&key)), F_RDLCK);
int ret;
ret = tdb_lock(tdb, BUCKET(tdb->hash_fn(&key)), F_RDLCK);
tdb_trace_1rec(tdb, "tdb_chainlock_read", key);
return ret;
}
int tdb_chainunlock_read(struct tdb_context *tdb, TDB_DATA key)
{
tdb_trace_1rec(tdb, "tdb_chainunlock_read", key);
return tdb_unlock(tdb, BUCKET(tdb->hash_fn(&key)), F_RDLCK);
}

View File

@ -55,8 +55,10 @@ static int tdb_new_database(struct tdb_context *tdb, int hash_size)
/* We make it up in memory, then write it out if not internal */
size = sizeof(struct tdb_header) + (hash_size+1)*sizeof(tdb_off_t);
if (!(newdb = (struct tdb_header *)calloc(size, 1)))
return TDB_ERRCODE(TDB_ERR_OOM, -1);
if (!(newdb = (struct tdb_header *)calloc(size, 1))) {
tdb->ecode = TDB_ERR_OOM;
return -1;
}
/* Fill in the header */
newdb->version = TDB_VERSION;
@ -161,6 +163,9 @@ struct tdb_context *tdb_open_ex(const char *name, int hash_size, int tdb_flags,
}
tdb_io_init(tdb);
tdb->fd = -1;
#ifdef TDB_TRACE
tdb->tracefd = -1;
#endif
tdb->name = NULL;
tdb->map_ptr = NULL;
tdb->flags = tdb_flags;
@ -197,6 +202,23 @@ struct tdb_context *tdb_open_ex(const char *name, int hash_size, int tdb_flags,
tdb->flags &= ~TDB_CLEAR_IF_FIRST;
}
if ((tdb->flags & TDB_ALLOW_NESTING) &&
(tdb->flags & TDB_DISALLOW_NESTING)) {
tdb->ecode = TDB_ERR_NESTING;
TDB_LOG((tdb, TDB_DEBUG_FATAL, "tdb_open_ex: "
"allow_nesting and disallow_nesting are not allowed together!"));
errno = EINVAL;
goto fail;
}
/*
* TDB_ALLOW_NESTING is the default behavior.
* Note: this may change in future versions!
*/
if (!(tdb->flags & TDB_DISALLOW_NESTING)) {
tdb->flags |= TDB_ALLOW_NESTING;
}
/* internal databases don't mmap or lock, and start off cleared */
if (tdb->flags & TDB_INTERNAL) {
tdb->flags |= (TDB_NOLOCK | TDB_NOMMAP);
@ -240,17 +262,19 @@ struct tdb_context *tdb_open_ex(const char *name, int hash_size, int tdb_flags,
errno = 0;
if (read(tdb->fd, &tdb->header, sizeof(tdb->header)) != sizeof(tdb->header)
|| strcmp(tdb->header.magic_food, TDB_MAGIC_FOOD) != 0
|| (tdb->header.version != TDB_VERSION
&& !(rev = (tdb->header.version==TDB_BYTEREV(TDB_VERSION))))) {
/* its not a valid database - possibly initialise it */
|| strcmp(tdb->header.magic_food, TDB_MAGIC_FOOD) != 0) {
if (!(open_flags & O_CREAT) || tdb_new_database(tdb, hash_size) == -1) {
if (errno == 0) {
errno = EIO; /* ie bad format or something */
errno = EIO; /* ie bad format or something */
}
goto fail;
}
rev = (tdb->flags & TDB_CONVERT);
} else if (tdb->header.version != TDB_VERSION
&& !(rev = (tdb->header.version==TDB_BYTEREV(TDB_VERSION)))) {
/* wrong version */
errno = EIO;
goto fail;
}
vp = (unsigned char *)&tdb->header.version;
vertest = (((uint32_t)vp[0]) << 24) | (((uint32_t)vp[1]) << 16) |
@ -313,6 +337,22 @@ struct tdb_context *tdb_open_ex(const char *name, int hash_size, int tdb_flags,
goto fail;
}
#ifdef TDB_TRACE
{
char tracefile[strlen(name) + 32];
snprintf(tracefile, sizeof(tracefile),
"%s.trace.%li", name, (long)getpid());
tdb->tracefd = open(tracefile, O_WRONLY|O_CREAT|O_EXCL, 0600);
if (tdb->tracefd >= 0) {
tdb_enable_seqnum(tdb);
tdb_trace_open(tdb, "tdb_open", hash_size, tdb_flags,
open_flags);
} else
TDB_LOG((tdb, TDB_DEBUG_ERROR, "tdb_open_ex: failed to open trace file %s!\n", tracefile));
}
#endif
internal:
/* Internal (memory-only) databases skip all the code above to
* do with disk files, and resume here by releasing their
@ -328,7 +368,10 @@ struct tdb_context *tdb_open_ex(const char *name, int hash_size, int tdb_flags,
if (!tdb)
return NULL;
#ifdef TDB_TRACE
close(tdb->tracefd);
#endif
if (tdb->map_ptr) {
if (tdb->flags & TDB_INTERNAL)
SAFE_FREE(tdb->map_ptr);
@ -364,8 +407,9 @@ int tdb_close(struct tdb_context *tdb)
struct tdb_context **i;
int ret = 0;
tdb_trace(tdb, "tdb_close");
if (tdb->transaction) {
tdb_transaction_cancel(tdb);
_tdb_transaction_cancel(tdb);
}
if (tdb->map_ptr) {
@ -375,8 +419,10 @@ int tdb_close(struct tdb_context *tdb)
tdb_munmap(tdb);
}
SAFE_FREE(tdb->name);
if (tdb->fd != -1)
if (tdb->fd != -1) {
ret = close(tdb->fd);
tdb->fd = -1;
}
SAFE_FREE(tdb->lockrecs);
/* Remove from contexts list */
@ -387,6 +433,9 @@ int tdb_close(struct tdb_context *tdb)
}
}
#ifdef TDB_TRACE
close(tdb->tracefd);
#endif
memset(tdb, 0, sizeof(*tdb));
SAFE_FREE(tdb);
@ -407,7 +456,10 @@ void *tdb_get_logging_private(struct tdb_context *tdb)
static int tdb_reopen_internal(struct tdb_context *tdb, bool active_lock)
{
#if !defined(LIBREPLACE_PREAD_NOT_REPLACED) || \
!defined(LIBREPLACE_PWRITE_NOT_REPLACED)
struct stat st;
#endif
if (tdb->flags & TDB_INTERNAL) {
return 0; /* Nothing to do. */

View File

@ -76,7 +76,7 @@ static int tdb_key_compare(TDB_DATA key, TDB_DATA data, void *private_data)
/* Returns 0 on fail. On success, return offset of record, and fills
in rec */
static tdb_off_t tdb_find(struct tdb_context *tdb, TDB_DATA key, uint32_t hash,
struct list_struct *r)
struct tdb_record *r)
{
tdb_off_t rec_ptr;
@ -98,17 +98,19 @@ static tdb_off_t tdb_find(struct tdb_context *tdb, TDB_DATA key, uint32_t hash,
}
/* detect tight infinite loop */
if (rec_ptr == r->next) {
tdb->ecode = TDB_ERR_CORRUPT;
TDB_LOG((tdb, TDB_DEBUG_FATAL, "tdb_find: loop detected.\n"));
return TDB_ERRCODE(TDB_ERR_CORRUPT, 0);
return 0;
}
rec_ptr = r->next;
}
return TDB_ERRCODE(TDB_ERR_NOEXIST, 0);
tdb->ecode = TDB_ERR_NOEXIST;
return 0;
}
/* As tdb_find, but if you succeed, keep the lock */
tdb_off_t tdb_find_lock_hash(struct tdb_context *tdb, TDB_DATA key, uint32_t hash, int locktype,
struct list_struct *rec)
struct tdb_record *rec)
{
uint32_t rec_ptr;
@ -119,6 +121,7 @@ tdb_off_t tdb_find_lock_hash(struct tdb_context *tdb, TDB_DATA key, uint32_t has
return rec_ptr;
}
static TDB_DATA _tdb_fetch(struct tdb_context *tdb, TDB_DATA key);
/* update an entry in place - this only works if the new data size
is <= the old data size and the key exists.
@ -126,13 +129,32 @@ tdb_off_t tdb_find_lock_hash(struct tdb_context *tdb, TDB_DATA key, uint32_t has
*/
static int tdb_update_hash(struct tdb_context *tdb, TDB_DATA key, uint32_t hash, TDB_DATA dbuf)
{
struct list_struct rec;
struct tdb_record rec;
tdb_off_t rec_ptr;
/* find entry */
if (!(rec_ptr = tdb_find(tdb, key, hash, &rec)))
return -1;
/* it could be an exact duplicate of what is there - this is
* surprisingly common (eg. with a ldb re-index). */
if (rec.key_len == key.dsize &&
rec.data_len == dbuf.dsize &&
rec.full_hash == hash) {
TDB_DATA data = _tdb_fetch(tdb, key);
if (data.dsize == dbuf.dsize &&
memcmp(data.dptr, dbuf.dptr, data.dsize) == 0) {
if (data.dptr) {
free(data.dptr);
}
return 0;
}
if (data.dptr) {
free(data.dptr);
}
}
/* must be long enough key, data and tailer */
if (rec.rec_len < key.dsize + dbuf.dsize + sizeof(tdb_off_t)) {
tdb->ecode = TDB_SUCCESS; /* Not really an error */
@ -158,10 +180,10 @@ static int tdb_update_hash(struct tdb_context *tdb, TDB_DATA key, uint32_t hash,
* then the TDB_DATA will have zero length but
* a non-zero pointer
*/
TDB_DATA tdb_fetch(struct tdb_context *tdb, TDB_DATA key)
static TDB_DATA _tdb_fetch(struct tdb_context *tdb, TDB_DATA key)
{
tdb_off_t rec_ptr;
struct list_struct rec;
struct tdb_record rec;
TDB_DATA ret;
uint32_t hash;
@ -177,6 +199,14 @@ TDB_DATA tdb_fetch(struct tdb_context *tdb, TDB_DATA key)
return ret;
}
TDB_DATA tdb_fetch(struct tdb_context *tdb, TDB_DATA key)
{
TDB_DATA ret = _tdb_fetch(tdb, key);
tdb_trace_1rec_retrec(tdb, "tdb_fetch", key, ret);
return ret;
}
/*
* Find an entry in the database and hand the record's data to a parsing
* function. The parsing function is executed under the chain read lock, so it
@ -199,7 +229,7 @@ int tdb_parse_record(struct tdb_context *tdb, TDB_DATA key,
void *private_data)
{
tdb_off_t rec_ptr;
struct list_struct rec;
struct tdb_record rec;
int ret;
uint32_t hash;
@ -207,8 +237,11 @@ int tdb_parse_record(struct tdb_context *tdb, TDB_DATA key,
hash = tdb->hash_fn(&key);
if (!(rec_ptr = tdb_find_lock_hash(tdb,key,hash,F_RDLCK,&rec))) {
return TDB_ERRCODE(TDB_ERR_NOEXIST, 0);
tdb_trace_1rec_ret(tdb, "tdb_parse_record", key, -1);
tdb->ecode = TDB_ERR_NOEXIST;
return 0;
}
tdb_trace_1rec_ret(tdb, "tdb_parse_record", key, 0);
ret = tdb_parse_data(tdb, key, rec_ptr + sizeof(rec) + rec.key_len,
rec.data_len, parser, private_data);
@ -226,7 +259,7 @@ int tdb_parse_record(struct tdb_context *tdb, TDB_DATA key,
*/
static int tdb_exists_hash(struct tdb_context *tdb, TDB_DATA key, uint32_t hash)
{
struct list_struct rec;
struct tdb_record rec;
if (tdb_find_lock_hash(tdb, key, hash, F_RDLCK, &rec) == 0)
return 0;
@ -237,18 +270,22 @@ static int tdb_exists_hash(struct tdb_context *tdb, TDB_DATA key, uint32_t hash)
int tdb_exists(struct tdb_context *tdb, TDB_DATA key)
{
uint32_t hash = tdb->hash_fn(&key);
return tdb_exists_hash(tdb, key, hash);
int ret;
ret = tdb_exists_hash(tdb, key, hash);
tdb_trace_1rec_ret(tdb, "tdb_exists", key, ret);
return ret;
}
/* actually delete an entry in the database given the offset */
int tdb_do_delete(struct tdb_context *tdb, tdb_off_t rec_ptr, struct list_struct *rec)
int tdb_do_delete(struct tdb_context *tdb, tdb_off_t rec_ptr, struct tdb_record *rec)
{
tdb_off_t last_ptr, i;
struct list_struct lastrec;
struct tdb_record lastrec;
if (tdb->read_only || tdb->traverse_read) return -1;
if (tdb->traverse_write != 0 ||
if (((tdb->traverse_write != 0) && (!TDB_DEAD(rec))) ||
tdb_write_lock_record(tdb, rec_ptr) == -1) {
/* Someone traversing here: mark it as dead */
rec->magic = TDB_DEAD_MAGIC;
@ -280,7 +317,7 @@ static int tdb_count_dead(struct tdb_context *tdb, uint32_t hash)
{
int res = 0;
tdb_off_t rec_ptr;
struct list_struct rec;
struct tdb_record rec;
/* read in the hash top */
if (tdb_ofs_read(tdb, TDB_HASH_TOP(hash), &rec_ptr) == -1)
@ -304,7 +341,7 @@ static int tdb_count_dead(struct tdb_context *tdb, uint32_t hash)
static int tdb_purge_dead(struct tdb_context *tdb, uint32_t hash)
{
int res = -1;
struct list_struct rec;
struct tdb_record rec;
tdb_off_t rec_ptr;
if (tdb_lock(tdb, -1, F_WRLCK) == -1) {
@ -340,7 +377,7 @@ static int tdb_purge_dead(struct tdb_context *tdb, uint32_t hash)
static int tdb_delete_hash(struct tdb_context *tdb, TDB_DATA key, uint32_t hash)
{
tdb_off_t rec_ptr;
struct list_struct rec;
struct tdb_record rec;
int ret;
if (tdb->max_dead_records != 0) {
@ -392,14 +429,18 @@ static int tdb_delete_hash(struct tdb_context *tdb, TDB_DATA key, uint32_t hash)
int tdb_delete(struct tdb_context *tdb, TDB_DATA key)
{
uint32_t hash = tdb->hash_fn(&key);
return tdb_delete_hash(tdb, key, hash);
int ret;
ret = tdb_delete_hash(tdb, key, hash);
tdb_trace_1rec_ret(tdb, "tdb_delete", key, ret);
return ret;
}
/*
* See if we have a dead record around with enough space
*/
static tdb_off_t tdb_find_dead(struct tdb_context *tdb, uint32_t hash,
struct list_struct *r, tdb_len_t length)
struct tdb_record *r, tdb_len_t length)
{
tdb_off_t rec_ptr;
@ -424,29 +465,14 @@ static tdb_off_t tdb_find_dead(struct tdb_context *tdb, uint32_t hash,
return 0;
}
/* store an element in the database, replacing any existing element
with the same key
return 0 on success, -1 on failure
*/
int tdb_store(struct tdb_context *tdb, TDB_DATA key, TDB_DATA dbuf, int flag)
static int _tdb_store(struct tdb_context *tdb, TDB_DATA key,
TDB_DATA dbuf, int flag, uint32_t hash)
{
struct list_struct rec;
uint32_t hash;
struct tdb_record rec;
tdb_off_t rec_ptr;
char *p = NULL;
int ret = -1;
if (tdb->read_only || tdb->traverse_read) {
tdb->ecode = TDB_ERR_RDONLY;
return -1;
}
/* find which hash bucket it is in */
hash = tdb->hash_fn(&key);
if (tdb_lock(tdb, BUCKET(hash), F_WRLCK) == -1)
return -1;
/* check for it existing, on insert. */
if (flag == TDB_INSERT) {
if (tdb_exists_hash(tdb, key, hash)) {
@ -562,10 +588,35 @@ int tdb_store(struct tdb_context *tdb, TDB_DATA key, TDB_DATA dbuf, int flag)
}
SAFE_FREE(p);
tdb_unlock(tdb, BUCKET(hash), F_WRLCK);
return ret;
}
/* store an element in the database, replacing any existing element
with the same key
return 0 on success, -1 on failure
*/
int tdb_store(struct tdb_context *tdb, TDB_DATA key, TDB_DATA dbuf, int flag)
{
uint32_t hash;
int ret;
if (tdb->read_only || tdb->traverse_read) {
tdb->ecode = TDB_ERR_RDONLY;
tdb_trace_2rec_flag_ret(tdb, "tdb_store", key, dbuf, flag, -1);
return -1;
}
/* find which hash bucket it is in */
hash = tdb->hash_fn(&key);
if (tdb_lock(tdb, BUCKET(hash), F_WRLCK) == -1)
return -1;
ret = _tdb_store(tdb, key, dbuf, flag, hash);
tdb_trace_2rec_flag_ret(tdb, "tdb_store", key, dbuf, flag, ret);
tdb_unlock(tdb, BUCKET(hash), F_WRLCK);
return ret;
}
/* Append to an entry. Create if not exist. */
int tdb_append(struct tdb_context *tdb, TDB_DATA key, TDB_DATA new_dbuf)
@ -579,7 +630,7 @@ int tdb_append(struct tdb_context *tdb, TDB_DATA key, TDB_DATA new_dbuf)
if (tdb_lock(tdb, BUCKET(hash), F_WRLCK) == -1)
return -1;
dbuf = tdb_fetch(tdb, key);
dbuf = _tdb_fetch(tdb, key);
if (dbuf.dptr == NULL) {
dbuf.dptr = (unsigned char *)malloc(new_dbuf.dsize);
@ -605,7 +656,8 @@ int tdb_append(struct tdb_context *tdb, TDB_DATA key, TDB_DATA new_dbuf)
memcpy(dbuf.dptr + dbuf.dsize, new_dbuf.dptr, new_dbuf.dsize);
dbuf.dsize += new_dbuf.dsize;
ret = tdb_store(tdb, key, dbuf, 0);
ret = _tdb_store(tdb, key, dbuf, 0, hash);
tdb_trace_2rec_retrec(tdb, "tdb_append", key, new_dbuf, dbuf);
failed:
tdb_unlock(tdb, BUCKET(hash), F_WRLCK);
@ -678,11 +730,41 @@ int tdb_get_flags(struct tdb_context *tdb)
void tdb_add_flags(struct tdb_context *tdb, unsigned flags)
{
if ((flags & TDB_ALLOW_NESTING) &&
(flags & TDB_DISALLOW_NESTING)) {
tdb->ecode = TDB_ERR_NESTING;
TDB_LOG((tdb, TDB_DEBUG_FATAL, "tdb_add_flags: "
"allow_nesting and disallow_nesting are not allowed together!"));
return;
}
if (flags & TDB_ALLOW_NESTING) {
tdb->flags &= ~TDB_DISALLOW_NESTING;
}
if (flags & TDB_DISALLOW_NESTING) {
tdb->flags &= ~TDB_ALLOW_NESTING;
}
tdb->flags |= flags;
}
void tdb_remove_flags(struct tdb_context *tdb, unsigned flags)
{
if ((flags & TDB_ALLOW_NESTING) &&
(flags & TDB_DISALLOW_NESTING)) {
tdb->ecode = TDB_ERR_NESTING;
TDB_LOG((tdb, TDB_DEBUG_FATAL, "tdb_remove_flags: "
"allow_nesting and disallow_nesting are not allowed together!"));
return;
}
if (flags & TDB_ALLOW_NESTING) {
tdb->flags |= TDB_DISALLOW_NESTING;
}
if (flags & TDB_DISALLOW_NESTING) {
tdb->flags |= TDB_ALLOW_NESTING;
}
tdb->flags &= ~flags;
}
@ -702,7 +784,7 @@ void tdb_enable_seqnum(struct tdb_context *tdb)
*/
static int tdb_free_region(struct tdb_context *tdb, tdb_off_t offset, ssize_t length)
{
struct list_struct rec;
struct tdb_record rec;
if (length <= sizeof(rec)) {
/* the region is not worth adding */
return 0;
@ -739,6 +821,8 @@ int tdb_wipe_all(struct tdb_context *tdb)
return -1;
}
tdb_trace(tdb, "tdb_wipe_all");
/* see if the tdb has a recovery area, and remember its size
if so. We don't want to lose this as otherwise each
tdb_wipe_all() in a transaction will increase the size of
@ -749,7 +833,7 @@ int tdb_wipe_all(struct tdb_context *tdb)
}
if (recovery_head != 0) {
struct list_struct rec;
struct tdb_record rec;
if (tdb->methods->tdb_read(tdb, recovery_head, &rec, sizeof(rec), DOCONV()) == -1) {
TDB_LOG((tdb, TDB_DEBUG_FATAL, "tdb_wipe_all: failed to read recovery record\n"));
return -1;
@ -819,9 +903,9 @@ struct traverse_state {
/*
traverse function for repacking
*/
static int repack_traverse(struct tdb_context *tdb, TDB_DATA key, TDB_DATA data, void *private)
static int repack_traverse(struct tdb_context *tdb, TDB_DATA key, TDB_DATA data, void *private_data)
{
struct traverse_state *state = (struct traverse_state *)private;
struct traverse_state *state = (struct traverse_state *)private_data;
if (tdb_store(state->dest_db, key, data, TDB_INSERT) != 0) {
state->error = true;
return -1;
@ -837,6 +921,8 @@ int tdb_repack(struct tdb_context *tdb)
struct tdb_context *tmp_db;
struct traverse_state state;
tdb_trace(tdb, "tdb_repack");
if (tdb_transaction_start(tdb) != 0) {
TDB_LOG((tdb, TDB_DEBUG_FATAL, __location__ " Failed to start transaction\n"));
return -1;
@ -899,3 +985,156 @@ int tdb_repack(struct tdb_context *tdb)
return 0;
}
#ifdef TDB_TRACE
static void tdb_trace_write(struct tdb_context *tdb, const char *str)
{
if (write(tdb->tracefd, str, strlen(str)) != strlen(str)) {
close(tdb->tracefd);
tdb->tracefd = -1;
}
}
static void tdb_trace_start(struct tdb_context *tdb)
{
tdb_off_t seqnum=0;
char msg[sizeof(tdb_off_t) * 4 + 1];
tdb_ofs_read(tdb, TDB_SEQNUM_OFS, &seqnum);
snprintf(msg, sizeof(msg), "%u ", seqnum);
tdb_trace_write(tdb, msg);
}
static void tdb_trace_end(struct tdb_context *tdb)
{
tdb_trace_write(tdb, "\n");
}
static void tdb_trace_end_ret(struct tdb_context *tdb, int ret)
{
char msg[sizeof(ret) * 4 + 4];
snprintf(msg, sizeof(msg), " = %i\n", ret);
tdb_trace_write(tdb, msg);
}
static void tdb_trace_record(struct tdb_context *tdb, TDB_DATA rec)
{
char msg[20 + rec.dsize*2], *p;
unsigned int i;
/* We differentiate zero-length records from non-existent ones. */
if (rec.dptr == NULL) {
tdb_trace_write(tdb, " NULL");
return;
}
/* snprintf here is purely cargo-cult programming. */
p = msg;
p += snprintf(p, sizeof(msg), " %zu:", rec.dsize);
for (i = 0; i < rec.dsize; i++)
p += snprintf(p, 2, "%02x", rec.dptr[i]);
tdb_trace_write(tdb, msg);
}
void tdb_trace(struct tdb_context *tdb, const char *op)
{
tdb_trace_start(tdb);
tdb_trace_write(tdb, op);
tdb_trace_end(tdb);
}
void tdb_trace_seqnum(struct tdb_context *tdb, uint32_t seqnum, const char *op)
{
char msg[sizeof(tdb_off_t) * 4 + 1];
snprintf(msg, sizeof(msg), "%u ", seqnum);
tdb_trace_write(tdb, msg);
tdb_trace_write(tdb, op);
tdb_trace_end(tdb);
}
void tdb_trace_open(struct tdb_context *tdb, const char *op,
unsigned hash_size, unsigned tdb_flags, unsigned open_flags)
{
char msg[128];
snprintf(msg, sizeof(msg),
"%s %u 0x%x 0x%x", op, hash_size, tdb_flags, open_flags);
tdb_trace_start(tdb);
tdb_trace_write(tdb, msg);
tdb_trace_end(tdb);
}
void tdb_trace_ret(struct tdb_context *tdb, const char *op, int ret)
{
tdb_trace_start(tdb);
tdb_trace_write(tdb, op);
tdb_trace_end_ret(tdb, ret);
}
void tdb_trace_retrec(struct tdb_context *tdb, const char *op, TDB_DATA ret)
{
tdb_trace_start(tdb);
tdb_trace_write(tdb, op);
tdb_trace_write(tdb, " =");
tdb_trace_record(tdb, ret);
tdb_trace_end(tdb);
}
void tdb_trace_1rec(struct tdb_context *tdb, const char *op,
TDB_DATA rec)
{
tdb_trace_start(tdb);
tdb_trace_write(tdb, op);
tdb_trace_record(tdb, rec);
tdb_trace_end(tdb);
}
void tdb_trace_1rec_ret(struct tdb_context *tdb, const char *op,
TDB_DATA rec, int ret)
{
tdb_trace_start(tdb);
tdb_trace_write(tdb, op);
tdb_trace_record(tdb, rec);
tdb_trace_end_ret(tdb, ret);
}
void tdb_trace_1rec_retrec(struct tdb_context *tdb, const char *op,
TDB_DATA rec, TDB_DATA ret)
{
tdb_trace_start(tdb);
tdb_trace_write(tdb, op);
tdb_trace_record(tdb, rec);
tdb_trace_write(tdb, " =");
tdb_trace_record(tdb, ret);
tdb_trace_end(tdb);
}
void tdb_trace_2rec_flag_ret(struct tdb_context *tdb, const char *op,
TDB_DATA rec1, TDB_DATA rec2, unsigned flag,
int ret)
{
char msg[1 + sizeof(ret) * 4];
snprintf(msg, sizeof(msg), " %#x", flag);
tdb_trace_start(tdb);
tdb_trace_write(tdb, op);
tdb_trace_record(tdb, rec1);
tdb_trace_record(tdb, rec2);
tdb_trace_write(tdb, msg);
tdb_trace_end_ret(tdb, ret);
}
void tdb_trace_2rec_retrec(struct tdb_context *tdb, const char *op,
TDB_DATA rec1, TDB_DATA rec2, TDB_DATA ret)
{
tdb_trace_start(tdb);
tdb_trace_write(tdb, op);
tdb_trace_record(tdb, rec1);
tdb_trace_record(tdb, rec2);
tdb_trace_write(tdb, " =");
tdb_trace_record(tdb, ret);
tdb_trace_end(tdb);
}
#endif

View File

@ -31,6 +31,7 @@
#include "system/wait.h"
#include "tdb.h"
/* #define TDB_TRACE 1 */
#ifndef HAVE_GETPAGESIZE
#define getpagesize() 0x2000
#endif
@ -68,6 +69,37 @@ typedef uint32_t tdb_off_t;
* argument. */
#define TDB_LOG(x) tdb->log.log_fn x
#ifdef TDB_TRACE
void tdb_trace(struct tdb_context *tdb, const char *op);
void tdb_trace_seqnum(struct tdb_context *tdb, uint32_t seqnum, const char *op);
void tdb_trace_open(struct tdb_context *tdb, const char *op,
unsigned hash_size, unsigned tdb_flags, unsigned open_flags);
void tdb_trace_ret(struct tdb_context *tdb, const char *op, int ret);
void tdb_trace_retrec(struct tdb_context *tdb, const char *op, TDB_DATA ret);
void tdb_trace_1rec(struct tdb_context *tdb, const char *op,
TDB_DATA rec);
void tdb_trace_1rec_ret(struct tdb_context *tdb, const char *op,
TDB_DATA rec, int ret);
void tdb_trace_1rec_retrec(struct tdb_context *tdb, const char *op,
TDB_DATA rec, TDB_DATA ret);
void tdb_trace_2rec_flag_ret(struct tdb_context *tdb, const char *op,
TDB_DATA rec1, TDB_DATA rec2, unsigned flag,
int ret);
void tdb_trace_2rec_retrec(struct tdb_context *tdb, const char *op,
TDB_DATA rec1, TDB_DATA rec2, TDB_DATA ret);
#else
#define tdb_trace(tdb, op)
#define tdb_trace_seqnum(tdb, seqnum, op)
#define tdb_trace_open(tdb, op, hash_size, tdb_flags, open_flags)
#define tdb_trace_ret(tdb, op, ret)
#define tdb_trace_retrec(tdb, op, ret)
#define tdb_trace_1rec(tdb, op, rec)
#define tdb_trace_1rec_ret(tdb, op, rec, ret)
#define tdb_trace_1rec_retrec(tdb, op, rec, ret)
#define tdb_trace_2rec_flag_ret(tdb, op, rec1, rec2, flag, ret)
#define tdb_trace_2rec_retrec(tdb, op, rec1, rec2, ret)
#endif /* !TDB_TRACE */
/* lock offsets */
#define GLOBAL_LOCK 0
#define ACTIVE_LOCK 4
@ -84,9 +116,9 @@ typedef uint32_t tdb_off_t;
#define CONVERT(x) (DOCONV() ? tdb_convert(&x, sizeof(x)) : &x)
/* the body of the database is made of one list_struct for the free space
/* the body of the database is made of one tdb_record for the free space
plus a separate data list for each hash value */
struct list_struct {
struct tdb_record {
tdb_off_t next; /* offset of the next record in the list */
tdb_len_t rec_len; /* total byte length of record */
tdb_len_t key_len; /* byte length of key */
@ -167,6 +199,9 @@ struct tdb_context {
int page_size;
int max_dead_records;
int transaction_lock_count;
#ifdef TDB_TRACE
int tracefd;
#endif
volatile sig_atomic_t *interrupt_sig_ptr;
};
@ -188,15 +223,16 @@ int tdb_write_unlock_record(struct tdb_context *tdb, tdb_off_t off);
int tdb_ofs_read(struct tdb_context *tdb, tdb_off_t offset, tdb_off_t *d);
int tdb_ofs_write(struct tdb_context *tdb, tdb_off_t offset, tdb_off_t *d);
void *tdb_convert(void *buf, uint32_t size);
int tdb_free(struct tdb_context *tdb, tdb_off_t offset, struct list_struct *rec);
tdb_off_t tdb_allocate(struct tdb_context *tdb, tdb_len_t length, struct list_struct *rec);
int tdb_free(struct tdb_context *tdb, tdb_off_t offset, struct tdb_record *rec);
tdb_off_t tdb_allocate(struct tdb_context *tdb, tdb_len_t length, struct tdb_record *rec);
int tdb_ofs_read(struct tdb_context *tdb, tdb_off_t offset, tdb_off_t *d);
int tdb_ofs_write(struct tdb_context *tdb, tdb_off_t offset, tdb_off_t *d);
int tdb_lock_record(struct tdb_context *tdb, tdb_off_t off);
int tdb_unlock_record(struct tdb_context *tdb, tdb_off_t off);
int tdb_rec_read(struct tdb_context *tdb, tdb_off_t offset, struct list_struct *rec);
int tdb_rec_write(struct tdb_context *tdb, tdb_off_t offset, struct list_struct *rec);
int tdb_do_delete(struct tdb_context *tdb, tdb_off_t rec_ptr, struct list_struct *rec);
int _tdb_transaction_cancel(struct tdb_context *tdb);
int tdb_rec_read(struct tdb_context *tdb, tdb_off_t offset, struct tdb_record *rec);
int tdb_rec_write(struct tdb_context *tdb, tdb_off_t offset, struct tdb_record *rec);
int tdb_do_delete(struct tdb_context *tdb, tdb_off_t rec_ptr, struct tdb_record *rec);
unsigned char *tdb_alloc_read(struct tdb_context *tdb, tdb_off_t offset, tdb_len_t len);
int tdb_parse_data(struct tdb_context *tdb, TDB_DATA key,
tdb_off_t offset, tdb_len_t len,
@ -204,10 +240,10 @@ int tdb_parse_data(struct tdb_context *tdb, TDB_DATA key,
void *private_data),
void *private_data);
tdb_off_t tdb_find_lock_hash(struct tdb_context *tdb, TDB_DATA key, uint32_t hash, int locktype,
struct list_struct *rec);
struct tdb_record *rec);
void tdb_io_init(struct tdb_context *tdb);
int tdb_expand(struct tdb_context *tdb, tdb_off_t size);
int tdb_rec_free_read(struct tdb_context *tdb, tdb_off_t off,
struct list_struct *rec);
struct tdb_record *rec);

View File

@ -86,12 +86,20 @@
fsync/msync calls are made.
- if TDB_ALLOW_NESTING is passed to flags in tdb open, or added using
tdb_add_flags() transaction is enabled.
The default is that transaction nesting is not allowed and an attempt
to create a nested transaction will fail with TDB_ERR_NESTING.
tdb_add_flags() transaction nesting is enabled.
It resets the TDB_DISALLOW_NESTING flag, as both cannot be used together.
The default is that transaction nesting is allowed.
Note: this default may change in future versions of tdb.
Beware. when transactions are nested a transaction successfully
completed with tdb_transaction_commit() can be silently unrolled later.
- if TDB_DISALLOW_NESTING is passed to flags in tdb open, or added using
tdb_add_flags() transaction nesting is disabled.
It resets the TDB_ALLOW_NESTING flag, as both cannot be used together.
An attempt create a nested transaction will fail with TDB_ERR_NESTING.
The default is that transaction nesting is allowed.
Note: this default may change in future versions of tdb.
*/
@ -144,14 +152,6 @@ static int transaction_read(struct tdb_context *tdb, tdb_off_t off, void *buf,
{
uint32_t blk;
/* Only a commit is allowed on a prepared transaction */
if (tdb->transaction->prepared) {
tdb->ecode = TDB_ERR_EINVAL;
TDB_LOG((tdb, TDB_DEBUG_FATAL, "transaction_read: transaction already prepared, read not allowed\n"));
tdb->transaction->transaction_error = 1;
return -1;
}
/* break it down into block sized ops */
while (len + (off % tdb->transaction->block_size) > tdb->transaction->block_size) {
tdb_len_t len2 = tdb->transaction->block_size - (off % tdb->transaction->block_size);
@ -387,7 +387,8 @@ static int transaction_oob(struct tdb_context *tdb, tdb_off_t len, int probe)
if (len <= tdb->map_size) {
return 0;
}
return TDB_ERRCODE(TDB_ERR_IO, -1);
tdb->ecode = TDB_ERR_IO;
return -1;
}
/*
@ -521,6 +522,8 @@ int tdb_transaction_start(struct tdb_context *tdb)
tdb->transaction->io_methods = tdb->methods;
tdb->methods = &transaction_methods;
/* Trace at the end, so we get sequence number correct. */
tdb_trace(tdb, "tdb_transaction_start");
return 0;
fail:
@ -547,7 +550,7 @@ static int transaction_sync(struct tdb_context *tdb, tdb_off_t offset, tdb_len_t
TDB_LOG((tdb, TDB_DEBUG_FATAL, "tdb_transaction: fsync failed\n"));
return -1;
}
#ifdef MS_SYNC
#ifdef HAVE_MMAP
if (tdb->map_ptr) {
tdb_off_t moffset = offset & ~(tdb->page_size-1);
if (msync(moffset + (char *)tdb->map_ptr,
@ -563,10 +566,7 @@ static int transaction_sync(struct tdb_context *tdb, tdb_off_t offset, tdb_len_t
}
/*
cancel the current transaction
*/
int tdb_transaction_cancel(struct tdb_context *tdb)
int _tdb_transaction_cancel(struct tdb_context *tdb)
{
int i, ret = 0;
@ -631,6 +631,14 @@ int tdb_transaction_cancel(struct tdb_context *tdb)
return ret;
}
/*
cancel the current transaction
*/
int tdb_transaction_cancel(struct tdb_context *tdb)
{
tdb_trace(tdb, "tdb_transaction_cancel");
return _tdb_transaction_cancel(tdb);
}
/*
work out how much space the linearised recovery data will consume
@ -668,7 +676,7 @@ static int tdb_recovery_allocate(struct tdb_context *tdb,
tdb_off_t *recovery_offset,
tdb_len_t *recovery_max_size)
{
struct list_struct rec;
struct tdb_record rec;
const struct tdb_methods *methods = tdb->transaction->io_methods;
tdb_off_t recovery_head;
@ -754,7 +762,7 @@ static int transaction_setup_recovery(struct tdb_context *tdb,
tdb_len_t recovery_size;
unsigned char *data, *p;
const struct tdb_methods *methods = tdb->transaction->io_methods;
struct list_struct *rec;
struct tdb_record *rec;
tdb_off_t recovery_offset, recovery_max_size;
tdb_off_t old_map_size = tdb->transaction->old_map_size;
uint32_t magic, tailer;
@ -774,7 +782,7 @@ static int transaction_setup_recovery(struct tdb_context *tdb,
return -1;
}
rec = (struct list_struct *)data;
rec = (struct tdb_record *)data;
memset(rec, 0, sizeof(*rec));
rec->magic = 0;
@ -857,7 +865,7 @@ static int transaction_setup_recovery(struct tdb_context *tdb,
magic = TDB_RECOVERY_MAGIC;
CONVERT(magic);
*magic_offset = recovery_offset + offsetof(struct list_struct, magic);
*magic_offset = recovery_offset + offsetof(struct tdb_record, magic);
if (methods->tdb_write(tdb, *magic_offset, &magic, sizeof(magic)) == -1) {
TDB_LOG((tdb, TDB_DEBUG_FATAL, "tdb_transaction_setup_recovery: failed to write recovery magic\n"));
@ -878,10 +886,7 @@ static int transaction_setup_recovery(struct tdb_context *tdb,
return 0;
}
/*
prepare to commit the current transaction
*/
int tdb_transaction_prepare_commit(struct tdb_context *tdb)
static int _tdb_transaction_prepare_commit(struct tdb_context *tdb)
{
const struct tdb_methods *methods;
@ -892,14 +897,14 @@ int tdb_transaction_prepare_commit(struct tdb_context *tdb)
if (tdb->transaction->prepared) {
tdb->ecode = TDB_ERR_EINVAL;
tdb_transaction_cancel(tdb);
_tdb_transaction_cancel(tdb);
TDB_LOG((tdb, TDB_DEBUG_ERROR, "tdb_transaction_prepare_commit: transaction already prepared\n"));
return -1;
}
if (tdb->transaction->transaction_error) {
tdb->ecode = TDB_ERR_IO;
tdb_transaction_cancel(tdb);
_tdb_transaction_cancel(tdb);
TDB_LOG((tdb, TDB_DEBUG_ERROR, "tdb_transaction_prepare_commit: transaction error pending\n"));
return -1;
}
@ -921,7 +926,7 @@ int tdb_transaction_prepare_commit(struct tdb_context *tdb)
if (tdb->num_locks || tdb->global_lock.count) {
tdb->ecode = TDB_ERR_LOCK;
TDB_LOG((tdb, TDB_DEBUG_ERROR, "tdb_transaction_prepare_commit: locks pending on commit\n"));
tdb_transaction_cancel(tdb);
_tdb_transaction_cancel(tdb);
return -1;
}
@ -929,7 +934,7 @@ int tdb_transaction_prepare_commit(struct tdb_context *tdb)
if (tdb_brlock_upgrade(tdb, FREELIST_TOP, 0) == -1) {
TDB_LOG((tdb, TDB_DEBUG_ERROR, "tdb_transaction_prepare_commit: failed to upgrade hash locks\n"));
tdb->ecode = TDB_ERR_LOCK;
tdb_transaction_cancel(tdb);
_tdb_transaction_cancel(tdb);
return -1;
}
@ -938,7 +943,7 @@ int tdb_transaction_prepare_commit(struct tdb_context *tdb)
if (tdb_brlock(tdb, GLOBAL_LOCK, F_WRLCK, F_SETLKW, 0, 1) == -1) {
TDB_LOG((tdb, TDB_DEBUG_ERROR, "tdb_transaction_prepare_commit: failed to get global lock\n"));
tdb->ecode = TDB_ERR_LOCK;
tdb_transaction_cancel(tdb);
_tdb_transaction_cancel(tdb);
return -1;
}
@ -947,7 +952,7 @@ int tdb_transaction_prepare_commit(struct tdb_context *tdb)
if (transaction_setup_recovery(tdb, &tdb->transaction->magic_offset) == -1) {
TDB_LOG((tdb, TDB_DEBUG_FATAL, "tdb_transaction_prepare_commit: failed to setup recovery data\n"));
tdb_brlock(tdb, GLOBAL_LOCK, F_UNLCK, F_SETLKW, 0, 1);
tdb_transaction_cancel(tdb);
_tdb_transaction_cancel(tdb);
return -1;
}
}
@ -962,7 +967,7 @@ int tdb_transaction_prepare_commit(struct tdb_context *tdb)
tdb->ecode = TDB_ERR_IO;
TDB_LOG((tdb, TDB_DEBUG_FATAL, "tdb_transaction_prepare_commit: expansion failed\n"));
tdb_brlock(tdb, GLOBAL_LOCK, F_UNLCK, F_SETLKW, 0, 1);
tdb_transaction_cancel(tdb);
_tdb_transaction_cancel(tdb);
return -1;
}
tdb->map_size = tdb->transaction->old_map_size;
@ -974,6 +979,15 @@ int tdb_transaction_prepare_commit(struct tdb_context *tdb)
return 0;
}
/*
prepare to commit the current transaction
*/
int tdb_transaction_prepare_commit(struct tdb_context *tdb)
{
tdb_trace(tdb, "tdb_transaction_prepare_commit");
return _tdb_transaction_prepare_commit(tdb);
}
/*
commit the current transaction
*/
@ -988,9 +1002,11 @@ int tdb_transaction_commit(struct tdb_context *tdb)
return -1;
}
tdb_trace(tdb, "tdb_transaction_commit");
if (tdb->transaction->transaction_error) {
tdb->ecode = TDB_ERR_IO;
tdb_transaction_cancel(tdb);
_tdb_transaction_cancel(tdb);
TDB_LOG((tdb, TDB_DEBUG_ERROR, "tdb_transaction_commit: transaction error pending\n"));
return -1;
}
@ -1003,12 +1019,12 @@ int tdb_transaction_commit(struct tdb_context *tdb)
/* check for a null transaction */
if (tdb->transaction->blocks == NULL) {
tdb_transaction_cancel(tdb);
_tdb_transaction_cancel(tdb);
return 0;
}
if (!tdb->transaction->prepared) {
int ret = tdb_transaction_prepare_commit(tdb);
int ret = _tdb_transaction_prepare_commit(tdb);
if (ret)
return ret;
}
@ -1039,7 +1055,7 @@ int tdb_transaction_commit(struct tdb_context *tdb)
tdb->methods = methods;
tdb_transaction_recover(tdb);
tdb_transaction_cancel(tdb);
_tdb_transaction_cancel(tdb);
tdb_brlock(tdb, GLOBAL_LOCK, F_UNLCK, F_SETLKW, 0, 1);
TDB_LOG((tdb, TDB_DEBUG_FATAL, "tdb_transaction_commit: write failed\n"));
@ -1077,7 +1093,7 @@ int tdb_transaction_commit(struct tdb_context *tdb)
/* use a transaction cancel to free memory and remove the
transaction locks */
tdb_transaction_cancel(tdb);
_tdb_transaction_cancel(tdb);
if (need_repack) {
return tdb_repack(tdb);
@ -1097,7 +1113,7 @@ int tdb_transaction_recover(struct tdb_context *tdb)
tdb_off_t recovery_head, recovery_eof;
unsigned char *data, *p;
uint32_t zero = 0;
struct list_struct rec;
struct tdb_record rec;
/* find the recovery area */
if (tdb_ofs_read(tdb, TDB_RECOVERY_HEAD, &recovery_head) == -1) {
@ -1184,7 +1200,7 @@ int tdb_transaction_recover(struct tdb_context *tdb)
}
/* remove the recovery magic */
if (tdb_ofs_write(tdb, recovery_head + offsetof(struct list_struct, magic),
if (tdb_ofs_write(tdb, recovery_head + offsetof(struct tdb_record, magic),
&zero) == -1) {
TDB_LOG((tdb, TDB_DEBUG_FATAL, "tdb_transaction_recover: failed to remove recovery magic\n"));
tdb->ecode = TDB_ERR_IO;

View File

@ -27,9 +27,12 @@
#include "tdb_private.h"
/* Uses traverse lock: 0 = finish, -1 = error, other = record offset */
static int tdb_next_lock(struct tdb_context *tdb, struct tdb_traverse_lock *tlock,
struct list_struct *rec)
#define TDB_NEXT_LOCK_ERR ((tdb_off_t)-1)
/* Uses traverse lock: 0 = finish, TDB_NEXT_LOCK_ERR = error,
other = record offset */
static tdb_off_t tdb_next_lock(struct tdb_context *tdb, struct tdb_traverse_lock *tlock,
struct tdb_record *rec)
{
int want_next = (tlock->off != 0);
@ -71,7 +74,7 @@ static int tdb_next_lock(struct tdb_context *tdb, struct tdb_traverse_lock *tloc
}
if (tdb_lock(tdb, tlock->hash, tlock->lock_rw) == -1)
return -1;
return TDB_NEXT_LOCK_ERR;
/* No previous record? Start at top of chain. */
if (!tlock->off) {
@ -99,6 +102,7 @@ static int tdb_next_lock(struct tdb_context *tdb, struct tdb_traverse_lock *tloc
/* Detect infinite loops. From "Shlomi Yaakobovich" <Shlomi@exanet.com>. */
if (tlock->off == rec->next) {
tdb->ecode = TDB_ERR_CORRUPT;
TDB_LOG((tdb, TDB_DEBUG_FATAL, "tdb_next_lock: loop detected.\n"));
goto fail;
}
@ -121,13 +125,14 @@ static int tdb_next_lock(struct tdb_context *tdb, struct tdb_traverse_lock *tloc
want_next = 0;
}
/* We finished iteration without finding anything */
return TDB_ERRCODE(TDB_SUCCESS, 0);
tdb->ecode = TDB_SUCCESS;
return 0;
fail:
tlock->off = 0;
if (tdb_unlock(tdb, tlock->hash, tlock->lock_rw) != 0)
TDB_LOG((tdb, TDB_DEBUG_FATAL, "tdb_next_lock: On error unlock failed!\n"));
return -1;
return TDB_NEXT_LOCK_ERR;
}
/* traverse the entire database - calling fn(tdb, key, data) on each element.
@ -140,8 +145,9 @@ static int tdb_traverse_internal(struct tdb_context *tdb,
struct tdb_traverse_lock *tl)
{
TDB_DATA key, dbuf;
struct list_struct rec;
int ret, count = 0;
struct tdb_record rec;
int ret = 0, count = 0;
tdb_off_t off;
/* This was in the initializaton, above, but the IRIX compiler
* did not like it. crh
@ -152,7 +158,11 @@ static int tdb_traverse_internal(struct tdb_context *tdb,
tdb->travlocks.next = tl;
/* tdb_next_lock places locks on the record returned, and its chain */
while ((ret = tdb_next_lock(tdb, tl, &rec)) > 0) {
while ((off = tdb_next_lock(tdb, tl, &rec)) != 0) {
if (off == TDB_NEXT_LOCK_ERR) {
ret = -1;
goto out;
}
count++;
/* now read the full record */
key.dptr = tdb_alloc_read(tdb, tl->off + sizeof(rec),
@ -169,6 +179,8 @@ static int tdb_traverse_internal(struct tdb_context *tdb,
dbuf.dptr = key.dptr + rec.key_len;
dbuf.dsize = rec.data_len;
tdb_trace_1rec_retrec(tdb, "traverse", key, dbuf);
/* Drop chain lock, call out */
if (tdb_unlock(tdb, tl->hash, tl->lock_rw) != 0) {
ret = -1;
@ -177,7 +189,7 @@ static int tdb_traverse_internal(struct tdb_context *tdb,
}
if (fn && fn(tdb, key, dbuf, private_data)) {
/* They want us to terminate traversal */
ret = count;
tdb_trace_ret(tdb, "tdb_traverse_end", count);
if (tdb_unlock_record(tdb, tl->off) != 0) {
TDB_LOG((tdb, TDB_DEBUG_FATAL, "tdb_traverse: unlock_record failed!\n"));;
ret = -1;
@ -187,6 +199,7 @@ static int tdb_traverse_internal(struct tdb_context *tdb,
}
SAFE_FREE(key.dptr);
}
tdb_trace(tdb, "tdb_traverse_end");
out:
tdb->travlocks.next = tl->next;
if (ret < 0)
@ -212,6 +225,7 @@ int tdb_traverse_read(struct tdb_context *tdb,
}
tdb->traverse_read++;
tdb_trace(tdb, "tdb_traverse_read_start");
ret = tdb_traverse_internal(tdb, fn, private_data, &tl);
tdb->traverse_read--;
@ -236,12 +250,13 @@ int tdb_traverse(struct tdb_context *tdb,
if (tdb->read_only || tdb->traverse_read) {
return tdb_traverse_read(tdb, fn, private_data);
}
if (tdb_transaction_lock(tdb, F_WRLCK)) {
return -1;
}
tdb->traverse_write++;
tdb_trace(tdb, "tdb_traverse_start");
ret = tdb_traverse_internal(tdb, fn, private_data, &tl);
tdb->traverse_write--;
@ -255,7 +270,8 @@ int tdb_traverse(struct tdb_context *tdb,
TDB_DATA tdb_firstkey(struct tdb_context *tdb)
{
TDB_DATA key;
struct list_struct rec;
struct tdb_record rec;
tdb_off_t off;
/* release any old lock */
if (tdb_unlock_record(tdb, tdb->travlocks.off) != 0)
@ -264,12 +280,17 @@ TDB_DATA tdb_firstkey(struct tdb_context *tdb)
tdb->travlocks.lock_rw = F_RDLCK;
/* Grab first record: locks chain and returned record. */
if (tdb_next_lock(tdb, &tdb->travlocks, &rec) <= 0)
off = tdb_next_lock(tdb, &tdb->travlocks, &rec);
if (off == 0 || off == TDB_NEXT_LOCK_ERR) {
tdb_trace_retrec(tdb, "tdb_firstkey", tdb_null);
return tdb_null;
}
/* now read the key */
key.dsize = rec.key_len;
key.dptr =tdb_alloc_read(tdb,tdb->travlocks.off+sizeof(rec),key.dsize);
tdb_trace_retrec(tdb, "tdb_firstkey", key);
/* Unlock the hash chain of the record we just read. */
if (tdb_unlock(tdb, tdb->travlocks.hash, tdb->travlocks.lock_rw) != 0)
TDB_LOG((tdb, TDB_DEBUG_FATAL, "tdb_firstkey: error occurred while tdb_unlocking!\n"));
@ -281,8 +302,9 @@ TDB_DATA tdb_nextkey(struct tdb_context *tdb, TDB_DATA oldkey)
{
uint32_t oldhash;
TDB_DATA key = tdb_null;
struct list_struct rec;
struct tdb_record rec;
unsigned char *k = NULL;
tdb_off_t off;
/* Is locked key the old key? If so, traverse will be reliable. */
if (tdb->travlocks.off) {
@ -294,6 +316,8 @@ TDB_DATA tdb_nextkey(struct tdb_context *tdb, TDB_DATA oldkey)
|| memcmp(k, oldkey.dptr, oldkey.dsize) != 0) {
/* No, it wasn't: unlock it and start from scratch */
if (tdb_unlock_record(tdb, tdb->travlocks.off) != 0) {
tdb_trace_1rec_retrec(tdb, "tdb_nextkey",
oldkey, tdb_null);
SAFE_FREE(k);
return tdb_null;
}
@ -310,8 +334,10 @@ TDB_DATA tdb_nextkey(struct tdb_context *tdb, TDB_DATA oldkey)
if (!tdb->travlocks.off) {
/* No previous element: do normal find, and lock record */
tdb->travlocks.off = tdb_find_lock_hash(tdb, oldkey, tdb->hash_fn(&oldkey), tdb->travlocks.lock_rw, &rec);
if (!tdb->travlocks.off)
if (!tdb->travlocks.off) {
tdb_trace_1rec_retrec(tdb, "tdb_nextkey", oldkey, tdb_null);
return tdb_null;
}
tdb->travlocks.hash = BUCKET(rec.full_hash);
if (tdb_lock_record(tdb, tdb->travlocks.off) != 0) {
TDB_LOG((tdb, TDB_DEBUG_FATAL, "tdb_nextkey: lock_record failed (%s)!\n", strerror(errno)));
@ -322,7 +348,8 @@ TDB_DATA tdb_nextkey(struct tdb_context *tdb, TDB_DATA oldkey)
/* Grab next record: locks chain and returned record,
unlocks old record */
if (tdb_next_lock(tdb, &tdb->travlocks, &rec) > 0) {
off = tdb_next_lock(tdb, &tdb->travlocks, &rec);
if (off != TDB_NEXT_LOCK_ERR && off != 0) {
key.dsize = rec.key_len;
key.dptr = tdb_alloc_read(tdb, tdb->travlocks.off+sizeof(rec),
key.dsize);
@ -333,6 +360,7 @@ TDB_DATA tdb_nextkey(struct tdb_context *tdb, TDB_DATA oldkey)
/* Unlock the chain of old record */
if (tdb_unlock(tdb, BUCKET(oldhash), tdb->travlocks.lock_rw) != 0)
TDB_LOG((tdb, TDB_DEBUG_FATAL, "tdb_nextkey: WARNING tdb_unlock failed!\n"));
tdb_trace_1rec_retrec(tdb, "tdb_nextkey", oldkey, key);
return key;
}

View File

@ -1,10 +1,10 @@
#! /bin/sh
# Attempt to guess a canonical system name.
# Copyright (C) 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999,
# 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009
# 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008
# Free Software Foundation, Inc.
timestamp='2009-06-10'
timestamp='2009-04-27'
# This file is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by
@ -170,7 +170,7 @@ case "${UNAME_MACHINE}:${UNAME_SYSTEM}:${UNAME_RELEASE}:${UNAME_VERSION}" in
arm*|i386|m68k|ns32k|sh3*|sparc|vax)
eval $set_cc_for_build
if echo __ELF__ | $CC_FOR_BUILD -E - 2>/dev/null \
| grep -q __ELF__
| grep __ELF__ >/dev/null
then
# Once all utilities can be ECOFF (netbsdecoff) or a.out (netbsdaout).
# Return netbsd for either. FIX?
@ -656,7 +656,7 @@ EOF
# => hppa64-hp-hpux11.23
if echo __LP64__ | (CCOPTS= $CC_FOR_BUILD -E - 2>/dev/null) |
grep -q __LP64__
grep __LP64__ >/dev/null
then
HP_ARCH="hppa2.0w"
else
@ -822,9 +822,6 @@ EOF
[345]86:Windows_95:* | [345]86:Windows_98:* | [345]86:Windows_NT:*)
echo i${UNAME_MACHINE}-pc-mks
exit ;;
8664:Windows_NT:*)
echo x86_64-pc-mks
exit ;;
i*:Windows_NT*:* | Pentium*:Windows_NT*:*)
# How do we know it's Interix rather than the generic POSIX subsystem?
# It also conflicts with pre-2.0 versions of AT&T UWIN. Should we
@ -885,17 +882,40 @@ EOF
m68*:Linux:*:*)
echo ${UNAME_MACHINE}-unknown-linux-gnu
exit ;;
mips:Linux:*:* | mips64:Linux:*:*)
mips:Linux:*:*)
eval $set_cc_for_build
sed 's/^ //' << EOF >$dummy.c
#undef CPU
#undef ${UNAME_MACHINE}
#undef ${UNAME_MACHINE}el
#undef mips
#undef mipsel
#if defined(__MIPSEL__) || defined(__MIPSEL) || defined(_MIPSEL) || defined(MIPSEL)
CPU=${UNAME_MACHINE}el
CPU=mipsel
#else
#if defined(__MIPSEB__) || defined(__MIPSEB) || defined(_MIPSEB) || defined(MIPSEB)
CPU=${UNAME_MACHINE}
CPU=mips
#else
CPU=
#endif
#endif
EOF
eval "`$CC_FOR_BUILD -E $dummy.c 2>/dev/null | sed -n '
/^CPU/{
s: ::g
p
}'`"
test x"${CPU}" != x && { echo "${CPU}-unknown-linux-gnu"; exit; }
;;
mips64:Linux:*:*)
eval $set_cc_for_build
sed 's/^ //' << EOF >$dummy.c
#undef CPU
#undef mips64
#undef mips64el
#if defined(__MIPSEL__) || defined(__MIPSEL) || defined(_MIPSEL) || defined(MIPSEL)
CPU=mips64el
#else
#if defined(__MIPSEB__) || defined(__MIPSEB) || defined(_MIPSEB) || defined(MIPSEB)
CPU=mips64
#else
CPU=
#endif
@ -927,7 +947,7 @@ EOF
EV67) UNAME_MACHINE=alphaev67 ;;
EV68*) UNAME_MACHINE=alphaev68 ;;
esac
objdump --private-headers /bin/sh | grep -q ld.so.1
objdump --private-headers /bin/sh | grep ld.so.1 >/dev/null
if test "$?" = 0 ; then LIBC="libc1" ; else LIBC="" ; fi
echo ${UNAME_MACHINE}-unknown-linux-gnu${LIBC}
exit ;;
@ -981,6 +1001,14 @@ EOF
elf32-i386)
TENTATIVE="${UNAME_MACHINE}-pc-linux-gnu"
;;
a.out-i386-linux)
echo "${UNAME_MACHINE}-pc-linux-gnuaout"
exit ;;
"")
# Either a pre-BFD a.out linker (linux-gnuoldld) or
# one that does not give us useful --help.
echo "${UNAME_MACHINE}-pc-linux-gnuoldld"
exit ;;
esac
# Determine whether the default compiler is a.out or elf
eval $set_cc_for_build
@ -1046,7 +1074,7 @@ EOF
i*86:syllable:*:*)
echo ${UNAME_MACHINE}-pc-syllable
exit ;;
i*86:LynxOS:2.*:* | i*86:LynxOS:3.[01]*:* | i*86:LynxOS:4.[02]*:*)
i*86:LynxOS:2.*:* | i*86:LynxOS:3.[01]*:* | i*86:LynxOS:4.0*:*)
echo i386-unknown-lynxos${UNAME_RELEASE}
exit ;;
i*86:*DOS:*:*)
@ -1154,7 +1182,7 @@ EOF
rs6000:LynxOS:2.*:*)
echo rs6000-unknown-lynxos${UNAME_RELEASE}
exit ;;
PowerPC:LynxOS:2.*:* | PowerPC:LynxOS:3.[01]*:* | PowerPC:LynxOS:4.[02]*:*)
PowerPC:LynxOS:2.*:* | PowerPC:LynxOS:3.[01]*:* | PowerPC:LynxOS:4.0*:*)
echo powerpc-unknown-lynxos${UNAME_RELEASE}
exit ;;
SM[BE]S:UNIX_SV:*:*)

View File

@ -1,59 +1,57 @@
################################################
# Start SUBSYSTEM LIBTDB
[LIBRARY::LIBTDB]
VERSION = 0.0.1
SO_VERSION = 0
DESCRIPTION = Trivial Database Library
OBJ_FILES = \
common/tdb.o common/dump.o common/io.o common/lock.o \
common/open.o common/traverse.o common/freelist.o \
common/error.o common/transaction.o
CFLAGS = -Ilib/tdb/include
PUBLIC_HEADERS = include/tdb.h
OUTPUT_TYPE = MERGED_OBJ
CFLAGS = -I$(tdbsrcdir)/include
#
# End SUBSYSTEM ldb
################################################
LIBTDB_OBJ_FILES = $(addprefix $(tdbsrcdir)/common/, \
tdb.o dump.o io.o lock.o \
open.o traverse.o freelist.o \
error.o transaction.o check.o)
################################################
# Start BINARY tdbtool
[BINARY::tdbtool]
INSTALLDIR = BINDIR
OBJ_FILES= \
tools/tdbtool.o
PRIVATE_DEPENDENCIES = \
LIBTDB
# End BINARY tdbtool
################################################
tdbtool_OBJ_FILES = $(tdbsrcdir)/tools/tdbtool.o
################################################
# Start BINARY tdbtorture
[BINARY::tdbtorture]
INSTALLDIR = BINDIR
OBJ_FILES= \
tools/tdbtorture.o
PRIVATE_DEPENDENCIES = \
LIBTDB
# End BINARY tdbtorture
################################################
tdbtorture_OBJ_FILES = $(tdbsrcdir)/tools/tdbtorture.o
################################################
# Start BINARY tdbdump
[BINARY::tdbdump]
INSTALLDIR = BINDIR
OBJ_FILES= \
tools/tdbdump.o
PRIVATE_DEPENDENCIES = \
LIBTDB
# End BINARY tdbdump
################################################
tdbdump_OBJ_FILES = $(tdbsrcdir)/tools/tdbdump.o
################################################
# Start BINARY tdbbackup
[BINARY::tdbbackup]
INSTALLDIR = BINDIR
OBJ_FILES= \
tools/tdbbackup.o
PRIVATE_DEPENDENCIES = \
LIBTDB
# End BINARY tdbbackup
################################################
tdbbackup_OBJ_FILES = $(tdbsrcdir)/tools/tdbbackup.o

View File

@ -1,10 +1,10 @@
#! /bin/sh
# Configuration validation subroutine script.
# Copyright (C) 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999,
# 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009
# 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008
# Free Software Foundation, Inc.
timestamp='2009-06-11'
timestamp='2009-04-17'
# This file is (in principle) common to ALL GNU software.
# The presence of a machine in this file suggests that SOME GNU software
@ -153,9 +153,6 @@ case $os in
os=
basic_machine=$1
;;
-bluegene*)
os=-cnk
;;
-sim | -cisco | -oki | -wec | -winbond)
os=
basic_machine=$1
@ -470,10 +467,6 @@ case $basic_machine in
basic_machine=bfin-`echo $basic_machine | sed 's/^[^-]*-//'`
os=-linux
;;
bluegene*)
basic_machine=powerpc-ibm
os=-cnk
;;
c90)
basic_machine=c90-cray
os=-unicos
@ -1267,7 +1260,7 @@ case $os in
# Each alternative MUST END IN A *, to match a version number.
# -sysv* is not here because it comes later, after sysvr4.
-gnu* | -bsd* | -mach* | -minix* | -genix* | -ultrix* | -irix* \
| -*vms* | -sco* | -esix* | -isc* | -aix* | -cnk* | -sunos | -sunos[34]*\
| -*vms* | -sco* | -esix* | -isc* | -aix* | -sunos | -sunos[34]*\
| -hpux* | -unos* | -osf* | -luna* | -dgux* | -solaris* | -sym* \
| -kopensolaris* \
| -amigaos* | -amigados* | -msdos* | -newsos* | -unicos* | -aof* \
@ -1620,7 +1613,7 @@ case $basic_machine in
-sunos*)
vendor=sun
;;
-cnk*|-aix*)
-aix*)
vendor=ibm
;;
-beos*)

View File

@ -2,14 +2,43 @@ AC_PREREQ(2.50)
AC_DEFUN([SMB_MODULE_DEFAULT], [echo -n ""])
AC_DEFUN([SMB_LIBRARY_ENABLE], [echo -n ""])
AC_DEFUN([SMB_ENABLE], [echo -n ""])
AC_INIT(tdb, 1.1.1)
AC_INIT(tdb, 1.2.0)
AC_CONFIG_SRCDIR([common/tdb.c])
AC_CONFIG_HEADER(include/config.h)
AC_LIBREPLACE_ALL_CHECKS
AC_LD_SONAMEFLAG
AC_LD_VERSIONSCRIPT
AC_LD_PICFLAG
AC_LD_SHLIBEXT
AC_LIBREPLACE_SHLD
AC_LIBREPLACE_SHLD_FLAGS
AC_LIBREPLACE_RUNTIME_LIB_PATH_VAR
m4_include(libtdb.m4)
AC_PATH_PROGS([PYTHON_CONFIG], [python2.6-config python2.5-config python2.4-config python-config])
AC_PATH_PROGS([PYTHON], [python2.6 python2.5 python2.4 python])
PYTHON_BUILD_TARGET="build-python"
PYTHON_INSTALL_TARGET="install-python"
PYTHON_CHECK_TARGET="check-python"
AC_SUBST(PYTHON_BUILD_TARGET)
AC_SUBST(PYTHON_INSTALL_TARGET)
AC_SUBST(PYTHON_CHECK_TARGET)
if test -z "$PYTHON_CONFIG"; then
PYTHON_BUILD_TARGET=""
PYTHON_INSTALL_TARGET=""
PYTHON_CHECK_TARGET=""
fi
AC_ARG_ENABLE(python,
AS_HELP_STRING([--enable-python], [Enables python binding]),
[ if test "x$enableval" = "xno" ; then
PYTHON_BUILD_TARGET=""
PYTHON_INSTALL_TARGET=""
PYTHON_CHECK_TARGET=""
fi
])
m4_include(build_macros.m4)
BUILD_WITH_SHARED_BUILD_DIR
AC_OUTPUT(Makefile tdb.pc)

View File

@ -69,11 +69,15 @@ TDB_CONTEXT *tdb_open(char *name, int hash_size, int tdb_flags,
TDB_NOLOCK - don't do any locking
TDB_NOMMAP - don't use mmap
TDB_NOSYNC - don't synchronise transactions to disk
TDB_SEQNUM - maintain a sequence number
TDB_VOLATILE - activate the per-hashchain freelist, default 5
TDB_ALLOW_NESTING - allow transactions to nest
TDB_DISALLOW_NESTING - disallow transactions to nest
----------------------------------------------------------------------
TDB_CONTEXT *tdb_open_ex(char *name, int hash_size, int tdb_flags,
int open_flags, mode_t mode,
tdb_log_func log_fn,
const struct tdb_logging_context *log_ctx,
tdb_hash_func hash_fn)
This is like tdb_open(), but allows you to pass an initial logging and
@ -92,13 +96,6 @@ int tdb_close(TDB_CONTEXT *tdb);
close a database
----------------------------------------------------------------------
int tdb_update(TDB_CONTEXT *tdb, TDB_DATA key, TDB_DATA dbuf);
update an entry in place - this only works if the new data size
is <= the old data size and the key exists.
on failure return -1
----------------------------------------------------------------------
TDB_DATA tdb_fetch(TDB_CONTEXT *tdb, TDB_DATA key);
@ -244,3 +241,14 @@ int tdb_transaction_prepare_commit(TDB_CONTEXT *tdb)
tdb_transaction_commit() or tdb_transaction_cancel(). Preparing
allocates disk space for the pending updates, so a subsequent
commit should succeed (barring any hardware failures).
----------------------------------------------------------------------
int tdb_check(TDB_CONTEXT *tdb,
int (*check)(TDB_DATA key, TDB_DATA data, void *private_data),
void *private_data);)
check the consistency of the database, calling back the check function
(if non-NULL) with each record. If some consistency check fails, or
the supplied check function returns -1, tdb_check returns -1, otherwise
0. Note that logging function (if set) will be called with additional
information on the corruption found.

View File

@ -0,0 +1,46 @@
How And Why To Use TDB Tracing
==============================
You can trace all TDB operations, using TDB_TRACE. It is not complete
(error conditions which expect to the logged will not always be traced
correctly, so you should set up a logging function too), but is designed
to collect benchmark-style traces to allow us to optimize TDB.
Note: tracing is not efficient, and the trace files are huge: a
traverse of the database is particularly large! But they compress very
well with rzip (http://rzip.samba.org)
How to gather trace files:
--------------------------
1) Uncomment /* #define TDB_TRACE 1 */ in tdb_private.h.
2) Rebuild TDB, and everything that uses it.
3) Run something.
Your trace files will be called <tdbname>.trace.<pid>. These files
will not be overwritten: if the same process reopens the same TDB, an
error will be logged and tracing will be disabled.
How to replay trace files:
--------------------------
1) For benchmarking, remember to rebuild tdb with #define TDB_TRACE commented
out again!
2) Grab the latest "replace_trace.c" from CCAN's tdb module (tools/ dir):
http://ccan.ozlabs.org/tarballs/tdb.tar.bz2
3) Compile up replay_trace, munging as necessary.
4) Run replay_trace <scratch-tdb-name> <tracefiles>...
If given more than one trace file (presumably from the same tdb)
replay_trace will try to figure out the dependencies between the operations
and fire off a child to run each trace. Occasionally it gets stuck, in
which case it will add another dependency and retry. Eventually it will
give a speed value.
replay_trace can intuit the existence of previous data in the tdb (ie.
activity prior to the trace(s) supplied) and will prepopulate as
neccessary.
You can run --quiet for straight benchmark results, and -n to run multiple
times (this saves time, since it need only calculate dependencies once).
Good luck!
Rusty Russell <rusty@rustcorp.com.au>

View File

@ -1,708 +0,0 @@
/* include/config.h.in. Generated from configure.ac by autoheader. */
/* Whether strndup is broken */
#undef BROKEN_STRNDUP
/* Whether strnlen is broken */
#undef BROKEN_STRNLEN
/* Define to 1 if you have the <arpa/inet.h> header file. */
#undef HAVE_ARPA_INET_H
/* Define to 1 if you have the `asprintf' function. */
#undef HAVE_ASPRINTF
/* Whether the bool type is available */
#undef HAVE_BOOL
/* Define to 1 if you have the `bzero' function. */
#undef HAVE_BZERO
/* Whether there is a C99 compliant vsnprintf */
#undef HAVE_C99_VSNPRINTF
/* Define to 1 if you have the `chroot' function. */
#undef HAVE_CHROOT
/* Define to 1 if you have the `chsize' function. */
#undef HAVE_CHSIZE
/* Whether or not we have comparison_fn_t */
#undef HAVE_COMPARISON_FN_T
/* Define to 1 if you have the <compat.h> header file. */
#undef HAVE_COMPAT_H
/* Define to 1 if you have the <ctype.h> header file. */
#undef HAVE_CTYPE_H
/* Define to 1 if you have the declaration of `asprintf', and to 0 if you
don't. */
#undef HAVE_DECL_ASPRINTF
/* Define to 1 if you have the declaration of `snprintf', and to 0 if you
don't. */
#undef HAVE_DECL_SNPRINTF
/* Define to 1 if you have the declaration of `vasprintf', and to 0 if you
don't. */
#undef HAVE_DECL_VASPRINTF
/* Define to 1 if you have the declaration of `vsnprintf', and to 0 if you
don't. */
#undef HAVE_DECL_VSNPRINTF
/* Define to 1 if you have the <direct.h> header file. */
#undef HAVE_DIRECT_H
/* Define to 1 if you have the <dirent.h> header file, and it defines `DIR'.
*/
#undef HAVE_DIRENT_H
/* Define to 1 if you have the `dlclose' function. */
#undef HAVE_DLCLOSE
/* Define to 1 if you have the `dlerror' function. */
#undef HAVE_DLERROR
/* Define to 1 if you have the <dlfcn.h> header file. */
#undef HAVE_DLFCN_H
/* Define to 1 if you have the `dlopen' function. */
#undef HAVE_DLOPEN
/* Define to 1 if you have the `dlsym' function. */
#undef HAVE_DLSYM
/* Define to 1 if you have the `endnetgrent' function. */
#undef HAVE_ENDNETGRENT
/* Whether errno() is available */
#undef HAVE_ERRNO_DECL
/* Define to 1 if you have the <fcntl.h> header file. */
#undef HAVE_FCNTL_H
/* Define to 1 if you have the <fnmatch.h> header file. */
#undef HAVE_FNMATCH_H
/* Define to 1 if you have the `ftruncate' function. */
#undef HAVE_FTRUNCATE
/* Whether there is a __FUNCTION__ macro */
#undef HAVE_FUNCTION_MACRO
/* Define to 1 if you have the `getdents' function. */
#undef HAVE_GETDENTS
/* Define to 1 if you have the `getdirentries' function. */
#undef HAVE_GETDIRENTRIES
/* Define to 1 if you have the `getnetgrent' function. */
#undef HAVE_GETNETGRENT
/* Define to 1 if you have the <getopt.h> header file. */
#undef HAVE_GETOPT_H
/* Define to 1 if you have the `getpagesize' function. */
#undef HAVE_GETPAGESIZE
/* Define to 1 if you have the `getpgrp' function. */
#undef HAVE_GETPGRP
/* Define to 1 if you have the <grp.h> header file. */
#undef HAVE_GRP_H
/* Whether the compiler supports immediate structures */
#undef HAVE_IMMEDIATE_STRUCTURES
/* Define to 1 if you have the `initgroups' function. */
#undef HAVE_INITGROUPS
/* Define to 1 if you have the `innetgr' function. */
#undef HAVE_INNETGR
/* Define to 1 if you have the <inttypes.h> header file. */
#undef HAVE_INTTYPES_H
/* Define to 1 if you have the <limits.h> header file. */
#undef HAVE_LIMITS_H
/* Define to 1 if you have the <locale.h> header file. */
#undef HAVE_LOCALE_H
/* Define to 1 if the system has the type `long long'. */
#undef HAVE_LONG_LONG
/* Define to 1 if you have the `lstat' function. */
#undef HAVE_LSTAT
/* Define to 1 if you have the `memcpy' function. */
#undef HAVE_MEMCPY
/* Define to 1 if you have the `memmove' function. */
#undef HAVE_MEMMOVE
/* Define to 1 if you have the <memory.h> header file. */
#undef HAVE_MEMORY_H
/* Define to 1 if you have the `memset' function. */
#undef HAVE_MEMSET
/* Define if target mkdir supports mode option */
#undef HAVE_MKDIR_MODE
/* Define to 1 if you have the `mkdtemp' function. */
#undef HAVE_MKDTEMP
/* Define to 1 if you have the `mktime' function. */
#undef HAVE_MKTIME
/* Define to 1 if you have the `mmap' function. */
#undef HAVE_MMAP
/* Define to 1 if you have the <ndir.h> header file, and it defines `DIR'. */
#undef HAVE_NDIR_H
/* Define to 1 if you have the <netdb.h> header file. */
#undef HAVE_NETDB_H
/* Define to 1 if you have the <netinet/in.h> header file. */
#undef HAVE_NETINET_IN_H
/* Define to 1 if you have the <netinet/in_ip.h> header file. */
#undef HAVE_NETINET_IN_IP_H
/* Define to 1 if you have the <netinet/in_systm.h> header file. */
#undef HAVE_NETINET_IN_SYSTM_H
/* Define to 1 if you have the <netinet/ip.h> header file. */
#undef HAVE_NETINET_IP_H
/* Define to 1 if you have the <netinet/tcp.h> header file. */
#undef HAVE_NETINET_TCP_H
/* usability of net/if.h */
#undef HAVE_NET_IF_H
/* Whether the open(2) accepts O_DIRECT */
#undef HAVE_OPEN_O_DIRECT
/* Define to 1 if you have the `pipe' function. */
#undef HAVE_PIPE
/* Define to 1 if you have the `pread' function. */
#undef HAVE_PREAD
/* Whether pread() is available */
#undef HAVE_PREAD_DECL
/* Define to 1 if you have the `printf' function. */
#undef HAVE_PRINTF
/* Define to 1 if you have the <pwd.h> header file. */
#undef HAVE_PWD_H
/* Define to 1 if you have the `pwrite' function. */
#undef HAVE_PWRITE
/* Whether pwrite() is available */
#undef HAVE_PWRITE_DECL
/* Define to 1 if you have the `rand' function. */
#undef HAVE_RAND
/* Define to 1 if you have the `random' function. */
#undef HAVE_RANDOM
/* Define to 1 if you have the `rename' function. */
#undef HAVE_RENAME
/* Whether mkstemp is secure */
#undef HAVE_SECURE_MKSTEMP
/* Define to 1 if you have the `setbuffer' function. */
#undef HAVE_SETBUFFER
/* Define to 1 if you have the `setegid' function. */
#undef HAVE_SETEGID
/* Define to 1 if you have the `setenv' function. */
#undef HAVE_SETENV
/* Define to 1 if you have the `seteuid' function. */
#undef HAVE_SETEUID
/* Define to 1 if you have the <setjmp.h> header file. */
#undef HAVE_SETJMP_H
/* Define to 1 if you have the `setlinebuf' function. */
#undef HAVE_SETLINEBUF
/* Define to 1 if you have the `setnetgrent' function. */
#undef HAVE_SETNETGRENT
/* Define to 1 if you have the `setresgid' function. */
#undef HAVE_SETRESGID
/* Whether setresgid() is available */
#undef HAVE_SETRESGID_DECL
/* Define to 1 if you have the `setresuid' function. */
#undef HAVE_SETRESUID
/* Whether setresuid() is available */
#undef HAVE_SETRESUID_DECL
/* Define to 1 if you have the <shadow.h> header file. */
#undef HAVE_SHADOW_H
/* Whether we have the atomic_t variable type */
#undef HAVE_SIG_ATOMIC_T_TYPE
/* Define to 1 if you have the `snprintf' function. */
#undef HAVE_SNPRINTF
/* Define to 1 if you have the `socketpair' function. */
#undef HAVE_SOCKETPAIR
/* Define to 1 if you have the `srand' function. */
#undef HAVE_SRAND
/* Define to 1 if you have the `srandom' function. */
#undef HAVE_SRANDOM
/* Define to 1 if you have the <standards.h> header file. */
#undef HAVE_STANDARDS_H
/* Define to 1 if you have the <stdarg.h> header file. */
#undef HAVE_STDARG_H
/* Define to 1 if you have the <stdbool.h> header file. */
#undef HAVE_STDBOOL_H
/* Define to 1 if you have the <stdint.h> header file. */
#undef HAVE_STDINT_H
/* Define to 1 if you have the <stdio.h> header file. */
#undef HAVE_STDIO_H
/* Define to 1 if you have the <stdlib.h> header file. */
#undef HAVE_STDLIB_H
/* Define to 1 if you have the `strcasestr' function. */
#undef HAVE_STRCASESTR
/* Define to 1 if you have the `strdup' function. */
#undef HAVE_STRDUP
/* Define to 1 if you have the `strerror' function. */
#undef HAVE_STRERROR
/* Define to 1 if you have the `strftime' function. */
#undef HAVE_STRFTIME
/* Define to 1 if you have the <strings.h> header file. */
#undef HAVE_STRINGS_H
/* Define to 1 if you have the <string.h> header file. */
#undef HAVE_STRING_H
/* Define to 1 if you have the `strlcat' function. */
#undef HAVE_STRLCAT
/* Define to 1 if you have the `strlcpy' function. */
#undef HAVE_STRLCPY
/* Define to 1 if you have the `strndup' function. */
#undef HAVE_STRNDUP
/* Define to 1 if you have the `strnlen' function. */
#undef HAVE_STRNLEN
/* Define to 1 if you have the `strtok_r' function. */
#undef HAVE_STRTOK_R
/* Define to 1 if you have the `strtoll' function. */
#undef HAVE_STRTOLL
/* Define to 1 if you have the `strtoq' function. */
#undef HAVE_STRTOQ
/* Define to 1 if you have the `strtoull' function. */
#undef HAVE_STRTOULL
/* Define to 1 if you have the `strtouq' function. */
#undef HAVE_STRTOUQ
/* Define to 1 if `st_rdev' is member of `struct stat'. */
#undef HAVE_STRUCT_STAT_ST_RDEV
/* Define to 1 if your `struct stat' has `st_rdev'. Deprecated, use
`HAVE_STRUCT_STAT_ST_RDEV' instead. */
#undef HAVE_ST_RDEV
/* Define to 1 if you have the `syslog' function. */
#undef HAVE_SYSLOG
/* Define to 1 if you have the <syslog.h> header file. */
#undef HAVE_SYSLOG_H
/* Define to 1 if you have the <sys/acl.h> header file. */
#undef HAVE_SYS_ACL_H
/* Define to 1 if you have the <sys/capability.h> header file. */
#undef HAVE_SYS_CAPABILITY_H
/* Define to 1 if you have the <sys/dir.h> header file, and it defines `DIR'.
*/
#undef HAVE_SYS_DIR_H
/* Define to 1 if you have the <sys/fcntl.h> header file. */
#undef HAVE_SYS_FCNTL_H
/* Define to 1 if you have the <sys/filio.h> header file. */
#undef HAVE_SYS_FILIO_H
/* Define to 1 if you have the <sys/filsys.h> header file. */
#undef HAVE_SYS_FILSYS_H
/* Define to 1 if you have the <sys/fs/s5param.h> header file. */
#undef HAVE_SYS_FS_S5PARAM_H
/* Define to 1 if you have the <sys/id.h> header file. */
#undef HAVE_SYS_ID_H
/* Define to 1 if you have the <sys/ioctl.h> header file. */
#undef HAVE_SYS_IOCTL_H
/* Define to 1 if you have the <sys/ipc.h> header file. */
#undef HAVE_SYS_IPC_H
/* Define to 1 if you have the <sys/mman.h> header file. */
#undef HAVE_SYS_MMAN_H
/* Define to 1 if you have the <sys/mode.h> header file. */
#undef HAVE_SYS_MODE_H
/* Define to 1 if you have the <sys/ndir.h> header file, and it defines `DIR'.
*/
#undef HAVE_SYS_NDIR_H
/* Define to 1 if you have the <sys/param.h> header file. */
#undef HAVE_SYS_PARAM_H
/* Define to 1 if you have the <sys/priv.h> header file. */
#undef HAVE_SYS_PRIV_H
/* Define to 1 if you have the <sys/resource.h> header file. */
#undef HAVE_SYS_RESOURCE_H
/* Define to 1 if you have the <sys/security.h> header file. */
#undef HAVE_SYS_SECURITY_H
/* Define to 1 if you have the <sys/select.h> header file. */
#undef HAVE_SYS_SELECT_H
/* Define to 1 if you have the <sys/shm.h> header file. */
#undef HAVE_SYS_SHM_H
/* Define to 1 if you have the <sys/socket.h> header file. */
#undef HAVE_SYS_SOCKET_H
/* Define to 1 if you have the <sys/sockio.h> header file. */
#undef HAVE_SYS_SOCKIO_H
/* Define to 1 if you have the <sys/stat.h> header file. */
#undef HAVE_SYS_STAT_H
/* Define to 1 if you have the <sys/syslog.h> header file. */
#undef HAVE_SYS_SYSLOG_H
/* Define to 1 if you have the <sys/termio.h> header file. */
#undef HAVE_SYS_TERMIO_H
/* Define to 1 if you have the <sys/time.h> header file. */
#undef HAVE_SYS_TIME_H
/* Define to 1 if you have the <sys/types.h> header file. */
#undef HAVE_SYS_TYPES_H
/* Define to 1 if you have the <sys/un.h> header file. */
#undef HAVE_SYS_UN_H
/* Define to 1 if you have <sys/wait.h> that is POSIX.1 compatible. */
#undef HAVE_SYS_WAIT_H
/* Define to 1 if you have the <termios.h> header file. */
#undef HAVE_TERMIOS_H
/* Define to 1 if you have the <termio.h> header file. */
#undef HAVE_TERMIO_H
/* Define to 1 if you have the `timegm' function. */
#undef HAVE_TIMEGM
/* Define to 1 if you have the <time.h> header file. */
#undef HAVE_TIME_H
/* Define to 1 if you have the <unistd.h> header file. */
#undef HAVE_UNISTD_H
/* Define to 1 if you have the `unsetenv' function. */
#undef HAVE_UNSETENV
/* Define to 1 if you have the `usleep' function. */
#undef HAVE_USLEEP
/* Define to 1 if you have the `utime' function. */
#undef HAVE_UTIME
/* Define to 1 if you have the <utime.h> header file. */
#undef HAVE_UTIME_H
/* Define to 1 if you have the <vararg.h> header file. */
#undef HAVE_VARARG_H
/* Define to 1 if you have the `vasprintf' function. */
#undef HAVE_VASPRINTF
/* Whether va_copy() is available */
#undef HAVE_VA_COPY
/* Whether the C compiler understands volatile */
#undef HAVE_VOLATILE
/* Define to 1 if you have the `vsnprintf' function. */
#undef HAVE_VSNPRINTF
/* Define to 1 if you have the `vsyslog' function. */
#undef HAVE_VSYSLOG
/* Define to 1 if you have the `waitpid' function. */
#undef HAVE_WAITPID
/* Define to 1 if you have the <windows.h> header file. */
#undef HAVE_WINDOWS_H
/* Define to 1 if you have the <winsock2.h> header file. */
#undef HAVE_WINSOCK2_H
/* Define to 1 if you have the <ws2tcpip.h> header file. */
#undef HAVE_WS2TCPIP_H
/* Whether the _Bool type is available */
#undef HAVE__Bool
/* Whether the __VA_ARGS__ macro is available */
#undef HAVE__VA_ARGS__MACRO
/* Define to 1 if you have the `__strtoll' function. */
#undef HAVE___STRTOLL
/* Define to 1 if you have the `__strtoull' function. */
#undef HAVE___STRTOULL
/* Whether __va_copy() is available */
#undef HAVE___VA_COPY
/* Whether there is a __func__ macro */
#undef HAVE_func_MACRO
/* Whether MMAP is broken */
#undef MMAP_BLACKLIST
/* Define to the address where bug reports for this package should be sent. */
#undef PACKAGE_BUGREPORT
/* Define to the full name of this package. */
#undef PACKAGE_NAME
/* Define to the full name and version of this package. */
#undef PACKAGE_STRING
/* Define to the one symbol short name of this package. */
#undef PACKAGE_TARNAME
/* Define to the version of this package. */
#undef PACKAGE_VERSION
/* Whether getpass should be replaced */
#undef REPLACE_GETPASS
/* Whether inet_ntoa should be replaced */
#undef REPLACE_INET_NTOA
/* replace readdir */
#undef REPLACE_READDIR
/* replace readdir using getdents() */
#undef REPLACE_READDIR_GETDENTS
/* replace readdir using getdirentries() */
#undef REPLACE_READDIR_GETDIRENTRIES
/* Define as the return type of signal handlers (`int' or `void'). */
#undef RETSIGTYPE
/* Whether seekdir returns an int */
#undef SEEKDIR_RETURNS_INT
/* The size of `char', as computed by sizeof. */
#undef SIZEOF_CHAR
/* The size of `int', as computed by sizeof. */
#undef SIZEOF_INT
/* The size of `long', as computed by sizeof. */
#undef SIZEOF_LONG
/* The size of `long long', as computed by sizeof. */
#undef SIZEOF_LONG_LONG
/* The size of `off_t', as computed by sizeof. */
#undef SIZEOF_OFF_T
/* The size of `short', as computed by sizeof. */
#undef SIZEOF_SHORT
/* The size of `size_t', as computed by sizeof. */
#undef SIZEOF_SIZE_T
/* The size of `ssize_t', as computed by sizeof. */
#undef SIZEOF_SSIZE_T
/* Define to 1 if you have the ANSI C header files. */
#undef STDC_HEADERS
/* Whether telldir takes a const pointer */
#undef TELLDIR_TAKES_CONST_DIR
/* Define to 1 if you can safely include both <sys/time.h> and <time.h>. */
#undef TIME_WITH_SYS_TIME
/* Define to 1 if your processor stores words with the most significant byte
first (like Motorola and SPARC, unlike Intel and VAX). */
#undef WORDS_BIGENDIAN
/* Define to 1 if on AIX 3.
System headers sometimes define this.
We just want to avoid a redefinition error message. */
#ifndef _ALL_SOURCE
# undef _ALL_SOURCE
#endif
/* Number of bits in a file offset, on hosts where this is settable. */
#undef _FILE_OFFSET_BITS
/* Enable GNU extensions on systems that have them. */
#ifndef _GNU_SOURCE
# undef _GNU_SOURCE
#endif
/* Define for large files, on AIX-style hosts. */
#undef _LARGE_FILES
/* Define to 1 if on MINIX. */
#undef _MINIX
#ifndef _OSF_SOURCE
# define _OSF_SOURCE 1
#endif
/* Define to 2 if the system does not provide POSIX.1 features except with
this defined. */
#undef _POSIX_1_SOURCE
/* Whether to enable POSIX support */
#undef _POSIX_C_SOURCE
/* Define to 1 if you need to in order for `stat' and other things to work. */
#undef _POSIX_SOURCE
/* Whether to enable System V compatibility */
#undef _SYSV
#ifndef _XOPEN_SOURCE_EXTENDED
# define _XOPEN_SOURCE_EXTENDED 1
#endif
/* Enable extensions on Solaris. */
#ifndef __EXTENSIONS__
# undef __EXTENSIONS__
#endif
#ifndef _POSIX_PTHREAD_SEMANTICS
# undef _POSIX_PTHREAD_SEMANTICS
#endif
/* Define to `int' if <sys/types.h> doesn't define. */
#undef gid_t
/* Define to `__inline__' or `__inline' if that's what the C compiler
calls it, or to nothing if 'inline' is not supported under any name. */
#ifndef __cplusplus
#undef inline
#endif
/* Define to `unsigned' if <sys/types.h> does not define. */
#undef ino_t
/* Define to `short' if <sys/types.h> does not define. */
#undef int16_t
/* Define to `long' if <sys/types.h> does not define. */
#undef int32_t
/* Define to `long long' if <sys/types.h> does not define. */
#undef int64_t
/* Define to `char' if <sys/types.h> does not define. */
#undef int8_t
/* Define to `unsigned long long' if <sys/types.h> does not define. */
#undef intptr_t
/* Define to `off_t' if <sys/types.h> does not define. */
#undef loff_t
/* Define to `int' if <sys/types.h> does not define. */
#undef mode_t
/* Define to `long int' if <sys/types.h> does not define. */
#undef off_t
/* Define to `loff_t' if <sys/types.h> does not define. */
#undef offset_t
/* Define to `int' if <sys/types.h> does not define. */
#undef pid_t
/* Define to `unsigned long long' if <sys/types.h> does not define. */
#undef ptrdiff_t
/* Define to `unsigned int' if <sys/types.h> does not define. */
#undef size_t
/* Socket length type */
#undef socklen_t
/* Define to `int' if <sys/types.h> does not define. */
#undef ssize_t
/* Define to `int' if <sys/types.h> doesn't define. */
#undef uid_t
/* Define to `unsigned short' if <sys/types.h> does not define. */
#undef uint16_t
/* Define to `unsigned long' if <sys/types.h> does not define. */
#undef uint32_t
/* Define to `unsigned long long' if <sys/types.h> does not define. */
#undef uint64_t
/* Define to `unsigned char' if <sys/types.h> does not define. */
#undef uint8_t
/* Define to `unsigned int' if <sys/types.h> does not define. */
#undef uint_t

View File

@ -30,6 +30,7 @@
extern "C" {
#endif
#include "signal.h"
/* flags to tdb_store() */
#define TDB_REPLACE 1 /* Unused */
@ -48,8 +49,7 @@ extern "C" {
#define TDB_SEQNUM 128 /* maintain a sequence number */
#define TDB_VOLATILE 256 /* Activate the per-hashchain freelist, default 5 */
#define TDB_ALLOW_NESTING 512 /* Allow transactions to nest */
#define TDB_ERRCODE(code, ret) ((tdb->ecode = (code)), ret)
#define TDB_DISALLOW_NESTING 1024 /* Disallow transactions to nest */
/* error codes */
enum TDB_ERROR {TDB_SUCCESS=0, TDB_ERR_CORRUPT, TDB_ERR_IO, TDB_ERR_LOCK,
@ -142,6 +142,9 @@ void tdb_add_flags(struct tdb_context *tdb, unsigned flag);
void tdb_remove_flags(struct tdb_context *tdb, unsigned flag);
void tdb_enable_seqnum(struct tdb_context *tdb);
void tdb_increment_seqnum_nonblock(struct tdb_context *tdb);
int tdb_check(struct tdb_context *tdb,
int (*check)(TDB_DATA key, TDB_DATA data, void *private_data),
void *private_data);
/* Low level locking functions: use with care */
int tdb_chainlock(struct tdb_context *tdb, TDB_DATA key);

View File

@ -1,9 +1,9 @@
dnl find the tdb sources. This is meant to work both for
dnl tdb standalone builds, and builds of packages using tdb
tdbdir=""
tdbpaths="$srcdir $srcdir/lib/tdb $srcdir/tdb $srcdir/../tdb"
tdbpaths=". lib/tdb tdb ../tdb ../lib/tdb"
for d in $tdbpaths; do
if test -f "$d/common/tdb.c"; then
if test -f "$srcdir/$d/common/tdb.c"; then
tdbdir="$d"
AC_SUBST(tdbdir)
break;
@ -13,7 +13,7 @@ if test x"$tdbdir" = "x"; then
AC_MSG_ERROR([cannot find tdb source in $tdbpaths])
fi
TDB_OBJ="common/tdb.o common/dump.o common/transaction.o common/error.o common/traverse.o"
TDB_OBJ="$TDB_OBJ common/freelist.o common/freelistcheck.o common/io.o common/lock.o common/open.o"
TDB_OBJ="$TDB_OBJ common/freelist.o common/freelistcheck.o common/io.o common/lock.o common/open.o common/check.o"
AC_SUBST(TDB_OBJ)
AC_SUBST(LIBREPLACEOBJ)
@ -28,3 +28,8 @@ AC_CHECK_HEADERS(getopt.h sys/select.h sys/time.h)
AC_HAVE_DECL(pread, [#include <unistd.h>])
AC_HAVE_DECL(pwrite, [#include <unistd.h>])
if test x"$VERSIONSCRIPT" != "x"; then
EXPORTSFILE=tdb.exports
AC_SUBST(EXPORTSFILE)
fi

516
ctdb/lib/tdb/pytdb.c Normal file
View File

@ -0,0 +1,516 @@
/*
Unix SMB/CIFS implementation.
Python interface to tdb.
Copyright (C) 2004-2006 Tim Potter <tpot@samba.org>
Copyright (C) 2007-2008 Jelmer Vernooij <jelmer@samba.org>
** NOTE! The following LGPL license applies to the tdb
** library. This does NOT imply that all of Samba is released
** under the LGPL
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 3 of the License, or (at your option) any later version.
This library 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
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, see <http://www.gnu.org/licenses/>.
*/
#include "replace.h"
#include "system/filesys.h"
#include <Python.h>
#ifndef Py_RETURN_NONE
#define Py_RETURN_NONE return Py_INCREF(Py_None), Py_None
#endif
/* Include tdb headers */
#include <tdb.h>
typedef struct {
PyObject_HEAD
TDB_CONTEXT *ctx;
bool closed;
} PyTdbObject;
PyAPI_DATA(PyTypeObject) PyTdb;
static void PyErr_SetTDBError(TDB_CONTEXT *tdb)
{
PyErr_SetObject(PyExc_RuntimeError,
Py_BuildValue("(i,s)", tdb_error(tdb), tdb_errorstr(tdb)));
}
static TDB_DATA PyString_AsTDB_DATA(PyObject *data)
{
TDB_DATA ret;
ret.dptr = (unsigned char *)PyString_AsString(data);
ret.dsize = PyString_Size(data);
return ret;
}
static PyObject *PyString_FromTDB_DATA(TDB_DATA data)
{
if (data.dptr == NULL && data.dsize == 0) {
Py_RETURN_NONE;
} else {
PyObject *ret = PyString_FromStringAndSize((const char *)data.dptr,
data.dsize);
free(data.dptr);
return ret;
}
}
#define PyErr_TDB_ERROR_IS_ERR_RAISE(ret, tdb) \
if (ret != 0) { \
PyErr_SetTDBError(tdb); \
return NULL; \
}
static PyObject *py_tdb_open(PyTypeObject *type, PyObject *args, PyObject *kwargs)
{
char *name;
int hash_size = 0, tdb_flags = TDB_DEFAULT, flags = O_RDWR, mode = 0600;
TDB_CONTEXT *ctx;
PyTdbObject *ret;
const char *kwnames[] = { "name", "hash_size", "tdb_flags", "flags", "mode", NULL };
if (!PyArg_ParseTupleAndKeywords(args, kwargs, "s|iiii", (char **)kwnames, &name, &hash_size, &tdb_flags, &flags, &mode))
return NULL;
ctx = tdb_open(name, hash_size, tdb_flags, flags, mode);
if (ctx == NULL) {
PyErr_SetFromErrno(PyExc_IOError);
return NULL;
}
ret = PyObject_New(PyTdbObject, &PyTdb);
ret->ctx = ctx;
ret->closed = false;
return (PyObject *)ret;
}
static PyObject *obj_transaction_cancel(PyTdbObject *self)
{
int ret = tdb_transaction_cancel(self->ctx);
PyErr_TDB_ERROR_IS_ERR_RAISE(ret, self->ctx);
Py_RETURN_NONE;
}
static PyObject *obj_transaction_commit(PyTdbObject *self)
{
int ret = tdb_transaction_commit(self->ctx);
PyErr_TDB_ERROR_IS_ERR_RAISE(ret, self->ctx);
Py_RETURN_NONE;
}
static PyObject *obj_transaction_recover(PyTdbObject *self)
{
int ret = tdb_transaction_recover(self->ctx);
PyErr_TDB_ERROR_IS_ERR_RAISE(ret, self->ctx);
Py_RETURN_NONE;
}
static PyObject *obj_transaction_start(PyTdbObject *self)
{
int ret = tdb_transaction_start(self->ctx);
PyErr_TDB_ERROR_IS_ERR_RAISE(ret, self->ctx);
Py_RETURN_NONE;
}
static PyObject *obj_reopen(PyTdbObject *self)
{
int ret = tdb_reopen(self->ctx);
PyErr_TDB_ERROR_IS_ERR_RAISE(ret, self->ctx);
Py_RETURN_NONE;
}
static PyObject *obj_lockall(PyTdbObject *self)
{
int ret = tdb_lockall(self->ctx);
PyErr_TDB_ERROR_IS_ERR_RAISE(ret, self->ctx);
Py_RETURN_NONE;
}
static PyObject *obj_unlockall(PyTdbObject *self)
{
int ret = tdb_unlockall(self->ctx);
PyErr_TDB_ERROR_IS_ERR_RAISE(ret, self->ctx);
Py_RETURN_NONE;
}
static PyObject *obj_lockall_read(PyTdbObject *self)
{
int ret = tdb_lockall_read(self->ctx);
PyErr_TDB_ERROR_IS_ERR_RAISE(ret, self->ctx);
Py_RETURN_NONE;
}
static PyObject *obj_unlockall_read(PyTdbObject *self)
{
int ret = tdb_unlockall_read(self->ctx);
PyErr_TDB_ERROR_IS_ERR_RAISE(ret, self->ctx);
Py_RETURN_NONE;
}
static PyObject *obj_close(PyTdbObject *self)
{
int ret;
if (self->closed)
Py_RETURN_NONE;
ret = tdb_close(self->ctx);
self->closed = true;
PyErr_TDB_ERROR_IS_ERR_RAISE(ret, self->ctx);
Py_RETURN_NONE;
}
static PyObject *obj_get(PyTdbObject *self, PyObject *args)
{
TDB_DATA key;
PyObject *py_key;
if (!PyArg_ParseTuple(args, "O", &py_key))
return NULL;
key = PyString_AsTDB_DATA(py_key);
return PyString_FromTDB_DATA(tdb_fetch(self->ctx, key));
}
static PyObject *obj_append(PyTdbObject *self, PyObject *args)
{
TDB_DATA key, data;
PyObject *py_key, *py_data;
int ret;
if (!PyArg_ParseTuple(args, "OO", &py_key, &py_data))
return NULL;
key = PyString_AsTDB_DATA(py_key);
data = PyString_AsTDB_DATA(py_data);
ret = tdb_append(self->ctx, key, data);
PyErr_TDB_ERROR_IS_ERR_RAISE(ret, self->ctx);
Py_RETURN_NONE;
}
static PyObject *obj_firstkey(PyTdbObject *self)
{
return PyString_FromTDB_DATA(tdb_firstkey(self->ctx));
}
static PyObject *obj_nextkey(PyTdbObject *self, PyObject *args)
{
TDB_DATA key;
PyObject *py_key;
if (!PyArg_ParseTuple(args, "O", &py_key))
return NULL;
key = PyString_AsTDB_DATA(py_key);
return PyString_FromTDB_DATA(tdb_nextkey(self->ctx, key));
}
static PyObject *obj_delete(PyTdbObject *self, PyObject *args)
{
TDB_DATA key;
PyObject *py_key;
int ret;
if (!PyArg_ParseTuple(args, "O", &py_key))
return NULL;
key = PyString_AsTDB_DATA(py_key);
ret = tdb_delete(self->ctx, key);
PyErr_TDB_ERROR_IS_ERR_RAISE(ret, self->ctx);
Py_RETURN_NONE;
}
static PyObject *obj_has_key(PyTdbObject *self, PyObject *args)
{
TDB_DATA key;
int ret;
PyObject *py_key;
if (!PyArg_ParseTuple(args, "O", &py_key))
return NULL;
key = PyString_AsTDB_DATA(py_key);
ret = tdb_exists(self->ctx, key);
if (ret != TDB_ERR_NOEXIST) {
PyErr_TDB_ERROR_IS_ERR_RAISE(ret, self->ctx);
}
return (ret == TDB_ERR_NOEXIST)?Py_False:Py_True;
}
static PyObject *obj_store(PyTdbObject *self, PyObject *args)
{
TDB_DATA key, value;
int ret;
int flag = TDB_REPLACE;
PyObject *py_key, *py_value;
if (!PyArg_ParseTuple(args, "OO|i", &py_key, &py_value, &flag))
return NULL;
key = PyString_AsTDB_DATA(py_key);
value = PyString_AsTDB_DATA(py_value);
ret = tdb_store(self->ctx, key, value, flag);
PyErr_TDB_ERROR_IS_ERR_RAISE(ret, self->ctx);
Py_RETURN_NONE;
}
typedef struct {
PyObject_HEAD
TDB_DATA current;
PyTdbObject *iteratee;
} PyTdbIteratorObject;
static PyObject *tdb_iter_next(PyTdbIteratorObject *self)
{
TDB_DATA current;
PyObject *ret;
if (self->current.dptr == NULL && self->current.dsize == 0)
return NULL;
current = self->current;
self->current = tdb_nextkey(self->iteratee->ctx, self->current);
ret = PyString_FromTDB_DATA(current);
return ret;
}
static void tdb_iter_dealloc(PyTdbIteratorObject *self)
{
Py_DECREF(self->iteratee);
PyObject_Del(self);
}
PyTypeObject PyTdbIterator = {
.tp_name = "Iterator",
.tp_basicsize = sizeof(PyTdbIteratorObject),
.tp_iternext = (iternextfunc)tdb_iter_next,
.tp_dealloc = (destructor)tdb_iter_dealloc,
.tp_flags = Py_TPFLAGS_DEFAULT,
.tp_iter = PyObject_SelfIter,
};
static PyObject *tdb_object_iter(PyTdbObject *self)
{
PyTdbIteratorObject *ret;
ret = PyObject_New(PyTdbIteratorObject, &PyTdbIterator);
ret->current = tdb_firstkey(self->ctx);
ret->iteratee = self;
Py_INCREF(self);
return (PyObject *)ret;
}
static PyObject *obj_clear(PyTdbObject *self)
{
int ret = tdb_wipe_all(self->ctx);
PyErr_TDB_ERROR_IS_ERR_RAISE(ret, self->ctx);
Py_RETURN_NONE;
}
static PyMethodDef tdb_object_methods[] = {
{ "transaction_cancel", (PyCFunction)obj_transaction_cancel, METH_NOARGS,
"S.transaction_cancel() -> None\n"
"Cancel the currently active transaction." },
{ "transaction_commit", (PyCFunction)obj_transaction_commit, METH_NOARGS,
"S.transaction_commit() -> None\n"
"Commit the currently active transaction." },
{ "transaction_recover", (PyCFunction)obj_transaction_recover, METH_NOARGS,
"S.transaction_recover() -> None\n"
"Recover the currently active transaction." },
{ "transaction_start", (PyCFunction)obj_transaction_start, METH_NOARGS,
"S.transaction_start() -> None\n"
"Start a new transaction." },
{ "reopen", (PyCFunction)obj_reopen, METH_NOARGS, "Reopen this file." },
{ "lock_all", (PyCFunction)obj_lockall, METH_NOARGS, NULL },
{ "unlock_all", (PyCFunction)obj_unlockall, METH_NOARGS, NULL },
{ "read_lock_all", (PyCFunction)obj_lockall_read, METH_NOARGS, NULL },
{ "read_unlock_all", (PyCFunction)obj_unlockall_read, METH_NOARGS, NULL },
{ "close", (PyCFunction)obj_close, METH_NOARGS, NULL },
{ "get", (PyCFunction)obj_get, METH_VARARGS, "S.get(key) -> value\n"
"Fetch a value." },
{ "append", (PyCFunction)obj_append, METH_VARARGS, "S.append(key, value) -> None\n"
"Append data to an existing key." },
{ "firstkey", (PyCFunction)obj_firstkey, METH_NOARGS, "S.firstkey() -> data\n"
"Return the first key in this database." },
{ "nextkey", (PyCFunction)obj_nextkey, METH_NOARGS, "S.nextkey(key) -> data\n"
"Return the next key in this database." },
{ "delete", (PyCFunction)obj_delete, METH_VARARGS, "S.delete(key) -> None\n"
"Delete an entry." },
{ "has_key", (PyCFunction)obj_has_key, METH_VARARGS, "S.has_key(key) -> None\n"
"Check whether key exists in this database." },
{ "store", (PyCFunction)obj_store, METH_VARARGS, "S.store(key, data, flag=REPLACE) -> None"
"Store data." },
{ "iterkeys", (PyCFunction)tdb_object_iter, METH_NOARGS, "S.iterkeys() -> iterator" },
{ "clear", (PyCFunction)obj_clear, METH_NOARGS, "S.clear() -> None\n"
"Wipe the entire database." },
{ NULL }
};
static PyObject *obj_get_hash_size(PyTdbObject *self, void *closure)
{
return PyInt_FromLong(tdb_hash_size(self->ctx));
}
static int obj_set_max_dead(PyTdbObject *self, PyObject *max_dead, void *closure)
{
if (!PyInt_Check(max_dead))
return -1;
tdb_set_max_dead(self->ctx, PyInt_AsLong(max_dead));
return 0;
}
static PyObject *obj_get_map_size(PyTdbObject *self, void *closure)
{
return PyInt_FromLong(tdb_map_size(self->ctx));
}
static PyObject *obj_get_flags(PyTdbObject *self, void *closure)
{
return PyInt_FromLong(tdb_get_flags(self->ctx));
}
static PyObject *obj_get_filename(PyTdbObject *self, void *closure)
{
return PyString_FromString(tdb_name(self->ctx));
}
static PyGetSetDef tdb_object_getsetters[] = {
{ (char *)"hash_size", (getter)obj_get_hash_size, NULL, NULL },
{ (char *)"map_size", (getter)obj_get_map_size, NULL, NULL },
{ (char *)"flags", (getter)obj_get_flags, NULL, NULL },
{ (char *)"max_dead", NULL, (setter)obj_set_max_dead, NULL },
{ (char *)"filename", (getter)obj_get_filename, NULL, (char *)"The filename of this TDB file."},
{ NULL }
};
static PyObject *tdb_object_repr(PyTdbObject *self)
{
return PyString_FromFormat("Tdb('%s')", tdb_name(self->ctx));
}
static void tdb_object_dealloc(PyTdbObject *self)
{
if (!self->closed)
tdb_close(self->ctx);
PyObject_Del(self);
}
static PyObject *obj_getitem(PyTdbObject *self, PyObject *key)
{
TDB_DATA tkey, val;
if (!PyString_Check(key)) {
PyErr_SetString(PyExc_TypeError, "Expected string as key");
return NULL;
}
tkey.dptr = (unsigned char *)PyString_AsString(key);
tkey.dsize = PyString_Size(key);
val = tdb_fetch(self->ctx, tkey);
if (val.dptr == NULL) {
PyErr_SetString(PyExc_KeyError, "No such TDB entry");
return NULL;
} else {
return PyString_FromTDB_DATA(val);
}
}
static int obj_setitem(PyTdbObject *self, PyObject *key, PyObject *value)
{
TDB_DATA tkey, tval;
int ret;
if (!PyString_Check(key)) {
PyErr_SetString(PyExc_TypeError, "Expected string as key");
return -1;
}
tkey = PyString_AsTDB_DATA(key);
if (value == NULL) {
ret = tdb_delete(self->ctx, tkey);
} else {
if (!PyString_Check(value)) {
PyErr_SetString(PyExc_TypeError, "Expected string as value");
return -1;
}
tval = PyString_AsTDB_DATA(value);
ret = tdb_store(self->ctx, tkey, tval, TDB_REPLACE);
}
if (ret != 0) {
PyErr_SetTDBError(self->ctx);
return -1;
}
return ret;
}
static PyMappingMethods tdb_object_mapping = {
.mp_subscript = (binaryfunc)obj_getitem,
.mp_ass_subscript = (objobjargproc)obj_setitem,
};
PyTypeObject PyTdb = {
.tp_name = "Tdb",
.tp_basicsize = sizeof(PyTdbObject),
.tp_methods = tdb_object_methods,
.tp_getset = tdb_object_getsetters,
.tp_new = py_tdb_open,
.tp_doc = "A TDB file",
.tp_repr = (reprfunc)tdb_object_repr,
.tp_dealloc = (destructor)tdb_object_dealloc,
.tp_as_mapping = &tdb_object_mapping,
.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE|Py_TPFLAGS_HAVE_ITER,
.tp_iter = (getiterfunc)tdb_object_iter,
};
static PyMethodDef tdb_methods[] = {
{ "open", (PyCFunction)py_tdb_open, METH_VARARGS|METH_KEYWORDS, "open(name, hash_size=0, tdb_flags=TDB_DEFAULT, flags=O_RDWR, mode=0600)\n"
"Open a TDB file." },
{ NULL }
};
void inittdb(void)
{
PyObject *m;
if (PyType_Ready(&PyTdb) < 0)
return;
if (PyType_Ready(&PyTdbIterator) < 0)
return;
m = Py_InitModule3("tdb", tdb_methods, "TDB is a simple key-value database similar to GDBM that supports multiple writers.");
if (m == NULL)
return;
PyModule_AddObject(m, "REPLACE", PyInt_FromLong(TDB_REPLACE));
PyModule_AddObject(m, "INSERT", PyInt_FromLong(TDB_INSERT));
PyModule_AddObject(m, "MODIFY", PyInt_FromLong(TDB_MODIFY));
PyModule_AddObject(m, "DEFAULT", PyInt_FromLong(TDB_DEFAULT));
PyModule_AddObject(m, "CLEAR_IF_FIRST", PyInt_FromLong(TDB_CLEAR_IF_FIRST));
PyModule_AddObject(m, "INTERNAL", PyInt_FromLong(TDB_INTERNAL));
PyModule_AddObject(m, "NOLOCK", PyInt_FromLong(TDB_NOLOCK));
PyModule_AddObject(m, "NOMMAP", PyInt_FromLong(TDB_NOMMAP));
PyModule_AddObject(m, "CONVERT", PyInt_FromLong(TDB_CONVERT));
PyModule_AddObject(m, "BIGENDIAN", PyInt_FromLong(TDB_BIGENDIAN));
PyModule_AddObject(m, "__docformat__", PyString_FromString("restructuredText"));
Py_INCREF(&PyTdb);
PyModule_AddObject(m, "Tdb", (PyObject *)&PyTdb);
Py_INCREF(&PyTdbIterator);
}

6
ctdb/lib/tdb/python.mk Normal file
View File

@ -0,0 +1,6 @@
[PYTHON::pytdb]
LIBRARY_REALNAME = tdb.$(SHLIBEXT)
PUBLIC_DEPENDENCIES = LIBTDB DYNCONFIG
pytdb_OBJ_FILES = $(tdbsrcdir)/pytdb.o

View File

@ -0,0 +1,12 @@
#!/usr/bin/python
# Trivial reimplementation of tdbdump in Python
import tdb, sys
if len(sys.argv) < 2:
print "Usage: tdbdump.py <tdb-file>"
sys.exit(1)
db = tdb.Tdb(sys.argv[1])
for (k, v) in db.iteritems():
print "{\nkey(%d) = %r\ndata(%d) = %r\n}" % (len(k), k, len(v), v)

View File

@ -0,0 +1,133 @@
#!/usr/bin/python
# Some simple tests for the Python bindings for TDB
# Note that this tests the interface of the Python bindings
# It does not test tdb itself.
#
# Copyright (C) 2007-2008 Jelmer Vernooij <jelmer@samba.org>
# Published under the GNU LGPLv3 or later
import tdb
from unittest import TestCase
import os, tempfile
class OpenTdbTests(TestCase):
def test_nonexistant_read(self):
self.assertRaises(IOError, tdb.Tdb, "/some/nonexistant/file", 0, tdb.DEFAULT, os.O_RDWR)
class CloseTdbTests(TestCase):
def test_double_close(self):
self.tdb = tdb.Tdb(tempfile.mkstemp()[1], 0, tdb.DEFAULT, os.O_CREAT|os.O_RDWR)
self.assertNotEqual(None, self.tdb)
# ensure that double close does not crash python
self.tdb.close()
self.tdb.close()
class SimpleTdbTests(TestCase):
def setUp(self):
super(SimpleTdbTests, self).setUp()
self.tdb = tdb.Tdb(tempfile.mkstemp()[1], 0, tdb.DEFAULT, os.O_CREAT|os.O_RDWR)
self.assertNotEqual(None, self.tdb)
def tearDown(self):
del self.tdb
def test_repr(self):
self.assertTrue(repr(self.tdb).startswith("Tdb('"))
def test_lockall(self):
self.tdb.lock_all()
def test_max_dead(self):
self.tdb.max_dead = 20
def test_unlockall(self):
self.tdb.lock_all()
self.tdb.unlock_all()
def test_lockall_read(self):
self.tdb.read_lock_all()
self.tdb.read_unlock_all()
def test_reopen(self):
self.tdb.reopen()
def test_store(self):
self.tdb.store("bar", "bla")
self.assertEquals("bla", self.tdb.get("bar"))
def test_getitem(self):
self.tdb["bar"] = "foo"
self.tdb.reopen()
self.assertEquals("foo", self.tdb["bar"])
def test_delete(self):
self.tdb["bar"] = "foo"
del self.tdb["bar"]
self.assertRaises(KeyError, lambda: self.tdb["bar"])
def test_contains(self):
self.tdb["bla"] = "bloe"
self.assertTrue("bla" in self.tdb)
def test_keyerror(self):
self.assertRaises(KeyError, lambda: self.tdb["bla"])
def test_hash_size(self):
self.tdb.hash_size
def test_map_size(self):
self.tdb.map_size
def test_name(self):
self.tdb.filename
def test_iterator(self):
self.tdb["bla"] = "1"
self.tdb["brainslug"] = "2"
self.assertEquals(["bla", "brainslug"], list(self.tdb))
def test_transaction_cancel(self):
self.tdb["bloe"] = "2"
self.tdb.transaction_start()
self.tdb["bloe"] = "1"
self.tdb.transaction_cancel()
self.assertEquals("2", self.tdb["bloe"])
def test_transaction_commit(self):
self.tdb["bloe"] = "2"
self.tdb.transaction_start()
self.tdb["bloe"] = "1"
self.tdb.transaction_commit()
self.assertEquals("1", self.tdb["bloe"])
def test_iterator(self):
self.tdb["bloe"] = "2"
self.tdb["bla"] = "hoi"
i = iter(self.tdb)
self.assertEquals(set(["bloe", "bla"]), set([i.next(), i.next()]))
def test_iterkeys(self):
self.tdb["bloe"] = "2"
self.tdb["bla"] = "25"
i = self.tdb.iterkeys()
self.assertEquals(set(["bloe", "bla"]), set([i.next(), i.next()]))
def test_clear(self):
self.tdb["bloe"] = "2"
self.tdb["bla"] = "25"
self.assertEquals(2, len(list(self.tdb)))
self.tdb.clear()
self.assertEquals(0, len(list(self.tdb)))
def test_len(self):
self.assertEquals(0, len(list(self.tdb)))
self.tdb["entry"] = "value"
self.assertEquals(1, len(list(self.tdb)))
if __name__ == '__main__':
import unittest
unittest.TestProgram()

48
ctdb/lib/tdb/release-script.sh Executable file
View File

@ -0,0 +1,48 @@
#!/bin/bash
if [ "$1" = "" ]; then
echo "Please provide version string, eg: 1.2.0"
exit 1
fi
if [ ! -d "lib/tdb" ]; then
echo "Run this script from the samba base directory."
exit 1
fi
git clean -f -x -d lib/tdb
git clean -f -x -d lib/replace
curbranch=`git branch |grep "^*" | tr -d "* "`
version=$1
strver=`echo ${version} | tr "." "-"`
# Checkout the release tag
git branch -f tdb-release-script-${strver} tdb-${strver}
if [ ! "$?" = "0" ]; then
echo "Unable to checkout tdb-${strver} release"
exit 1
fi
git checkout tdb-release-script-${strver}
# Test configure agrees with us
confver=`grep "^AC_INIT" lib/tdb/configure.ac | tr -d "AC_INIT(tdb, " | tr -d ")"`
if [ ! "$confver" = "$version" ]; then
echo "Wrong version, requested release for ${version}, found ${confver}"
exit 1
fi
# Now build tarball
cp -a lib/tdb tdb-${version}
cp -a lib/replace tdb-${version}/libreplace
pushd tdb-${version}
./autogen.sh
popd
tar cvzf tdb-${version}.tar.gz tdb-${version}
rm -fr tdb-${version}
#Clean up
git checkout $curbranch
git branch -d tdb-release-script-${strver}

16
ctdb/lib/tdb/rules.mk Normal file
View File

@ -0,0 +1,16 @@
showflags::
@echo 'tdb will be compiled with flags:'
@echo ' CFLAGS = $(CFLAGS)'
@echo ' CPPFLAGS = $(CPPFLAGS)'
@echo ' LDFLAGS = $(LDFLAGS)'
@echo ' LIBS = $(LIBS)'
.SUFFIXES: .c .o
.c.o:
@echo Compiling $*.c
@mkdir -p `dirname $@`
@$(CC) $(PICFLAG) $(CFLAGS) $(ABI_CHECK) -c $< -o $@
distclean::
rm -f *~ */*~

View File

@ -0,0 +1,91 @@
#!/bin/sh
#
# abi_checks.sh - check for possible abi changes
#
# Copyright (C) 2009 Michael Adam <obnox@samba.org>
#
# 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/>.
#
#
# USAGE: abi_checks.sh LIBNAME header1 [header2 ...]
#
# This script creates symbol and signature lists from the provided header
# files with the aid of the mksyms.sh and mksigs.pl scripts (saved as
# $LIBNAME.exports.check and $LIBNAME.sigatures.check). It then compares
# the resulting files with the files $LIBNAME.exports and $LIBNME.signatures
# which it expects to find in the current directory.
#
LANG=C; export LANG
LC_ALL=C; export LC_ALL
LC_COLLATE=C; export LC_COLLATE
script=$0
dir_name=$(dirname ${script})
if test x"$1" = "x" ; then
echo "USAGE: ${script} libname header [header ...]"
exit 1
fi
libname="$1"
shift
if test x"$1" = "x" ; then
echo "USAGE: ${script} libname header [header ...]"
exit 1
fi
headers="$*"
exports_file=${libname}.exports
exports_file_check=${exports_file}.check
signatures_file=${libname}.signatures
signatures_file_check=${signatures_file}.check
${dir_name}/mksyms.sh awk ${exports_file_check} ${headers} 2>&1 > /dev/null
cat ${headers} | ${dir_name}/mksigs.pl > ${signatures_file_check} 2> /dev/null
normalize_exports_file() {
filename=$1
cat ${filename} \
| sed -e 's/^[ \t]*//g' \
| sed -e 's/^$//g' \
| sed -e 's/^#.*$//g' \
| sort | uniq > ${filename}.sort
}
normalize_exports_file ${exports_file}
normalize_exports_file ${exports_file_check}
normalize_exports_file ${signatures_file}
normalize_exports_file ${signatures_file_check}
diff -u ${exports_file}.sort ${exports_file_check}.sort
if test "x$?" != "x0" ; then
echo "WARNING: possible ABI change detected in exports!"
else
echo "exports check: OK"
fi
diff -u ${signatures_file}.sort ${signatures_file_check}.sort
if test "x$?" != "x0" ; then
echo "WARNING: possible ABI change detected in signatures!"
else
echo "signatures check: OK"
fi

View File

@ -0,0 +1,35 @@
#!/bin/bash
make clean
mkdir -p abi/common
mkdir -p abi/tools
ABI_CHECKS="-aux-info abi/\$@.X"
make ABI_CHECK="$ABI_CHECKS" CC="/usr/bin/gcc"
for i in abi/*/*.X; do cat $i | grep 'tdb\.h'; done | sort | uniq | awk -F "extern " '{ print $2 }' | sort > abi/signatures
grep '^extern' include/tdb.h | grep -v '"C"' | sort | uniq | awk -F "extern " '{ print $2 }' >> abi/signatures
cat > abi/exports << EOF
{
global:
EOF
#Functions
cat abi/signatures | grep "(" | awk -F '(' '{ print $1 }' | awk -F ' ' '{ print " "$NF";" }' | tr -d '*' | sort >> abi/exports
#global vars
cat abi/signatures | grep -v "(" | awk -F ';' '{print $1 }' | awk -F ' ' '{ print " "$NF";" }' | tr -d '*' | sort >> abi/exports
cat >> abi/exports << EOF
local: *;
};
EOF
diff -u tdb.signatures abi/signatures
if [ "$?" != "0" ]; then
echo "WARNING: Possible ABI Change!!"
fi
diff -u tdb.exports abi/exports
if [ "$?" != "0" ]; then
echo "WARNING: Export file may be outdated!!"
fi

183
ctdb/lib/tdb/script/mksigs.pl Executable file
View File

@ -0,0 +1,183 @@
#!/usr/bin/perl
# mksigs.pl - extract signatures from C headers
#
# 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/>.
# USAGE: cat $header_files | mksigs.pl > $signature_file
#
# The header files to parse are read from stdin.
# The output is in a form as produced by gcc with the -aux-info switch
# and printed to stdout.
use strict;
use warnings;
my $in_comment = 0;
my $extern_C_block = 0;
while (my $LINE = <>) {
# find end of started multi-line-comment
if ($in_comment) {
if ($LINE =~ /^.*?\*\/(.*)$/) {
$LINE = $1;
$in_comment = 0;
} else {
# whole line within comment
next;
}
}
# strip C++-style comments
$LINE =~ s/^(.*?)\/\/.*$/$1/;
# strip in-line-comments:
while ($LINE =~ /\/\*.*?\*\//) {
$LINE =~ s/\/\*.*?\*\///;
}
# find starts of multi-line-comments
if ($LINE =~ /^(.*)\/\*/) {
$in_comment = 1;
$LINE = $1;
}
# skip empty lines
next if $LINE =~ /^\s*$/;
# remove leading spaces
$LINE =~ s/^\s*(.*)$/$1/;
# concatenate lines split with "\" (usually macro defines)
while ($LINE =~ /^(.*?)\s+\\$/) {
my $LINE2 = <>;
$LINE = $1;
$LINE2 =~ s/^\s*(.*)$/$1/;
$LINE .= " " . $LINE2;
}
# remove all preprocessor directives
next if ($LINE =~ /^#/);
if ($LINE =~ /^extern\s+"C"\s+\{/) {
$extern_C_block = 1;
next;
}
if (($LINE =~ /^[^\{]*\}/) and $extern_C_block) {
$extern_C_block = 0;
next;
}
$LINE =~ s/^extern\s//;
# concatenate braces stretched over multiple lines
# (from structs or enums)
my $REST = $LINE;
my $braces = 0;
while (($REST =~ /[\{\}]/) or ($braces)) {
while ($REST =~ /[\{\}]/) {
# collect opening
while ($REST =~ /^[^\{\}]*\{(.*)$/) {
$braces++;
$REST = $1;
}
# collect closing
while ($REST =~ /^[^\{\}]*\}(.*)$/) {
$braces--;
$REST = $1;
}
}
# concatenate if not balanced
if ($braces) {
if (my $LINE2 = <>) {
$LINE2 =~ s/^\s*(.*)$/$1/;
chomp($LINE);
$LINE .= " " . $LINE2;
chomp $REST;
$REST .= " " . $LINE2;
} else {
print "ERROR: unbalanced braces ($braces)\n";
last;
}
}
}
# concetenate function prototypes that stretch over multiple lines
$REST = $LINE;
my $parenthesis = 0;
while (($REST =~ /[\(\)]/) or ($parenthesis)) {
while ($REST =~ /[\(\)]/) {
# collect opening
while ($REST =~ /^[^\(\)]*\((.*)$/) {
$parenthesis++;
$REST = $1;
}
# collect closing
while ($REST =~ /^[^\(\)]*\)(.*)$/) {
$parenthesis--;
$REST = $1;
}
}
# concatenate if not balanced
if ($parenthesis) {
if (my $LINE2 = <>) {
$LINE2 =~ s/^\s*(.*)$/$1/;
chomp($LINE);
$LINE .= " " . $LINE2;
chomp($REST);
$REST .= " " . $LINE2;
} else {
print "ERROR: unbalanced parantheses ($parenthesis)\n";
last;
}
}
}
next if ($LINE =~ /^typedef\s/);
next if ($LINE =~ /^enum\s+[^\{\(]+\s+\{/);
next if ($LINE =~ /^struct\s+[^\{\(]+\s+\{.*\}\s*;/);
next if ($LINE =~ /^struct\s+[a-zA-Z0-9_]+\s*;/);
# remove trailing spaces
$LINE =~ s/(.*?)\s*$/$1/;
$LINE =~ s/^(.*\))\s+PRINTF_ATTRIBUTE\([^\)]*\)(\s*[;,])/$1$2/;
$LINE =~ s/^(.*\))\s*[a-zA-Z0-9_]+\s*;$/$1;/;
# remove parameter names - slightly too coarse probably
$LINE =~ s/([\s\(]\*?)[_0-9a-zA-Z]+\s*([,\)])/$1$2/g;
# remedy (void) from last line
$LINE =~ s/\(\)/(void)/g;
# normalize spaces
$LINE =~ s/\s*\)\s*/)/g;
$LINE =~ s/\s*\(\s*/ (/g;
$LINE =~ s/\s*,\s*/, /g;
# normalize unsigned
$LINE =~ s/([\s,\(])unsigned([,\)])/$1unsigned int$2/g;
# normalize bool
$LINE =~ s/(\b)bool(\b)/_Bool/g;
print $LINE . "\n";
}

View File

@ -0,0 +1,76 @@
#
# mksyms.awk
#
# Extract symbols to export from C-header files.
# output in version-script format for linking shared libraries.
#
# Copyright (C) 2008 Micheal Adam <obnox@samba.org>
#
BEGIN {
inheader=0;
current_file="";
print "#"
print "# This file is automatically generated with \"make symbols\". DO NOT EDIT "
print "#"
print "{"
print "\tglobal:"
}
END {
print""
print "\tlocal: *;"
print "};"
}
{
if (FILENAME!=current_file) {
print "\t\t# The following definitions come from",FILENAME
current_file=FILENAME
}
if (inheader) {
if (match($0,"[)][^()]*[;][ \t]*$")) {
inheader = 0;
}
next;
}
}
/^static/ || /^[ \t]*typedef/ || !/^[a-zA-Z\_]/ {
next;
}
/^extern[ \t]+[^()]+[;][ \t]*$/ {
gsub(/[^ \t]+[ \t]+/, "");
sub(/[;][ \t]*$/, "");
printf "\t\t%s;\n", $0;
next;
}
# look for function headers:
{
gotstart = 0;
if ($0 ~ /^[A-Za-z_][A-Za-z0-9_]+/) {
gotstart = 1;
}
if(!gotstart) {
next;
}
}
/[_A-Za-z0-9]+[ \t]*[(].*[)][^()]*;[ \t]*$/ {
sub(/[(].*$/, "");
gsub(/[^ \t]+[ \t]+/, "");
gsub(/^[*]+/, "");
printf "\t\t%s;\n",$0;
next;
}
/[_A-Za-z0-9]+[ \t]*[(]/ {
inheader=1;
sub(/[(].*$/, "");
gsub(/[^ \t]+[ \t]+/, "");
gsub(/^[*]/, "");
printf "\t\t%s;\n",$0;
next;
}

45
ctdb/lib/tdb/script/mksyms.sh Executable file
View File

@ -0,0 +1,45 @@
#! /bin/sh
#
# mksyms.sh
#
# Extract symbols to export from C-header files.
# output in version-script format for linking shared libraries.
#
# This is the shell wrapper for the mksyms.awk core script.
#
# Copyright (C) 2008 Micheal Adam <obnox@samba.org>
#
LANG=C; export LANG
LC_ALL=C; export LC_ALL
LC_COLLATE=C; export LC_COLLATE
if [ $# -lt 2 ]
then
echo "Usage: $0 awk output_file header_files"
exit 1
fi
awk="$1"
shift
symsfile="$1"
shift
symsfile_tmp="$symsfile.$$.tmp~"
proto_src="`echo $@ | tr ' ' '\n' | sort | uniq `"
echo creating $symsfile
mkdir -p `dirname $symsfile`
${awk} -f `dirname $0`/mksyms.awk $proto_src > $symsfile_tmp
if cmp -s $symsfile $symsfile_tmp 2>/dev/null
then
echo "$symsfile unchanged"
rm $symsfile_tmp
else
mv $symsfile_tmp $symsfile
fi

View File

@ -1,116 +0,0 @@
"""Provide a more Pythonic and object-oriented interface to tdb."""
#
# Swig interface to Samba
#
# Copyright (C) Tim Potter 2006
#
# 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, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#
import os
from tdb import *
# Open flags
DEFAULT = TDB_DEFAULT
CLEAR_IF_FIRST = TDB_CLEAR_IF_FIRST
INTERNAL = TDB_INTERNAL
NOLOCK = TDB_NOLOCK
NOMMAP = TDB_NOMMAP
# Class representing a TDB file
class Tdb:
# Create and destroy Tdb objects
def __init__(self, name, hash_size = 0, flags = TDB_DEFAULT,
open_flags = os.O_RDWR | os.O_CREAT, mode = 0600):
self.tdb = tdb_open(name, hash_size, flags, open_flags, mode)
if self.tdb is None:
raise IOError, tdb_errorstr(self.tdb)
def __del__(self):
self.close()
def close(self):
if hasattr(self, 'tdb') and self.tdb is not None:
if tdb_close(self.tdb) == -1:
raise IOError, tdb_errorstr(self.tdb)
self.tdb = None
# Random access to keys, values
def __getitem__(self, key):
result = tdb_fetch(self.tdb, key)
if result is None:
raise KeyError, '%s: %s' % (key, tdb_errorstr(self.tdb))
return result
def __setitem__(self, key, item):
if tdb_store(self.tdb, key, item) == -1:
raise IOError, tdb_errorstr(self.tdb)
def __delitem__(self, key):
if not tdb_exists(self.tdb, key):
raise KeyError, '%s: %s' % (key, tdb_errorstr(self.tdb))
tdb_delete(self.tdb, key)
def has_key(self, key):
return tdb_exists(self.tdb, key)
# Tdb iterator
class TdbIterator:
def __init__(self, tdb):
self.tdb = tdb
self.key = None
def __iter__(self):
return self
def next(self):
if self.key is None:
self.key = tdb_firstkey(self.tdb)
if self.key is None:
raise StopIteration
return self.key
else:
self.key = tdb_nextkey(self.tdb, self.key)
if self.key is None:
raise StopIteration
return self.key
def __iter__(self):
return Tdb.TdbIterator(self.tdb)
# Implement other dict functions using TdbIterator
def keys(self):
return [k for k in iter(self)]
def values(self):
return [self[k] for k in iter(self)]
def items(self):
return [(k, self[k]) for k in iter(self)]
def __len__(self):
return len(self.keys())
def clear(self):
for k in iter(self):
del(self[k])

View File

@ -1,167 +0,0 @@
/*
Unix SMB/CIFS implementation.
Swig interface to tdb.
Copyright (C) 2004,2005 Tim Potter <tpot@samba.org>
** NOTE! The following LGPL license applies to the tdb
** library. This does NOT imply that all of Samba is released
** under the LGPL
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 3 of the License, or (at your option) any later version.
This library 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
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, see <http://www.gnu.org/licenses/>.
*/
%module tdb
%{
/* This symbol is used in both includes.h and Python.h which causes an
annoying compiler warning. */
#ifdef HAVE_FSTAT
#undef HAVE_FSTAT
#endif
#if (__GNUC__ >= 3)
/** Use gcc attribute to check printf fns. a1 is the 1-based index of
* the parameter containing the format, and a2 the index of the first
* argument. Note that some gcc 2.x versions don't handle this
* properly **/
#define PRINTF_ATTRIBUTE(a1, a2) __attribute__ ((format (__printf__, a1, a2)))
#else
#define PRINTF_ATTRIBUTE(a1, a2)
#endif
/* Include tdb headers */
#include "lib/tdb/include/tdb.h"
%}
/* The tdb functions will crash if a NULL tdb context is passed */
%include exception.i
%typemap(check) TDB_CONTEXT* {
if ($1 == NULL)
SWIG_exception(SWIG_ValueError,
"tdb context must be non-NULL");
}
/* In and out typemaps for the TDB_DATA structure. This is converted to
and from the Python string type which can contain arbitrary binary
data.. */
%typemap(in) TDB_DATA {
if (!PyString_Check($input)) {
PyErr_SetString(PyExc_TypeError, "string arg expected");
return NULL;
}
$1.dsize = PyString_Size($input);
$1.dptr = PyString_AsString($input);
}
%typemap(out) TDB_DATA {
if ($1.dptr == NULL && $1.dsize == 0) {
$result = Py_None;
} else {
$result = PyString_FromStringAndSize($1.dptr, $1.dsize);
free($1.dptr);
}
}
/* Treat a mode_t as an unsigned integer */
typedef int mode_t;
/* flags to tdb_store() */
#define TDB_REPLACE 1
#define TDB_INSERT 2
#define TDB_MODIFY 3
/* flags for tdb_open() */
#define TDB_DEFAULT 0 /* just a readability place holder */
#define TDB_CLEAR_IF_FIRST 1
#define TDB_INTERNAL 2 /* don't store on disk */
#define TDB_NOLOCK 4 /* don't do any locking */
#define TDB_NOMMAP 8 /* don't use mmap */
#define TDB_CONVERT 16 /* convert endian (internal use) */
#define TDB_BIGENDIAN 32 /* header is big-endian (internal use) */
/* Throw an IOError exception if tdb_open() or tdb_open_ex() returns NULL */
%exception {
$action
if (result == NULL) {
PyErr_SetFromErrno(PyExc_IOError);
SWIG_fail;
}
}
TDB_CONTEXT *tdb_open(const char *name, int hash_size, int tdb_flags,
int open_flags, mode_t mode);
TDB_CONTEXT *tdb_open_ex(const char *name, int hash_size, int tdb_flags,
int open_flags, mode_t mode,
tdb_log_func log_fn,
tdb_hash_func hash_fn);
%exception;
int tdb_reopen(TDB_CONTEXT *tdb);
int tdb_reopen_all(int parent_longlived);
void tdb_logging_function(TDB_CONTEXT *tdb, tdb_log_func);
enum TDB_ERROR tdb_error(TDB_CONTEXT *tdb);
const char *tdb_errorstr(TDB_CONTEXT *tdb);
TDB_DATA tdb_fetch(TDB_CONTEXT *tdb, TDB_DATA key);
int tdb_delete(TDB_CONTEXT *tdb, TDB_DATA key);
int tdb_store(TDB_CONTEXT *tdb, TDB_DATA key, TDB_DATA dbuf, int flag = TDB_REPLACE);
int tdb_append(TDB_CONTEXT *tdb, TDB_DATA key, TDB_DATA new_dbuf);
int tdb_close(TDB_CONTEXT *tdb);
TDB_DATA tdb_firstkey(TDB_CONTEXT *tdb);
TDB_DATA tdb_nextkey(TDB_CONTEXT *tdb, TDB_DATA key);
int tdb_traverse(TDB_CONTEXT *tdb, tdb_traverse_func fn, void *state);
int tdb_exists(TDB_CONTEXT *tdb, TDB_DATA key);
int tdb_lockall(TDB_CONTEXT *tdb);
void tdb_unlockall(TDB_CONTEXT *tdb);
/* Low level locking functions: use with care */
int tdb_chainlock(TDB_CONTEXT *tdb, TDB_DATA key);
int tdb_chainunlock(TDB_CONTEXT *tdb, TDB_DATA key);
/* Debug functions. Not used in production. */
void tdb_dump_all(TDB_CONTEXT *tdb);
int tdb_printfreelist(TDB_CONTEXT *tdb);

View File

@ -1,95 +0,0 @@
# This file was created automatically by SWIG 1.3.28.
# Don't modify this file, modify the SWIG interface instead.
# This file is compatible with both classic and new-style classes.
import _tdb
import new
new_instancemethod = new.instancemethod
def _swig_setattr_nondynamic(self,class_type,name,value,static=1):
if (name == "thisown"): return self.this.own(value)
if (name == "this"):
if type(value).__name__ == 'PySwigObject':
self.__dict__[name] = value
return
method = class_type.__swig_setmethods__.get(name,None)
if method: return method(self,value)
if (not static) or hasattr(self,name):
self.__dict__[name] = value
else:
raise AttributeError("You cannot add attributes to %s" % self)
def _swig_setattr(self,class_type,name,value):
return _swig_setattr_nondynamic(self,class_type,name,value,0)
def _swig_getattr(self,class_type,name):
if (name == "thisown"): return self.this.own()
method = class_type.__swig_getmethods__.get(name,None)
if method: return method(self)
raise AttributeError,name
import types
try:
_object = types.ObjectType
_newclass = 1
except AttributeError:
class _object : pass
_newclass = 0
del types
TDB_REPLACE = _tdb.TDB_REPLACE
TDB_INSERT = _tdb.TDB_INSERT
TDB_MODIFY = _tdb.TDB_MODIFY
TDB_DEFAULT = _tdb.TDB_DEFAULT
TDB_CLEAR_IF_FIRST = _tdb.TDB_CLEAR_IF_FIRST
TDB_INTERNAL = _tdb.TDB_INTERNAL
TDB_NOLOCK = _tdb.TDB_NOLOCK
TDB_NOMMAP = _tdb.TDB_NOMMAP
TDB_CONVERT = _tdb.TDB_CONVERT
TDB_BIGENDIAN = _tdb.TDB_BIGENDIAN
open = _tdb.open
open_ex = _tdb.open_ex
reopen = _tdb.reopen
reopen_all = _tdb.reopen_all
logging_function = _tdb.logging_function
error = _tdb.error
errorstr = _tdb.errorstr
fetch = _tdb.fetch
delete = _tdb.delete
store = _tdb.store
append = _tdb.append
close = _tdb.close
firstkey = _tdb.firstkey
nextkey = _tdb.nextkey
traverse = _tdb.traverse
exists = _tdb.exists
lockall = _tdb.lockall
unlockall = _tdb.unlockall
chainlock = _tdb.chainlock
chainunlock = _tdb.chainunlock
dump_all = _tdb.dump_all
printfreelist = _tdb.printfreelist

65
ctdb/lib/tdb/tdb.exports Normal file
View File

@ -0,0 +1,65 @@
{
global:
tdb_add_flags;
tdb_append;
tdb_chainlock;
tdb_chainlock_mark;
tdb_chainlock_nonblock;
tdb_chainlock_read;
tdb_chainlock_unmark;
tdb_chainunlock;
tdb_chainunlock_read;
tdb_check;
tdb_close;
tdb_delete;
tdb_dump_all;
tdb_enable_seqnum;
tdb_error;
tdb_errorstr;
tdb_exists;
tdb_fd;
tdb_fetch;
tdb_firstkey;
tdb_freelist_size;
tdb_get_flags;
tdb_get_logging_private;
tdb_get_seqnum;
tdb_hash_size;
tdb_increment_seqnum_nonblock;
tdb_lockall;
tdb_lockall_mark;
tdb_lockall_nonblock;
tdb_lockall_read;
tdb_lockall_read_nonblock;
tdb_lockall_unmark;
tdb_log_fn;
tdb_map_size;
tdb_name;
tdb_nextkey;
tdb_open;
tdb_open_ex;
tdb_parse_record;
tdb_printfreelist;
tdb_remove_flags;
tdb_reopen;
tdb_reopen_all;
tdb_repack;
tdb_setalarm_sigptr;
tdb_set_logging_function;
tdb_set_max_dead;
tdb_store;
tdb_transaction_cancel;
tdb_transaction_commit;
tdb_transaction_prepare_commit;
tdb_transaction_recover;
tdb_transaction_start;
tdb_traverse;
tdb_traverse_read;
tdb_unlockall;
tdb_unlockall_read;
tdb_validate_freelist;
tdb_wipe_all;
tdb_null;
local: *;
};

89
ctdb/lib/tdb/tdb.mk Normal file
View File

@ -0,0 +1,89 @@
dirs::
@mkdir -p bin common tools
PROGS = bin/tdbtool$(EXEEXT) bin/tdbdump$(EXEEXT) bin/tdbbackup$(EXEEXT)
PROGS_NOINSTALL = bin/tdbtest$(EXEEXT) bin/tdbtorture$(EXEEXT)
ALL_PROGS = $(PROGS) $(PROGS_NOINSTALL)
TDB_SONAME = libtdb.$(SHLIBEXT).1
TDB_SOLIB = libtdb.$(SHLIBEXT).$(PACKAGE_VERSION)
TDB_STLIB = libtdb.a
TDB_LIB = $(TDB_STLIB)
bin/tdbtest$(EXEEXT): tools/tdbtest.o $(TDB_LIB)
$(CC) $(CFLAGS) $(LDFLAGS) -o bin/tdbtest tools/tdbtest.o -L. -ltdb -lgdbm
bin/tdbtool$(EXEEXT): tools/tdbtool.o $(TDB_LIB)
$(CC) $(CFLAGS) $(LDFLAGS) -o bin/tdbtool tools/tdbtool.o -L. -ltdb
bin/tdbtorture$(EXEEXT): tools/tdbtorture.o $(TDB_LIB)
$(CC) $(CFLAGS) $(LDFLAGS) -o bin/tdbtorture tools/tdbtorture.o -L. -ltdb
bin/tdbdump$(EXEEXT): tools/tdbdump.o $(TDB_LIB)
$(CC) $(CFLAGS) $(LDFLAGS) -o bin/tdbdump tools/tdbdump.o -L. -ltdb
bin/tdbbackup$(EXEEXT): tools/tdbbackup.o $(TDB_LIB)
$(CC) $(CFLAGS) $(LDFLAGS) -o bin/tdbbackup tools/tdbbackup.o -L. -ltdb
test:: abi_checks
test:: bin/tdbtorture$(EXEEXT) $(TDB_SONAME)
$(LIB_PATH_VAR)=. bin/tdbtorture$(EXEEXT)
abi_checks::
@echo ABI checks:
@./script/abi_checks.sh tdb include/tdb.h
clean::
rm -f test.db test.tdb torture.tdb test.gdbm
rm -f $(TDB_SONAME) $(TDB_SOLIB) $(TDB_STLIB) libtdb.$(SHLIBEXT)
rm -f $(ALL_PROGS) tdb.pc
rm -f tdb.exports.sort tdb.exports.check tdb.exports.check.sort
rm -f tdb.signatures.sort tdb.signatures.check tdb.signatures.check.sort
build-python:: tdb.$(SHLIBEXT)
pytdb.o: $(tdbdir)/pytdb.c
$(CC) $(PICFLAG) -c $(tdbdir)/pytdb.c $(CFLAGS) `$(PYTHON_CONFIG) --cflags`
tdb.$(SHLIBEXT): libtdb.$(SHLIBEXT) pytdb.o
$(SHLD) $(SHLD_FLAGS) -o $@ pytdb.o -L. -ltdb `$(PYTHON_CONFIG) --ldflags`
install:: installdirs installbin installheaders installlibs \
$(PYTHON_INSTALL_TARGET)
install-python:: build-python
mkdir -p $(DESTDIR)`$(PYTHON) -c "import distutils.sysconfig; print distutils.sysconfig.get_python_lib(1, prefix='$(prefix)')"`
cp tdb.$(SHLIBEXT) $(DESTDIR)`$(PYTHON) -c "import distutils.sysconfig; print distutils.sysconfig.get_python_lib(1, prefix='$(prefix)')"`
check-python:: build-python $(TDB_SONAME)
$(LIB_PATH_VAR)=. PYTHONPATH=".:$(tdbdir)" $(PYTHON) $(tdbdir)/python/tests/simple.py
clean::
rm -f tdb.$(SHLIBEXT)
installdirs::
mkdir -p $(DESTDIR)$(bindir)
mkdir -p $(DESTDIR)$(includedir)
mkdir -p $(DESTDIR)$(libdir)
mkdir -p $(DESTDIR)$(libdir)/pkgconfig
installbin:: all installdirs
cp $(PROGS) $(DESTDIR)$(bindir)
installheaders:: installdirs
cp $(srcdir)/include/tdb.h $(DESTDIR)$(includedir)
installlibs:: all installdirs
cp tdb.pc $(DESTDIR)$(libdir)/pkgconfig
cp $(TDB_STLIB) $(TDB_SOLIB) $(DESTDIR)$(libdir)
$(TDB_STLIB): $(TDB_OBJ)
ar -rv $(TDB_STLIB) $(TDB_OBJ)
libtdb.$(SHLIBEXT): $(TDB_SOLIB)
ln -fs $< $@
$(TDB_SONAME): $(TDB_SOLIB)
ln -fs $< $@

View File

@ -1,12 +0,0 @@
prefix=/home/tridge/samba/samba4/prefix
exec_prefix=/home/tridge/samba/samba4/prefix
libdir=${exec_prefix}/lib
includedir=${prefix}/include
Name: tdb
Description: Trivial Database Library
Requires.private:
Version: 0.0.1
Libs: -L${libdir} -ltdb
Libs.private: -lreplace
Cflags: -I${includedir} -DHAVE_IMMEDIATE_STRUCTURES=1

View File

@ -8,3 +8,4 @@ Description: A trivial database
Version: @PACKAGE_VERSION@
Libs: -L${libdir} -ltdb
Cflags: -I${includedir}
URL: http://tdb.samba.org/

View File

@ -0,0 +1,60 @@
const char *tdb_errorstr (struct tdb_context *);
const char *tdb_name (struct tdb_context *);
enum TDB_ERROR tdb_error (struct tdb_context *);
int tdb_append (struct tdb_context *, TDB_DATA, TDB_DATA);
int tdb_chainlock_mark (struct tdb_context *, TDB_DATA);
int tdb_chainlock_nonblock (struct tdb_context *, TDB_DATA);
int tdb_chainlock_read (struct tdb_context *, TDB_DATA);
int tdb_chainlock (struct tdb_context *, TDB_DATA);
int tdb_chainlock_unmark (struct tdb_context *, TDB_DATA);
int tdb_chainunlock_read (struct tdb_context *, TDB_DATA);
int tdb_chainunlock (struct tdb_context *, TDB_DATA);
int tdb_close (struct tdb_context *);
int tdb_delete (struct tdb_context *, TDB_DATA);
int tdb_exists (struct tdb_context *, TDB_DATA);
int tdb_fd (struct tdb_context *);
int tdb_freelist_size (struct tdb_context *);
int tdb_get_flags (struct tdb_context *);
int tdb_get_seqnum (struct tdb_context *);
int tdb_hash_size (struct tdb_context *);
int tdb_lockall_mark (struct tdb_context *);
int tdb_lockall_nonblock (struct tdb_context *);
int tdb_lockall_read_nonblock (struct tdb_context *);
int tdb_lockall_read (struct tdb_context *);
int tdb_lockall (struct tdb_context *);
int tdb_lockall_unmark (struct tdb_context *);
int tdb_parse_record (struct tdb_context *, TDB_DATA, int (*) (TDB_DATA, TDB_DATA, void *), void *);
int tdb_printfreelist (struct tdb_context *);
int tdb_reopen_all (int);
int tdb_reopen (struct tdb_context *);
int tdb_repack (struct tdb_context *);
int tdb_store (struct tdb_context *, TDB_DATA, TDB_DATA, int);
int tdb_transaction_cancel (struct tdb_context *);
int tdb_transaction_commit (struct tdb_context *);
int tdb_transaction_prepare_commit (struct tdb_context *);
int tdb_transaction_recover (struct tdb_context *);
int tdb_transaction_start (struct tdb_context *);
int tdb_traverse_read (struct tdb_context *, tdb_traverse_func, void *);
int tdb_traverse (struct tdb_context *, tdb_traverse_func, void *);
int tdb_unlockall_read (struct tdb_context *);
int tdb_unlockall (struct tdb_context *);
int tdb_validate_freelist (struct tdb_context *, int *);
int tdb_wipe_all (struct tdb_context *);
size_t tdb_map_size (struct tdb_context *);
struct tdb_context *tdb_open (const char *, int, int, int, mode_t);
struct tdb_context *tdb_open_ex (const char *, int, int, int, mode_t, const struct tdb_logging_context *, tdb_hash_func);
TDB_DATA tdb_fetch (struct tdb_context *, TDB_DATA);
TDB_DATA tdb_firstkey (struct tdb_context *);
TDB_DATA tdb_nextkey (struct tdb_context *, TDB_DATA);
tdb_log_func tdb_log_fn (struct tdb_context *);
void tdb_add_flags (struct tdb_context *, unsigned int);
void tdb_dump_all (struct tdb_context *);
void tdb_enable_seqnum (struct tdb_context *);
void *tdb_get_logging_private (struct tdb_context *);
void tdb_increment_seqnum_nonblock (struct tdb_context *);
void tdb_remove_flags (struct tdb_context *, unsigned int);
void tdb_setalarm_sigptr (struct tdb_context *, volatile sig_atomic_t *);
void tdb_set_logging_function (struct tdb_context *, const struct tdb_logging_context *);
void tdb_set_max_dead (struct tdb_context *, int);
int tdb_check (struct tdb_context *, int (*)(TDB_DATA, TDB_DATA, void *), void *);
TDB_DATA tdb_null;

View File

@ -53,6 +53,21 @@
static int failed;
static struct tdb_logging_context log_ctx;
#ifdef PRINTF_ATTRIBUTE
static void tdb_log(struct tdb_context *tdb, enum tdb_debug_level level, const char *format, ...) PRINTF_ATTRIBUTE(3,4);
#endif
static void tdb_log(struct tdb_context *tdb, enum tdb_debug_level level, const char *format, ...)
{
va_list ap;
va_start(ap, format);
vfprintf(stdout, format, ap);
va_end(ap);
fflush(stdout);
}
static char *add_suffix(const char *name, const char *suffix)
{
char *ret;
@ -107,7 +122,8 @@ static int backup_tdb(const char *old_name, const char *new_name, int hash_size)
}
/* open the old tdb */
tdb = tdb_open(old_name, 0, 0, O_RDWR, 0);
tdb = tdb_open_ex(old_name, 0, 0,
O_RDWR, 0, &log_ctx, NULL);
if (!tdb) {
printf("Failed to open %s\n", old_name);
free(tmp_name);
@ -116,19 +132,28 @@ static int backup_tdb(const char *old_name, const char *new_name, int hash_size)
/* create the new tdb */
unlink(tmp_name);
tdb_new = tdb_open(tmp_name,
hash_size ? hash_size : tdb_hash_size(tdb),
TDB_DEFAULT, O_RDWR|O_CREAT|O_EXCL,
st.st_mode & 0777);
tdb_new = tdb_open_ex(tmp_name,
hash_size ? hash_size : tdb_hash_size(tdb),
TDB_DEFAULT,
O_RDWR|O_CREAT|O_EXCL, st.st_mode & 0777,
&log_ctx, NULL);
if (!tdb_new) {
perror(tmp_name);
free(tmp_name);
return 1;
}
/* lock the old tdb */
if (tdb_lockall(tdb) != 0) {
fprintf(stderr,"Failed to lock %s\n", old_name);
if (tdb_transaction_start(tdb) != 0) {
printf("Failed to start transaction on old tdb\n");
tdb_close(tdb);
tdb_close(tdb_new);
unlink(tmp_name);
free(tmp_name);
return 1;
}
if (tdb_transaction_start(tdb_new) != 0) {
printf("Failed to start transaction on new tdb\n");
tdb_close(tdb);
tdb_close(tdb_new);
unlink(tmp_name);
@ -152,9 +177,21 @@ static int backup_tdb(const char *old_name, const char *new_name, int hash_size)
/* close the old tdb */
tdb_close(tdb);
if (tdb_transaction_commit(tdb_new) != 0) {
fprintf(stderr, "Failed to commit new tdb\n");
tdb_close(tdb_new);
unlink(tmp_name);
free(tmp_name);
return 1;
}
/* close the new tdb and re-open read-only */
tdb_close(tdb_new);
tdb_new = tdb_open(tmp_name, 0, TDB_DEFAULT, O_RDONLY, 0);
tdb_new = tdb_open_ex(tmp_name,
0,
TDB_DEFAULT,
O_RDONLY, 0,
&log_ctx, NULL);
if (!tdb_new) {
fprintf(stderr,"failed to reopen %s\n", tmp_name);
unlink(tmp_name);
@ -173,9 +210,6 @@ static int backup_tdb(const char *old_name, const char *new_name, int hash_size)
return 1;
}
/* make sure the new tdb has reached stable storage */
fsync(tdb_fd(tdb_new));
/* close the new tdb and rename it to .bak */
tdb_close(tdb_new);
if (rename(tmp_name, new_name) != 0) {
@ -198,7 +232,8 @@ static int verify_tdb(const char *fname, const char *bak_name)
int count = -1;
/* open the tdb */
tdb = tdb_open(fname, 0, 0, O_RDONLY, 0);
tdb = tdb_open_ex(fname, 0, 0,
O_RDONLY, 0, &log_ctx, NULL);
/* traverse the tdb, then close it */
if (tdb) {
@ -251,6 +286,8 @@ static void usage(void)
int hashsize = 0;
const char *suffix = ".bak";
log_ctx.log_fn = tdb_log;
while ((c = getopt(argc, argv, "vhs:n:")) != -1) {
switch (c) {
case 'h':

View File

@ -65,8 +65,8 @@ static int dump_tdb(const char *fname, const char *keyname)
if (!keyname) {
tdb_traverse(tdb, traverse_fn, NULL);
} else {
key.dptr = discard_const_p(uint8_t,keyname);
key.dsize = strlen( keyname);
key.dptr = discard_const_p(uint8_t, keyname);
key.dsize = strlen(keyname);
value = tdb_fetch(tdb, key);
if (!value.dptr) {
return 1;

View File

@ -40,6 +40,9 @@ static int disable_mmap;
enum commands {
CMD_CREATE_TDB,
CMD_OPEN_TDB,
CMD_TRANSACTION_START,
CMD_TRANSACTION_COMMIT,
CMD_TRANSACTION_CANCEL,
CMD_ERASE,
CMD_DUMP,
CMD_INSERT,
@ -57,6 +60,7 @@ enum commands {
CMD_FIRST,
CMD_NEXT,
CMD_SYSTEM,
CMD_CHECK,
CMD_QUIT,
CMD_HELP
};
@ -69,6 +73,9 @@ typedef struct {
COMMAND_TABLE cmd_table[] = {
{"create", CMD_CREATE_TDB},
{"open", CMD_OPEN_TDB},
{"transaction_start", CMD_TRANSACTION_START},
{"transaction_commit", CMD_TRANSACTION_COMMIT},
{"transaction_cancel", CMD_TRANSACTION_CANCEL},
{"erase", CMD_ERASE},
{"dump", CMD_DUMP},
{"insert", CMD_INSERT},
@ -87,6 +94,7 @@ COMMAND_TABLE cmd_table[] = {
{"1", CMD_FIRST},
{"next", CMD_NEXT},
{"n", CMD_NEXT},
{"check", CMD_CHECK},
{"quit", CMD_QUIT},
{"q", CMD_QUIT},
{"!", CMD_SYSTEM},
@ -107,6 +115,18 @@ static double _end_timer(void)
(tp2.tv_usec - tp1.tv_usec)*1.0e-6);
}
#ifdef PRINTF_ATTRIBUTE
static void tdb_log(struct tdb_context *tdb, enum tdb_debug_level level, const char *format, ...) PRINTF_ATTRIBUTE(3,4);
#endif
static void tdb_log(struct tdb_context *tdb, enum tdb_debug_level level, const char *format, ...)
{
va_list ap;
va_start(ap, format);
vfprintf(stderr, format, ap);
va_end(ap);
}
/* a tdb tool for manipulating a tdb database */
static TDB_CONTEXT *tdb;
@ -167,6 +187,9 @@ static void help(void)
"tdbtool: \n"
" create dbname : create a database\n"
" open dbname : open an existing database\n"
" transaction_start : start a transaction\n"
" transaction_commit : commit a transaction\n"
" transaction_cancel : cancel a transaction\n"
" erase : erase the database\n"
" dump : dump the database as strings\n"
" keys : dump the database keys as strings\n"
@ -179,7 +202,9 @@ static void help(void)
" delete key : delete a record by key\n"
" list : print the database hash table and freelist\n"
" free : print the database freelist\n"
" ! command : execute system command\n"
" check : check the integrity of an opened database\n"
" speed : perform speed tests on the database\n"
" ! command : execute system command\n"
" 1 | first : print the first record\n"
" n | next : print the next record\n"
" q | quit : terminate\n"
@ -194,9 +219,12 @@ static void terror(const char *why)
static void create_tdb(const char *tdbname)
{
struct tdb_logging_context log_ctx;
log_ctx.log_fn = tdb_log;
if (tdb) tdb_close(tdb);
tdb = tdb_open(tdbname, 0, TDB_CLEAR_IF_FIRST | (disable_mmap?TDB_NOMMAP:0),
O_RDWR | O_CREAT | O_TRUNC, 0600);
tdb = tdb_open_ex(tdbname, 0, TDB_CLEAR_IF_FIRST | (disable_mmap?TDB_NOMMAP:0),
O_RDWR | O_CREAT | O_TRUNC, 0600, &log_ctx, NULL);
if (!tdb) {
printf("Could not create %s: %s\n", tdbname, strerror(errno));
}
@ -204,8 +232,12 @@ static void create_tdb(const char *tdbname)
static void open_tdb(const char *tdbname)
{
struct tdb_logging_context log_ctx;
log_ctx.log_fn = tdb_log;
if (tdb) tdb_close(tdb);
tdb = tdb_open(tdbname, 0, disable_mmap?TDB_NOMMAP:0, O_RDWR, 0600);
tdb = tdb_open_ex(tdbname, 0, disable_mmap?TDB_NOMMAP:0, O_RDWR, 0600,
&log_ctx, NULL);
if (!tdb) {
printf("Could not open %s: %s\n", tdbname, strerror(errno));
}
@ -387,17 +419,71 @@ static void info_tdb(void)
static void speed_tdb(const char *tlimit)
{
const char *str = "store test", *str2 = "transaction test";
unsigned timelimit = tlimit?atoi(tlimit):0;
double t;
int ops=0;
if (timelimit == 0) timelimit = 10;
int ops;
if (timelimit == 0) timelimit = 5;
ops = 0;
printf("Testing store speed for %u seconds\n", timelimit);
_start_timer();
do {
long int r = random();
TDB_DATA key, dbuf;
key.dptr = discard_const_p(uint8_t, str);
key.dsize = strlen((char *)key.dptr);
dbuf.dptr = (uint8_t *) &r;
dbuf.dsize = sizeof(r);
tdb_store(tdb, key, dbuf, TDB_REPLACE);
t = _end_timer();
ops++;
} while (t < timelimit);
printf("%10.3f ops/sec\n", ops/t);
ops = 0;
printf("Testing fetch speed for %u seconds\n", timelimit);
_start_timer();
do {
long int r = random();
TDB_DATA key, dbuf;
key.dptr = discard_const_p(uint8_t, str);
key.dsize = strlen((char *)key.dptr);
dbuf.dptr = (uint8_t *) &r;
dbuf.dsize = sizeof(r);
tdb_fetch(tdb, key);
t = _end_timer();
ops++;
} while (t < timelimit);
printf("%10.3f ops/sec\n", ops/t);
ops = 0;
printf("Testing transaction speed for %u seconds\n", timelimit);
_start_timer();
do {
long int r = random();
TDB_DATA key, dbuf;
key.dptr = discard_const_p(uint8_t, str2);
key.dsize = strlen((char *)key.dptr);
dbuf.dptr = (uint8_t *) &r;
dbuf.dsize = sizeof(r);
tdb_transaction_start(tdb);
tdb_store(tdb, key, dbuf, TDB_REPLACE);
tdb_transaction_commit(tdb);
t = _end_timer();
ops++;
} while (t < timelimit);
printf("%10.3f ops/sec\n", ops/t);
ops = 0;
printf("Testing traverse speed for %u seconds\n", timelimit);
_start_timer();
while ((t=_end_timer()) < timelimit) {
do {
tdb_traverse(tdb, traverse_fn, NULL);
printf("%10.3f ops/sec\r", (++ops)/t);
}
printf("\n");
t = _end_timer();
ops++;
} while (t < timelimit);
printf("%10.3f ops/sec\n", ops/t);
}
static void toggle_mmap(void)
@ -452,6 +538,24 @@ static void next_record(TDB_CONTEXT *the_tdb, TDB_DATA *pkey)
print_rec(the_tdb, *pkey, dbuf, NULL);
}
static int count(TDB_DATA key, TDB_DATA data, void *private)
{
(*(unsigned int *)private)++;
return 0;
}
static void check_db(TDB_CONTEXT *the_tdb)
{
int tdbcount = 0;
if (!the_tdb)
printf("Error: No database opened!\n");
else if (tdb_check(the_tdb, count, &tdbcount) == -1)
printf("Integrity check for the opened database failed.\n");
else
printf("Database integrity is OK and has %d records.\n",
tdbcount);
}
static int do_command(void)
{
COMMAND_TABLE *ctp = cmd_table;
@ -459,112 +563,129 @@ static int do_command(void)
int cmd_len;
if (cmdname && strlen(cmdname) == 0) {
mycmd = CMD_NEXT;
mycmd = CMD_NEXT;
} else {
while (ctp->name) {
cmd_len = strlen(ctp->name);
if (strncmp(ctp->name,cmdname,cmd_len) == 0) {
mycmd = ctp->cmd;
break;
while (ctp->name) {
cmd_len = strlen(ctp->name);
if (strncmp(ctp->name,cmdname,cmd_len) == 0) {
mycmd = ctp->cmd;
break;
}
ctp++;
}
ctp++;
}
}
switch (mycmd) {
case CMD_CREATE_TDB:
bIterate = 0;
create_tdb(arg1);
return 0;
bIterate = 0;
create_tdb(arg1);
return 0;
case CMD_OPEN_TDB:
bIterate = 0;
open_tdb(arg1);
return 0;
bIterate = 0;
open_tdb(arg1);
return 0;
case CMD_SYSTEM:
/* Shell command */
system(arg1);
return 0;
/* Shell command */
if (system(arg1) == -1) {
terror("system() call failed\n");
}
return 0;
case CMD_QUIT:
return 1;
return 1;
default:
/* all the rest require a open database */
if (!tdb) {
bIterate = 0;
terror("database not open");
help();
return 0;
}
switch (mycmd) {
case CMD_ERASE:
bIterate = 0;
tdb_traverse(tdb, do_delete_fn, NULL);
return 0;
case CMD_DUMP:
bIterate = 0;
tdb_traverse(tdb, print_rec, NULL);
return 0;
case CMD_INSERT:
bIterate = 0;
insert_tdb(arg1, arg1len,arg2,arg2len);
return 0;
case CMD_MOVE:
bIterate = 0;
move_rec(arg1,arg1len,arg2);
return 0;
case CMD_STORE:
bIterate = 0;
store_tdb(arg1,arg1len,arg2,arg2len);
return 0;
case CMD_SHOW:
bIterate = 0;
show_tdb(arg1, arg1len);
return 0;
case CMD_KEYS:
tdb_traverse(tdb, print_key, NULL);
return 0;
case CMD_HEXKEYS:
tdb_traverse(tdb, print_hexkey, NULL);
return 0;
case CMD_DELETE:
bIterate = 0;
delete_tdb(arg1,arg1len);
return 0;
case CMD_LIST_HASH_FREE:
tdb_dump_all(tdb);
return 0;
case CMD_LIST_FREE:
tdb_printfreelist(tdb);
return 0;
case CMD_INFO:
info_tdb();
return 0;
case CMD_SPEED:
speed_tdb(arg1);
return 0;
case CMD_MMAP:
toggle_mmap();
return 0;
case CMD_FIRST:
bIterate = 1;
first_record(tdb, &iterate_kbuf);
return 0;
case CMD_NEXT:
if (bIterate)
next_record(tdb, &iterate_kbuf);
return 0;
case CMD_HELP:
help();
return 0;
case CMD_CREATE_TDB:
case CMD_OPEN_TDB:
case CMD_SYSTEM:
case CMD_QUIT:
/*
* unhandled commands. cases included here to avoid compiler
* warnings.
*/
return 0;
}
/* all the rest require a open database */
if (!tdb) {
bIterate = 0;
terror("database not open");
help();
return 0;
}
switch (mycmd) {
case CMD_TRANSACTION_START:
bIterate = 0;
tdb_transaction_start(tdb);
return 0;
case CMD_TRANSACTION_COMMIT:
bIterate = 0;
tdb_transaction_commit(tdb);
return 0;
case CMD_TRANSACTION_CANCEL:
bIterate = 0;
tdb_transaction_cancel(tdb);
return 0;
case CMD_ERASE:
bIterate = 0;
tdb_traverse(tdb, do_delete_fn, NULL);
return 0;
case CMD_DUMP:
bIterate = 0;
tdb_traverse(tdb, print_rec, NULL);
return 0;
case CMD_INSERT:
bIterate = 0;
insert_tdb(arg1, arg1len,arg2,arg2len);
return 0;
case CMD_MOVE:
bIterate = 0;
move_rec(arg1,arg1len,arg2);
return 0;
case CMD_STORE:
bIterate = 0;
store_tdb(arg1,arg1len,arg2,arg2len);
return 0;
case CMD_SHOW:
bIterate = 0;
show_tdb(arg1, arg1len);
return 0;
case CMD_KEYS:
tdb_traverse(tdb, print_key, NULL);
return 0;
case CMD_HEXKEYS:
tdb_traverse(tdb, print_hexkey, NULL);
return 0;
case CMD_DELETE:
bIterate = 0;
delete_tdb(arg1,arg1len);
return 0;
case CMD_LIST_HASH_FREE:
tdb_dump_all(tdb);
return 0;
case CMD_LIST_FREE:
tdb_printfreelist(tdb);
return 0;
case CMD_INFO:
info_tdb();
return 0;
case CMD_SPEED:
speed_tdb(arg1);
return 0;
case CMD_MMAP:
toggle_mmap();
return 0;
case CMD_FIRST:
bIterate = 1;
first_record(tdb, &iterate_kbuf);
return 0;
case CMD_NEXT:
if (bIterate)
next_record(tdb, &iterate_kbuf);
return 0;
case CMD_CHECK:
check_db(tdb);
return 0;
case CMD_HELP:
help();
return 0;
case CMD_CREATE_TDB:
case CMD_OPEN_TDB:
case CMD_SYSTEM:
case CMD_QUIT:
/*
* unhandled commands. cases included here to avoid compiler
* warnings.
*/
return 0;
}
}
return 0;
@ -572,88 +693,87 @@ static int do_command(void)
static char *convert_string(char *instring, size_t *sizep)
{
size_t length = 0;
char *outp, *inp;
char temp[3];
size_t length = 0;
char *outp, *inp;
char temp[3];
outp = inp = instring;
outp = inp = instring;
while (*inp) {
if (*inp == '\\') {
inp++;
if (*inp && strchr("0123456789abcdefABCDEF",(int)*inp)) {
temp[0] = *inp++;
temp[1] = '\0';
if (*inp && strchr("0123456789abcdefABCDEF",(int)*inp)) {
temp[1] = *inp++;
temp[2] = '\0';
while (*inp) {
if (*inp == '\\') {
inp++;
if (*inp && strchr("0123456789abcdefABCDEF",(int)*inp)) {
temp[0] = *inp++;
temp[1] = '\0';
if (*inp && strchr("0123456789abcdefABCDEF",(int)*inp)) {
temp[1] = *inp++;
temp[2] = '\0';
}
*outp++ = (char)strtol((const char *)temp,NULL,16);
} else {
*outp++ = *inp++;
}
} else {
*outp++ = *inp++;
}
*outp++ = (char)strtol((const char *)temp,NULL,16);
} else {
*outp++ = *inp++;
}
} else {
*outp++ = *inp++;
length++;
}
length++;
}
*sizep = length;
return instring;
*sizep = length;
return instring;
}
int main(int argc, char *argv[])
{
cmdname = "";
arg1 = NULL;
arg1len = 0;
arg2 = NULL;
arg2len = 0;
if (argv[1]) {
cmdname = "open";
arg1 = argv[1];
do_command();
cmdname = "";
cmdname = "";
arg1 = NULL;
}
arg1len = 0;
arg2 = NULL;
arg2len = 0;
switch (argc) {
if (argv[1]) {
cmdname = "open";
arg1 = argv[1];
do_command();
cmdname = "";
arg1 = NULL;
}
switch (argc) {
case 1:
case 2:
/* Interactive mode */
while ((cmdname = tdb_getline("tdb> "))) {
arg2 = arg1 = NULL;
if ((arg1 = strchr((const char *)cmdname,' ')) != NULL) {
arg1++;
arg2 = arg1;
while (*arg2) {
if (*arg2 == ' ') {
*arg2++ = '\0';
break;
/* Interactive mode */
while ((cmdname = tdb_getline("tdb> "))) {
arg2 = arg1 = NULL;
if ((arg1 = strchr((const char *)cmdname,' ')) != NULL) {
arg1++;
arg2 = arg1;
while (*arg2) {
if (*arg2 == ' ') {
*arg2++ = '\0';
break;
}
if ((*arg2++ == '\\') && (*arg2 == ' ')) {
arg2++;
}
}
}
if ((*arg2++ == '\\') && (*arg2 == ' ')) {
arg2++;
}
}
if (arg1) arg1 = convert_string(arg1,&arg1len);
if (arg2) arg2 = convert_string(arg2,&arg2len);
if (do_command()) break;
}
if (arg1) arg1 = convert_string(arg1,&arg1len);
if (arg2) arg2 = convert_string(arg2,&arg2len);
if (do_command()) break;
}
break;
break;
case 5:
arg2 = convert_string(argv[4],&arg2len);
arg2 = convert_string(argv[4],&arg2len);
case 4:
arg1 = convert_string(argv[3],&arg1len);
arg1 = convert_string(argv[3],&arg1len);
case 3:
cmdname = argv[2];
cmdname = argv[2];
default:
do_command();
break;
}
do_command();
break;
}
if (tdb) tdb_close(tdb);
if (tdb) tdb_close(tdb);
return 0;
return 0;
}

View File

@ -18,6 +18,7 @@
#define STORE_PROB 4
#define APPEND_PROB 6
#define TRANSACTION_PROB 10
#define TRANSACTION_PREPARE_PROB 2
#define LOCKSTORE_PROB 5
#define TRAVERSE_PROB 20
#define TRAVERSE_READ_PROB 20
@ -28,6 +29,7 @@
static struct tdb_context *db;
static int in_transaction;
static int error_count;
static int always_transaction = 0;
#ifdef PRINTF_ATTRIBUTE
static void tdb_log(struct tdb_context *tdb, enum tdb_debug_level level, const char *format, ...) PRINTF_ATTRIBUTE(3,4);
@ -35,8 +37,11 @@ static void tdb_log(struct tdb_context *tdb, enum tdb_debug_level level, const c
static void tdb_log(struct tdb_context *tdb, enum tdb_debug_level level, const char *format, ...)
{
va_list ap;
error_count++;
/* trace level messages do not indicate an error */
if (level != TDB_DEBUG_TRACE) {
error_count++;
}
va_start(ap, format);
vfprintf(stdout, format, ap);
@ -100,8 +105,16 @@ static void addrec_db(void)
data.dptr = (unsigned char *)d;
data.dsize = dlen+1;
#if REOPEN_PROB
if (in_transaction == 0 && random() % REOPEN_PROB == 0) {
tdb_reopen_all(0);
goto next;
}
#endif
#if TRANSACTION_PROB
if (in_transaction == 0 && random() % TRANSACTION_PROB == 0) {
if (in_transaction == 0 &&
(always_transaction || random() % TRANSACTION_PROB == 0)) {
if (tdb_transaction_start(db) != 0) {
fatal("tdb_transaction_start failed");
}
@ -109,6 +122,11 @@ static void addrec_db(void)
goto next;
}
if (in_transaction && random() % TRANSACTION_PROB == 0) {
if (random() % TRANSACTION_PREPARE_PROB == 0) {
if (tdb_transaction_prepare_commit(db) != 0) {
fatal("tdb_transaction_prepare_commit failed");
}
}
if (tdb_transaction_commit(db) != 0) {
fatal("tdb_transaction_commit failed");
}
@ -124,13 +142,6 @@ static void addrec_db(void)
}
#endif
#if REOPEN_PROB
if (in_transaction == 0 && random() % REOPEN_PROB == 0) {
tdb_reopen_all(0);
goto next;
}
#endif
#if DELETE_PROB
if (random() % DELETE_PROB == 0) {
tdb_delete(db, key);
@ -200,7 +211,7 @@ static int traverse_fn(struct tdb_context *tdb, TDB_DATA key, TDB_DATA dbuf,
static void usage(void)
{
printf("Usage: tdbtorture [-n NUM_PROCS] [-l NUM_LOOPS] [-s SEED] [-H HASH_SIZE]\n");
printf("Usage: tdbtorture [-t] [-n NUM_PROCS] [-l NUM_LOOPS] [-s SEED] [-H HASH_SIZE]\n");
exit(0);
}
@ -217,7 +228,7 @@ static void usage(void)
struct tdb_logging_context log_ctx;
log_ctx.log_fn = tdb_log;
while ((c = getopt(argc, argv, "n:l:s:H:h")) != -1) {
while ((c = getopt(argc, argv, "n:l:s:H:th")) != -1) {
switch (c) {
case 'n':
num_procs = strtol(optarg, NULL, 0);
@ -231,6 +242,9 @@ static void usage(void)
case 's':
seed = strtol(optarg, NULL, 0);
break;
case 't':
always_transaction = 1;
break;
default:
usage();
}
@ -256,8 +270,8 @@ static void usage(void)
}
if (i == 0) {
printf("testing with %d processes, %d loops, %d hash_size, seed=%d\n",
num_procs, num_loops, hash_size, seed);
printf("testing with %d processes, %d loops, %d hash_size, seed=%d%s\n",
num_procs, num_loops, hash_size, seed, always_transaction ? " (all within transactions)" : "");
}
srand(seed + i);
@ -269,8 +283,20 @@ static void usage(void)
if (error_count == 0) {
tdb_traverse_read(db, NULL, NULL);
if (always_transaction) {
while (in_transaction) {
tdb_transaction_cancel(db);
in_transaction--;
}
if (tdb_transaction_start(db) != 0)
fatal("tdb_transaction_start failed");
}
tdb_traverse(db, traverse_fn, NULL);
tdb_traverse(db, traverse_fn, NULL);
if (always_transaction) {
if (tdb_transaction_commit(db) != 0)
fatal("tdb_transaction_commit failed");
}
}
tdb_close(db);
@ -310,6 +336,8 @@ static void usage(void)
pids[j] = 0;
}
free(pids);
if (error_count == 0) {
printf("OK\n");
}

View File

@ -0,0 +1,48 @@
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2//EN">
<HTML>
<HEAD>
<TITLE>ldb</TITLE>
</HEAD>
<BODY BGCOLOR="#ffffff" TEXT="#000000" VLINK="#292555" LINK="#292555" ALINK="#cc0033">
<h1>tdb</h1>
TDB is a Trivial Database. In concept, it is very much like GDBM, and BSD's DB
except that it allows multiple simultaneous writers and uses locking
internally to keep writers from trampling on each other. TDB is also extremely
small.
<h2>Download</h2>
You can download the latest releases of tdb from the <a
href="http://samba.org/ftp/tdb">tdb directory</a> on the samba public
source archive.
<h2>Discussion and bug reports</h2>
tdb does not currently have its own mailing list or bug tracking
system. For now, please use the <a
href="https://lists.samba.org/mailman/listinfo/samba-technical">samba-technical</a>
mailing list, and the <a href="http://bugzilla.samba.org/">Samba
bugzilla</a> bug tracking system.
<h2>Download</h2>
You can download the latest code either via git or rsync.<br>
<br>
To fetch via git see the following guide:<br>
<a href="http://wiki.samba.org/index.php/Using_Git_for_Samba_Development">Using Git for Samba Development</a><br>
Once you have cloned the tree switch to the master branch and cd into the source/lib/tdb directory.<br>
<br>
To fetch via rsync use these commands:
<pre>
rsync -Pavz samba.org::ftp/unpacked/standalone_projects/lib/tdb .
rsync -Pavz samba.org::ftp/unpacked/standalone_projects/lib/replace .
</pre>
and build in tdb. It will find the replace library in the directory
above automatically.
</BODY>
</HTML>

View File

@ -560,6 +560,14 @@ static int32_t ctdb_control_dispatch(struct ctdb_context *ctdb,
CHECK_CONTROL_DATA_SIZE(sizeof(uint64_t));
return ctdb_control_get_db_seqnum(ctdb, indata, outdata);
case CTDB_CONTROL_DB_SET_HEALTHY:
CHECK_CONTROL_DATA_SIZE(sizeof(uint32_t));
return ctdb_control_db_set_healthy(ctdb, indata);
case CTDB_CONTROL_DB_GET_HEALTH:
CHECK_CONTROL_DATA_SIZE(sizeof(uint32_t));
return ctdb_control_db_get_health(ctdb, indata, outdata);
default:
DEBUG(DEBUG_CRIT,(__location__ " Unknown CTDB control opcode %u\n", opcode));
return -1;

View File

@ -354,6 +354,16 @@ static void daemon_request_call_from_client(struct ctdb_client *client,
return;
}
if (ctdb_db->unhealthy_reason) {
/*
* this is just a warning, as the tdb should be empty anyway,
* and only persistent databases can be unhealthy, which doesn't
* use this code patch
*/
DEBUG(DEBUG_WARNING,("warn: db(%s) unhealty in daemon_request_call_from_client(): %s\n",
ctdb_db->db_name, ctdb_db->unhealthy_reason));
}
key.dptr = c->data;
key.dsize = c->keylen;
@ -764,9 +774,9 @@ int ctdb_start_daemon(struct ctdb_context *ctdb, bool do_fork, bool use_syslog)
ctdb_fatal(ctdb, "transport failed to initialise");
}
/* attach to any existing persistent databases */
if (ctdb_attach_persistent(ctdb) != 0) {
ctdb_fatal(ctdb, "Failed to attach to persistent databases\n");
/* attach to existing databases */
if (ctdb_attach_databases(ctdb) != 0) {
ctdb_fatal(ctdb, "Failed to attach to databases\n");
}
/* start frozen, then let the first election sort things out */

View File

@ -489,7 +489,8 @@ int32_t ctdb_control_transaction_commit(struct ctdb_context *ctdb, uint32_t id)
{
struct ctdb_db_context *ctdb_db;
int i;
int healthy_nodes = 0;
for (i=1;i<=NUM_DB_PRIORITIES; i++) {
if (ctdb->freeze_mode[i] != CTDB_FREEZE_FROZEN) {
DEBUG(DEBUG_ERR,(__location__ " Failed transaction_start while not frozen\n"));
@ -507,32 +508,55 @@ int32_t ctdb_control_transaction_commit(struct ctdb_context *ctdb, uint32_t id)
return -1;
}
DEBUG(DEBUG_DEBUG,(__location__ " num_nodes[%d]\n", ctdb->num_nodes));
for (i=0; i < ctdb->num_nodes; i++) {
DEBUG(DEBUG_DEBUG,(__location__ " node[%d].flags[0x%X]\n",
i, ctdb->nodes[i]->flags));
if (ctdb->nodes[i]->flags == 0) {
healthy_nodes++;
}
}
DEBUG(DEBUG_INFO,(__location__ " healthy_nodes[%d]\n", healthy_nodes));
for (ctdb_db=ctdb->db_list;ctdb_db;ctdb_db=ctdb_db->next) {
int ret;
tdb_add_flags(ctdb_db->ltdb->tdb, TDB_NOLOCK);
if (tdb_transaction_commit(ctdb_db->ltdb->tdb) != 0) {
ret = tdb_transaction_commit(ctdb_db->ltdb->tdb);
if (ret != 0) {
DEBUG(DEBUG_ERR,(__location__ " Failed to commit transaction for db '%s'. Cancel all transactions and resetting transaction_started to false.\n",
ctdb_db->db_name));
/* cancel any pending transactions */
for (ctdb_db=ctdb->db_list;ctdb_db;ctdb_db=ctdb_db->next) {
tdb_add_flags(ctdb_db->ltdb->tdb, TDB_NOLOCK);
if (tdb_transaction_cancel(ctdb_db->ltdb->tdb) != 0) {
DEBUG(DEBUG_ERR,(__location__ " Failed to cancel transaction for db '%s'\n",
ctdb_db->db_name));
}
tdb_remove_flags(ctdb_db->ltdb->tdb, TDB_NOLOCK);
}
ctdb->freeze_transaction_started = false;
return -1;
goto fail;
}
tdb_remove_flags(ctdb_db->ltdb->tdb, TDB_NOLOCK);
ret = ctdb_update_persistent_health(ctdb, ctdb_db, NULL, healthy_nodes);
if (ret != 0) {
DEBUG(DEBUG_CRIT,(__location__ " Failed to update persistent health for db '%s'. "
"Cancel all remaining transactions and resetting transaction_started to false.\n",
ctdb_db->db_name));
goto fail;
}
}
ctdb->freeze_transaction_started = false;
ctdb->freeze_transaction_id = 0;
return 0;
fail:
/* cancel any pending transactions */
for (ctdb_db=ctdb->db_list;ctdb_db;ctdb_db=ctdb_db->next) {
tdb_add_flags(ctdb_db->ltdb->tdb, TDB_NOLOCK);
if (tdb_transaction_cancel(ctdb_db->ltdb->tdb) != 0) {
DEBUG(DEBUG_ERR,(__location__ " Failed to cancel transaction for db '%s'\n",
ctdb_db->db_name));
}
tdb_remove_flags(ctdb_db->ltdb->tdb, TDB_NOLOCK);
}
ctdb->freeze_transaction_started = false;
return -1;
}
/*

View File

@ -23,11 +23,14 @@
#include "system/network.h"
#include "system/filesys.h"
#include "system/dir.h"
#include "system/time.h"
#include "../include/ctdb_private.h"
#include "db_wrap.h"
#include "lib/util/dlinklist.h"
#include <ctype.h>
#define PERSISTENT_HEALTH_TDB "persistent_health.tdb"
/*
this is the dummy null procedure that all databases support
*/
@ -188,17 +191,333 @@ static void ctdb_check_db_empty(struct ctdb_db_context *ctdb_db)
}
}
int ctdb_load_persistent_health(struct ctdb_context *ctdb,
struct ctdb_db_context *ctdb_db)
{
struct tdb_context *tdb = ctdb->db_persistent_health->tdb;
char *old;
char *reason = NULL;
TDB_DATA key;
TDB_DATA val;
key.dptr = discard_const_p(uint8_t, ctdb_db->db_name);
key.dsize = strlen(ctdb_db->db_name);
old = ctdb_db->unhealthy_reason;
ctdb_db->unhealthy_reason = NULL;
val = tdb_fetch(tdb, key);
if (val.dsize > 0) {
reason = talloc_strndup(ctdb_db,
(const char *)val.dptr,
val.dsize);
if (reason == NULL) {
DEBUG(DEBUG_ALERT,(__location__ " talloc_strndup(%d) failed\n",
(int)val.dsize));
ctdb_db->unhealthy_reason = old;
free(val.dptr);
return -1;
}
}
if (val.dptr) {
free(val.dptr);
}
talloc_free(old);
ctdb_db->unhealthy_reason = reason;
return 0;
}
int ctdb_update_persistent_health(struct ctdb_context *ctdb,
struct ctdb_db_context *ctdb_db,
const char *given_reason,/* NULL means healthy */
int num_healthy_nodes)
{
struct tdb_context *tdb = ctdb->db_persistent_health->tdb;
int ret;
TDB_DATA key;
TDB_DATA val;
char *new_reason = NULL;
char *old_reason = NULL;
ret = tdb_transaction_start(tdb);
if (ret != 0) {
DEBUG(DEBUG_ALERT,(__location__ " tdb_transaction_start('%s') failed: %d - %s\n",
tdb_name(tdb), ret, tdb_errorstr(tdb)));
return -1;
}
ret = ctdb_load_persistent_health(ctdb, ctdb_db);
if (ret != 0) {
DEBUG(DEBUG_ALERT,(__location__ " ctdb_load_persistent_health('%s') failed: %d\n",
ctdb_db->db_name, ret));
return -1;
}
old_reason = ctdb_db->unhealthy_reason;
key.dptr = discard_const_p(uint8_t, ctdb_db->db_name);
key.dsize = strlen(ctdb_db->db_name);
if (given_reason) {
new_reason = talloc_strdup(ctdb_db, given_reason);
if (new_reason == NULL) {
DEBUG(DEBUG_ALERT,(__location__ " talloc_strdup(%s) failed\n",
given_reason));
return -1;
}
} else if (old_reason && num_healthy_nodes == 0) {
/*
* If the reason indicates ok, but there where no healthy nodes
* available, that it means, we have not recovered valid content
* of the db. So if there's an old reason, prefix it with
* "NO-HEALTHY-NODES - "
*/
const char *prefix;
#define _TMP_PREFIX "NO-HEALTHY-NODES - "
ret = strncmp(_TMP_PREFIX, old_reason, strlen(_TMP_PREFIX));
if (ret != 0) {
prefix = _TMP_PREFIX;
} else {
prefix = "";
}
new_reason = talloc_asprintf(ctdb_db, "%s%s",
prefix, old_reason);
if (new_reason == NULL) {
DEBUG(DEBUG_ALERT,(__location__ " talloc_asprintf(%s%s) failed\n",
prefix, old_reason));
return -1;
}
#undef _TMP_PREFIX
}
if (new_reason) {
val.dptr = discard_const_p(uint8_t, new_reason);
val.dsize = strlen(new_reason);
ret = tdb_store(tdb, key, val, TDB_REPLACE);
if (ret != 0) {
tdb_transaction_cancel(tdb);
DEBUG(DEBUG_ALERT,(__location__ " tdb_store('%s', %s, %s) failed: %d - %s\n",
tdb_name(tdb), ctdb_db->db_name, new_reason,
ret, tdb_errorstr(tdb)));
talloc_free(new_reason);
return -1;
}
DEBUG(DEBUG_ALERT,("Updated db health for db(%s) to: %s\n",
ctdb_db->db_name, new_reason));
} else if (old_reason) {
ret = tdb_delete(tdb, key);
if (ret != 0) {
tdb_transaction_cancel(tdb);
DEBUG(DEBUG_ALERT,(__location__ " tdb_delete('%s', %s) failed: %d - %s\n",
tdb_name(tdb), ctdb_db->db_name,
ret, tdb_errorstr(tdb)));
talloc_free(new_reason);
return -1;
}
DEBUG(DEBUG_NOTICE,("Updated db health for db(%s): OK\n",
ctdb_db->db_name));
}
ret = tdb_transaction_commit(tdb);
if (ret != TDB_SUCCESS) {
DEBUG(DEBUG_ALERT,(__location__ " tdb_transaction_commit('%s') failed: %d - %s\n",
tdb_name(tdb), ret, tdb_errorstr(tdb)));
talloc_free(new_reason);
return -1;
}
talloc_free(old_reason);
ctdb_db->unhealthy_reason = new_reason;
return 0;
}
static int ctdb_backup_corrupted_tdb(struct ctdb_context *ctdb,
struct ctdb_db_context *ctdb_db)
{
time_t now = time(NULL);
char *new_path;
char *new_reason;
int ret;
struct tm *tm;
tm = gmtime(&now);
/* formatted like: foo.tdb.0.corrupted.20091204160825.0Z */
new_path = talloc_asprintf(ctdb_db, "%s.corrupted."
"%04u%02u%02u%02u%02u%02u.0Z",
ctdb_db->db_path,
tm->tm_year+1900, tm->tm_mon+1,
tm->tm_mday, tm->tm_hour, tm->tm_min,
tm->tm_sec);
if (new_path == NULL) {
DEBUG(DEBUG_CRIT,(__location__ " talloc_asprintf() failed\n"));
return -1;
}
new_reason = talloc_asprintf(ctdb_db,
"ERROR - Backup of corrupted TDB in '%s'",
new_path);
if (new_reason == NULL) {
DEBUG(DEBUG_CRIT,(__location__ " talloc_asprintf() failed\n"));
return -1;
}
ret = ctdb_update_persistent_health(ctdb, ctdb_db, new_reason, 0);
talloc_free(new_reason);
if (ret != 0) {
DEBUG(DEBUG_CRIT,(__location__
": ctdb_backup_corrupted_tdb(%s) not implemented yet\n",
ctdb_db->db_path));
return -1;
}
ret = rename(ctdb_db->db_path, new_path);
if (ret != 0) {
DEBUG(DEBUG_CRIT,(__location__
": ctdb_backup_corrupted_tdb(%s) rename to %s failed: %d - %s\n",
ctdb_db->db_path, new_path,
errno, strerror(errno)));
talloc_free(new_path);
return -1;
}
DEBUG(DEBUG_CRIT,(__location__
": ctdb_backup_corrupted_tdb(%s) renamed to %s\n",
ctdb_db->db_path, new_path));
talloc_free(new_path);
return 0;
}
int ctdb_recheck_persistent_health(struct ctdb_context *ctdb)
{
struct ctdb_db_context *ctdb_db;
int ret;
int ok = 0;
int fail = 0;
for (ctdb_db = ctdb->db_list; ctdb_db; ctdb_db = ctdb_db->next) {
if (!ctdb_db->persistent) {
continue;
}
ret = ctdb_load_persistent_health(ctdb, ctdb_db);
if (ret != 0) {
DEBUG(DEBUG_ALERT,(__location__
" load persistent health for '%s' failed\n",
ctdb_db->db_path));
return -1;
}
if (ctdb_db->unhealthy_reason == NULL) {
ok++;
DEBUG(DEBUG_INFO,(__location__
" persistent db '%s' healthy\n",
ctdb_db->db_path));
continue;
}
fail++;
DEBUG(DEBUG_ALERT,(__location__
" persistent db '%s' unhealthy: %s\n",
ctdb_db->db_path,
ctdb_db->unhealthy_reason));
}
DEBUG((fail!=0)?DEBUG_ALERT:DEBUG_NOTICE,
("ctdb_recheck_presistent_health: OK[%d] FAIL[%d]\n",
ok, fail));
if (fail != 0) {
return -1;
}
return 0;
}
/*
mark a database - as healthy
*/
int32_t ctdb_control_db_set_healthy(struct ctdb_context *ctdb, TDB_DATA indata)
{
uint32_t db_id = *(uint32_t *)indata.dptr;
struct ctdb_db_context *ctdb_db;
int ret;
bool may_recover = false;
ctdb_db = find_ctdb_db(ctdb, db_id);
if (!ctdb_db) {
DEBUG(DEBUG_ERR,(__location__ " Unknown db 0x%x\n", db_id));
return -1;
}
if (ctdb_db->unhealthy_reason) {
may_recover = true;
}
ret = ctdb_update_persistent_health(ctdb, ctdb_db, NULL, 1);
if (ret != 0) {
DEBUG(DEBUG_ERR,(__location__
" ctdb_update_persistent_health(%s) failed\n",
ctdb_db->db_name));
return -1;
}
if (may_recover && !ctdb->done_startup) {
DEBUG(DEBUG_ERR, (__location__ " db %s become healthy - force recovery for startup\n",
ctdb_db->db_name));
ctdb->recovery_mode = CTDB_RECOVERY_ACTIVE;
}
return 0;
}
int32_t ctdb_control_db_get_health(struct ctdb_context *ctdb,
TDB_DATA indata,
TDB_DATA *outdata)
{
uint32_t db_id = *(uint32_t *)indata.dptr;
struct ctdb_db_context *ctdb_db;
int ret;
ctdb_db = find_ctdb_db(ctdb, db_id);
if (!ctdb_db) {
DEBUG(DEBUG_ERR,(__location__ " Unknown db 0x%x\n", db_id));
return -1;
}
ret = ctdb_load_persistent_health(ctdb, ctdb_db);
if (ret != 0) {
DEBUG(DEBUG_ERR,(__location__
" ctdb_load_persistent_health(%s) failed\n",
ctdb_db->db_name));
return -1;
}
*outdata = tdb_null;
if (ctdb_db->unhealthy_reason) {
outdata->dptr = (uint8_t *)ctdb_db->unhealthy_reason;
outdata->dsize = strlen(ctdb_db->unhealthy_reason)+1;
}
return 0;
}
/*
attach to a database, handling both persistent and non-persistent databases
return 0 on success, -1 on failure
*/
static int ctdb_local_attach(struct ctdb_context *ctdb, const char *db_name, bool persistent)
static int ctdb_local_attach(struct ctdb_context *ctdb, const char *db_name,
bool persistent, const char *unhealthy_reason)
{
struct ctdb_db_context *ctdb_db, *tmp_db;
int ret;
struct TDB_DATA key;
unsigned tdb_flags;
int mode = 0600;
int remaining_tries = 0;
ctdb_db = talloc_zero(ctdb, struct ctdb_db_context);
CTDB_NO_MEMORY(ctdb, ctdb_db);
@ -223,23 +542,45 @@ static int ctdb_local_attach(struct ctdb_context *ctdb, const char *db_name, boo
}
}
if (ctdb->db_directory == NULL) {
ctdb->db_directory = VARDIR "/ctdb";
if (persistent) {
if (unhealthy_reason) {
ret = ctdb_update_persistent_health(ctdb, ctdb_db,
unhealthy_reason, 0);
if (ret != 0) {
DEBUG(DEBUG_ALERT,(__location__ " ctdb_update_persistent_health('%s','%s') failed: %d\n",
ctdb_db->db_name, unhealthy_reason, ret));
talloc_free(ctdb_db);
return -1;
}
}
if (ctdb->max_persistent_check_errors > 0) {
remaining_tries = 1;
}
if (ctdb->done_startup) {
remaining_tries = 0;
}
ret = ctdb_load_persistent_health(ctdb, ctdb_db);
if (ret != 0) {
DEBUG(DEBUG_ALERT,(__location__ " ctdb_load_persistent_health('%s') failed: %d\n",
ctdb_db->db_name, ret));
talloc_free(ctdb_db);
return -1;
}
}
/* make sure the db directory exists */
if (mkdir(ctdb->db_directory, 0700) == -1 && errno != EEXIST) {
DEBUG(DEBUG_CRIT,(__location__ " Unable to create ctdb directory '%s'\n",
ctdb->db_directory));
if (ctdb_db->unhealthy_reason && remaining_tries == 0) {
DEBUG(DEBUG_ALERT,(__location__ "ERROR: tdb %s is marked as unhealthy: %s\n",
ctdb_db->db_name, ctdb_db->unhealthy_reason));
talloc_free(ctdb_db);
return -1;
}
if (persistent && mkdir(ctdb->db_directory_persistent, 0700) == -1 && errno != EEXIST) {
DEBUG(DEBUG_CRIT,(__location__ " Unable to create ctdb persistent directory '%s'\n",
ctdb->db_directory_persistent));
talloc_free(ctdb_db);
return -1;
if (ctdb_db->unhealthy_reason) {
/* this is just a warning, but we want that in the log file! */
DEBUG(DEBUG_ALERT,(__location__ "Warning: tdb %s is marked as unhealthy: %s\n",
ctdb_db->db_name, ctdb_db->unhealthy_reason));
}
/* open the database */
@ -253,18 +594,105 @@ static int ctdb_local_attach(struct ctdb_context *ctdb, const char *db_name, boo
}
tdb_flags |= TDB_DISALLOW_NESTING;
again:
ctdb_db->ltdb = tdb_wrap_open(ctdb, ctdb_db->db_path,
ctdb->tunable.database_hash_size,
tdb_flags,
O_CREAT|O_RDWR, 0666);
O_CREAT|O_RDWR, mode);
if (ctdb_db->ltdb == NULL) {
DEBUG(DEBUG_CRIT,("Failed to open tdb '%s'\n", ctdb_db->db_path));
talloc_free(ctdb_db);
return -1;
struct stat st;
int saved_errno = errno;
if (!persistent) {
DEBUG(DEBUG_CRIT,("Failed to open tdb '%s': %d - %s\n",
ctdb_db->db_path,
saved_errno,
strerror(saved_errno)));
talloc_free(ctdb_db);
return -1;
}
if (remaining_tries == 0) {
DEBUG(DEBUG_CRIT,(__location__
"Failed to open persistent tdb '%s': %d - %s\n",
ctdb_db->db_path,
saved_errno,
strerror(saved_errno)));
talloc_free(ctdb_db);
return -1;
}
ret = stat(ctdb_db->db_path, &st);
if (ret != 0) {
DEBUG(DEBUG_CRIT,(__location__
"Failed to open persistent tdb '%s': %d - %s\n",
ctdb_db->db_path,
saved_errno,
strerror(saved_errno)));
talloc_free(ctdb_db);
return -1;
}
ret = ctdb_backup_corrupted_tdb(ctdb, ctdb_db);
if (ret != 0) {
DEBUG(DEBUG_CRIT,(__location__
"Failed to open persistent tdb '%s': %d - %s\n",
ctdb_db->db_path,
saved_errno,
strerror(saved_errno)));
talloc_free(ctdb_db);
return -1;
}
remaining_tries--;
mode = st.st_mode;
goto again;
}
if (!persistent) {
ctdb_check_db_empty(ctdb_db);
} else {
ret = tdb_check(ctdb_db->ltdb->tdb, NULL, NULL);
if (ret != 0) {
int fd;
struct stat st;
DEBUG(DEBUG_CRIT,("tdb_check(%s) failed: %d - %s\n",
ctdb_db->db_path, ret,
tdb_errorstr(ctdb_db->ltdb->tdb)));
if (remaining_tries == 0) {
talloc_free(ctdb_db);
return -1;
}
fd = tdb_fd(ctdb_db->ltdb->tdb);
ret = fstat(fd, &st);
if (ret != 0) {
DEBUG(DEBUG_CRIT,(__location__
"Failed to fstat() persistent tdb '%s': %d - %s\n",
ctdb_db->db_path,
errno,
strerror(errno)));
talloc_free(ctdb_db);
return -1;
}
/* close the TDB */
talloc_free(ctdb_db->ltdb);
ctdb_db->ltdb = NULL;
ret = ctdb_backup_corrupted_tdb(ctdb, ctdb_db);
if (ret != 0) {
DEBUG(DEBUG_CRIT,("Failed to backup corrupted tdb '%s'\n",
ctdb_db->db_path));
talloc_free(ctdb_db);
return -1;
}
remaining_tries--;
mode = st.st_mode;
goto again;
}
}
DLIST_ADD(ctdb->db_list, ctdb_db);
@ -346,7 +774,7 @@ int32_t ctdb_control_db_attach(struct ctdb_context *ctdb, TDB_DATA indata,
return 0;
}
if (ctdb_local_attach(ctdb, db_name, persistent) != 0) {
if (ctdb_local_attach(ctdb, db_name, persistent, NULL) != 0) {
return -1;
}
@ -377,7 +805,8 @@ int32_t ctdb_control_db_attach(struct ctdb_context *ctdb, TDB_DATA indata,
/*
attach to all existing persistent databases
*/
int ctdb_attach_persistent(struct ctdb_context *ctdb)
static int ctdb_attach_persistent(struct ctdb_context *ctdb,
const char *unhealthy_reason)
{
DIR *d;
struct dirent *de;
@ -418,12 +847,13 @@ int ctdb_attach_persistent(struct ctdb_context *ctdb)
}
p[4] = 0;
if (ctdb_local_attach(ctdb, s, true) != 0) {
if (ctdb_local_attach(ctdb, s, true, unhealthy_reason) != 0) {
DEBUG(DEBUG_ERR,("Failed to attach to persistent database '%s'\n", de->d_name));
closedir(d);
talloc_free(s);
return -1;
}
DEBUG(DEBUG_INFO,("Attached to persistent database %s\n", s));
talloc_free(s);
@ -432,6 +862,158 @@ int ctdb_attach_persistent(struct ctdb_context *ctdb)
return 0;
}
int ctdb_attach_databases(struct ctdb_context *ctdb)
{
int ret;
char *persistent_health_path = NULL;
char *unhealthy_reason = NULL;
bool first_try = true;
if (ctdb->db_directory == NULL) {
ctdb->db_directory = VARDIR "/ctdb";
}
if (ctdb->db_directory_persistent == NULL) {
ctdb->db_directory_persistent = VARDIR "/ctdb/persistent";
}
if (ctdb->db_directory_state == NULL) {
ctdb->db_directory_state = VARDIR "/ctdb/state";
}
/* make sure the db directory exists */
ret = mkdir(ctdb->db_directory, 0700);
if (ret == -1 && errno != EEXIST) {
DEBUG(DEBUG_CRIT,(__location__ " Unable to create ctdb directory '%s'\n",
ctdb->db_directory));
return -1;
}
/* make sure the persistent db directory exists */
ret = mkdir(ctdb->db_directory_persistent, 0700);
if (ret == -1 && errno != EEXIST) {
DEBUG(DEBUG_CRIT,(__location__ " Unable to create ctdb persistent directory '%s'\n",
ctdb->db_directory_persistent));
return -1;
}
/* make sure the internal state db directory exists */
ret = mkdir(ctdb->db_directory_state, 0700);
if (ret == -1 && errno != EEXIST) {
DEBUG(DEBUG_CRIT,(__location__ " Unable to create ctdb state directory '%s'\n",
ctdb->db_directory_state));
return -1;
}
persistent_health_path = talloc_asprintf(ctdb, "%s/%s.%u",
ctdb->db_directory_state,
PERSISTENT_HEALTH_TDB,
ctdb->pnn);
if (persistent_health_path == NULL) {
DEBUG(DEBUG_CRIT,(__location__ " talloc_asprintf() failed\n"));
return -1;
}
again:
ctdb->db_persistent_health = tdb_wrap_open(ctdb, persistent_health_path,
0, TDB_DISALLOW_NESTING,
O_CREAT | O_RDWR, 0600);
if (ctdb->db_persistent_health == NULL) {
struct tdb_wrap *tdb;
if (!first_try) {
DEBUG(DEBUG_CRIT,("Failed to open tdb '%s': %d - %s\n",
persistent_health_path,
errno,
strerror(errno)));
talloc_free(persistent_health_path);
talloc_free(unhealthy_reason);
return -1;
}
first_try = false;
unhealthy_reason = talloc_asprintf(ctdb, "WARNING - '%s' %s - %s",
persistent_health_path,
"was cleared after a failure",
"manual verification needed");
if (unhealthy_reason == NULL) {
DEBUG(DEBUG_CRIT,(__location__ " talloc_asprintf() failed\n"));
talloc_free(persistent_health_path);
return -1;
}
DEBUG(DEBUG_CRIT,("Failed to open tdb '%s' - retrying after CLEAR_IF_FIRST\n",
persistent_health_path));
tdb = tdb_wrap_open(ctdb, persistent_health_path,
0, TDB_CLEAR_IF_FIRST | TDB_DISALLOW_NESTING,
O_CREAT | O_RDWR, 0600);
if (tdb) {
DEBUG(DEBUG_CRIT,("Failed to open tdb '%s' - with CLEAR_IF_FIRST: %d - %s\n",
persistent_health_path,
errno,
strerror(errno)));
talloc_free(persistent_health_path);
talloc_free(unhealthy_reason);
return -1;
}
talloc_free(tdb);
goto again;
}
ret = tdb_check(ctdb->db_persistent_health->tdb, NULL, NULL);
if (ret != 0) {
struct tdb_wrap *tdb;
talloc_free(ctdb->db_persistent_health);
ctdb->db_persistent_health = NULL;
if (!first_try) {
DEBUG(DEBUG_CRIT,("tdb_check('%s') failed\n",
persistent_health_path));
talloc_free(persistent_health_path);
talloc_free(unhealthy_reason);
return -1;
}
first_try = false;
unhealthy_reason = talloc_asprintf(ctdb, "WARNING - '%s' %s - %s",
persistent_health_path,
"was cleared after a failure",
"manual verification needed");
if (unhealthy_reason == NULL) {
DEBUG(DEBUG_CRIT,(__location__ " talloc_asprintf() failed\n"));
talloc_free(persistent_health_path);
return -1;
}
DEBUG(DEBUG_CRIT,("tdb_check('%s') failed - retrying after CLEAR_IF_FIRST\n",
persistent_health_path));
tdb = tdb_wrap_open(ctdb, persistent_health_path,
0, TDB_CLEAR_IF_FIRST | TDB_DISALLOW_NESTING,
O_CREAT | O_RDWR, 0600);
if (tdb) {
DEBUG(DEBUG_CRIT,("Failed to open tdb '%s' - with CLEAR_IF_FIRST: %d - %s\n",
persistent_health_path,
errno,
strerror(errno)));
talloc_free(persistent_health_path);
talloc_free(unhealthy_reason);
return -1;
}
talloc_free(tdb);
goto again;
}
talloc_free(persistent_health_path);
ret = ctdb_attach_persistent(ctdb, unhealthy_reason);
talloc_free(unhealthy_reason);
if (ret != 0) {
return ret;
}
return 0;
}
/*
called when a broadcast seqnum update comes in
*/
@ -449,6 +1031,12 @@ int32_t ctdb_ltdb_update_seqnum(struct ctdb_context *ctdb, uint32_t db_id, uint3
return -1;
}
if (ctdb_db->unhealthy_reason) {
DEBUG(DEBUG_ERR,("db(%s) unhealty in ctdb_ltdb_update_seqnum: %s\n",
ctdb_db->db_name, ctdb_db->unhealthy_reason));
return -1;
}
tdb_increment_seqnum_nonblock(ctdb_db->ltdb->tdb);
ctdb_db->seqnum = tdb_get_seqnum(ctdb_db->ltdb->tdb);
return 0;

View File

@ -220,10 +220,13 @@ static void ctdb_wait_until_recovered(struct event_context *ev, struct timed_eve
struct timeval t, void *private_data)
{
struct ctdb_context *ctdb = talloc_get_type(private_data, struct ctdb_context);
int ret;
DEBUG(DEBUG_NOTICE,("CTDB_WAIT_UNTIL_RECOVERED\n"));
if (ctdb->vnn_map->generation == INVALID_GENERATION) {
ctdb->db_persistent_startup_generation = INVALID_GENERATION;
DEBUG(DEBUG_NOTICE,(__location__ " generation is INVALID. Wait one more second\n"));
event_add_timed(ctdb->ev, ctdb->monitor->monitor_context,
timeval_current_ofs(1, 0),
@ -232,6 +235,8 @@ static void ctdb_wait_until_recovered(struct event_context *ev, struct timed_eve
}
if (ctdb->recovery_mode != CTDB_RECOVERY_NORMAL) {
ctdb->db_persistent_startup_generation = INVALID_GENERATION;
DEBUG(DEBUG_NOTICE,(__location__ " in recovery. Wait one more second\n"));
event_add_timed(ctdb->ev, ctdb->monitor->monitor_context,
timeval_current_ofs(1, 0),
@ -241,6 +246,8 @@ static void ctdb_wait_until_recovered(struct event_context *ev, struct timed_eve
if (timeval_elapsed(&ctdb->last_recovery_finished) < (ctdb->tunable.rerecovery_timeout + 3)) {
ctdb->db_persistent_startup_generation = INVALID_GENERATION;
DEBUG(DEBUG_NOTICE,(__location__ " wait for pending recoveries to end. Wait one more second.\n"));
event_add_timed(ctdb->ev, ctdb->monitor->monitor_context,
@ -249,6 +256,48 @@ static void ctdb_wait_until_recovered(struct event_context *ev, struct timed_eve
return;
}
if (ctdb->vnn_map->generation == ctdb->db_persistent_startup_generation) {
DEBUG(DEBUG_INFO,(__location__ " skip ctdb_recheck_persistent_health() "
"until the next recovery\n"));
event_add_timed(ctdb->ev, ctdb->monitor->monitor_context,
timeval_current_ofs(1, 0),
ctdb_wait_until_recovered, ctdb);
return;
}
ctdb->db_persistent_startup_generation = ctdb->vnn_map->generation;
ret = ctdb_recheck_persistent_health(ctdb);
if (ret != 0) {
ctdb->db_persistent_check_errors++;
if (ctdb->db_persistent_check_errors < ctdb->max_persistent_check_errors) {
DEBUG(ctdb->db_persistent_check_errors==1?DEBUG_ERR:DEBUG_WARNING,
(__location__ "ctdb_recheck_persistent_health() "
"failed (%llu of %llu times) - retry later\n",
(unsigned long long)ctdb->db_persistent_check_errors,
(unsigned long long)ctdb->max_persistent_check_errors));
event_add_timed(ctdb->ev,
ctdb->monitor->monitor_context,
timeval_current_ofs(1, 0),
ctdb_wait_until_recovered, ctdb);
return;
}
DEBUG(DEBUG_ALERT,(__location__
"ctdb_recheck_persistent_health() failed (%llu times) - prepare shutdown\n",
(unsigned long long)ctdb->db_persistent_check_errors));
ctdb_stop_recoverd(ctdb);
ctdb_stop_keepalive(ctdb);
ctdb_stop_monitoring(ctdb);
ctdb_release_all_ips(ctdb);
if (ctdb->methods != NULL) {
ctdb->methods->shutdown(ctdb);
}
ctdb_event_script(ctdb, CTDB_EVENT_SHUTDOWN);
DEBUG(DEBUG_ALERT,("ctdb_recheck_persistent_health() failed - Stopping CTDB daemon\n"));
exit(11);
}
ctdb->db_persistent_check_errors = 0;
DEBUG(DEBUG_NOTICE,(__location__
"ctdb_start_monitoring: ctdb_recheck_persistent_health() OK\n"));
DEBUG(DEBUG_NOTICE,(__location__ " Recoveries finished. Running the \"startup\" event.\n"));
event_add_timed(ctdb->ev, ctdb->monitor->monitor_context,
@ -421,6 +470,11 @@ int32_t ctdb_control_modflags(struct ctdb_context *ctdb, TDB_DATA indata)
DEBUG(DEBUG_INFO, ("Control modflags on node %u - flags now 0x%x\n", c->pnn, node->flags));
if (node->flags == 0 && !ctdb->done_startup) {
DEBUG(DEBUG_ERR, (__location__ " Node %u became healthy - force recovery for startup\n",
c->pnn));
ctdb->recovery_mode = CTDB_RECOVERY_ACTIVE;
}
/* tell the recovery daemon something has changed */
ctdb_daemon_send_message(ctdb, ctdb->pnn,

View File

@ -117,6 +117,12 @@ int32_t ctdb_control_trans2_commit(struct ctdb_context *ctdb,
return -1;
}
if (ctdb_db->unhealthy_reason) {
DEBUG(DEBUG_ERR,("db(%s) unhealty in ctdb_control_trans2_commit: %s\n",
ctdb_db->db_name, ctdb_db->unhealthy_reason));
return -1;
}
/* handling num_persistent_updates is a bit strange -
there are 3 cases
1) very old clients, which never called CTDB_CONTROL_START_PERSISTENT_UPDATE
@ -597,6 +603,12 @@ int32_t ctdb_control_update_record(struct ctdb_context *ctdb,
return -1;
}
if (ctdb_db->unhealthy_reason) {
DEBUG(DEBUG_ERR,("db(%s) unhealty in ctdb_control_update_record: %s\n",
ctdb_db->db_name, ctdb_db->unhealthy_reason));
return -1;
}
state = talloc(ctdb, struct ctdb_persistent_write_state);
CTDB_NO_MEMORY(ctdb, state);

View File

@ -386,6 +386,12 @@ int32_t ctdb_control_pull_db(struct ctdb_context *ctdb, TDB_DATA indata, TDB_DAT
params.len = offsetof(struct ctdb_marshall_buffer, data);
params.failed = false;
if (ctdb_db->unhealthy_reason) {
/* this is just a warning, as the tdb should be empty anyway */
DEBUG(DEBUG_WARNING,("db(%s) unhealty in ctdb_control_pull_db: %s\n",
ctdb_db->db_name, ctdb_db->unhealthy_reason));
}
if (ctdb_lock_all_databases_mark(ctdb, ctdb_db->priority) != 0) {
DEBUG(DEBUG_ERR,(__location__ " Failed to get lock on entired db - failing\n"));
return -1;

View File

@ -1010,7 +1010,9 @@ static struct tdb_wrap *create_recdb(struct ctdb_context *ctdb, TALLOC_CTX *mem_
unsigned tdb_flags;
/* open up the temporary recovery database */
name = talloc_asprintf(mem_ctx, "%s/recdb.tdb", ctdb->db_directory);
name = talloc_asprintf(mem_ctx, "%s/recdb.tdb.%u",
ctdb->db_directory_state,
ctdb->pnn);
if (name == NULL) {
return NULL;
}
@ -1324,6 +1326,23 @@ static int do_recovery(struct ctdb_recoverd *rec,
return -1;
}
/*
update all nodes to have the same flags that we have
*/
for (i=0;i<nodemap->num;i++) {
if (nodemap->nodes[i].flags & NODE_FLAGS_DISCONNECTED) {
continue;
}
ret = update_flags_on_all_nodes(ctdb, nodemap, i, nodemap->nodes[i].flags);
if (ret != 0) {
DEBUG(DEBUG_ERR, (__location__ " Unable to update flags on all nodes for node %d\n", i));
return -1;
}
}
DEBUG(DEBUG_NOTICE, (__location__ " Recovery - updated flags\n"));
/* pick a new generation number */
generation = new_generation();

View File

@ -102,6 +102,18 @@ int ctdb_set_tdb_dir_persistent(struct ctdb_context *ctdb, const char *dir)
return 0;
}
/*
set the directory for internal state databases
*/
int ctdb_set_tdb_dir_state(struct ctdb_context *ctdb, const char *dir)
{
ctdb->db_directory_state = talloc_strdup(ctdb, dir);
if (ctdb->db_directory_state == NULL) {
return -1;
}
return 0;
}
/*
add a node to the list of nodes
*/

View File

@ -388,6 +388,16 @@ int32_t ctdb_control_traverse_all(struct ctdb_context *ctdb, TDB_DATA data, TDB_
return -1;
}
if (ctdb_db->unhealthy_reason) {
if (ctdb->tunable.allow_unhealthy_db_read == 0) {
DEBUG(DEBUG_ERR,("db(%s) unhealty in ctdb_control_traverse_all: %s\n",
ctdb_db->db_name, ctdb_db->unhealthy_reason));
return -1;
}
DEBUG(DEBUG_WARNING,("warn: db(%s) unhealty in ctdb_control_traverse_all: %s\n",
ctdb_db->db_name, ctdb_db->unhealthy_reason));
}
state = talloc(ctdb_db, struct traverse_all_state);
if (state == NULL) {
return -1;
@ -561,6 +571,16 @@ int32_t ctdb_control_traverse_start(struct ctdb_context *ctdb, TDB_DATA data,
return -1;
}
if (ctdb_db->unhealthy_reason) {
if (ctdb->tunable.allow_unhealthy_db_read == 0) {
DEBUG(DEBUG_ERR,("db(%s) unhealty in ctdb_control_traverse_start: %s\n",
ctdb_db->db_name, ctdb_db->unhealthy_reason));
return -1;
}
DEBUG(DEBUG_WARNING,("warn: db(%s) unhealty in ctdb_control_traverse_start: %s\n",
ctdb_db->db_name, ctdb_db->unhealthy_reason));
}
state = talloc(client, struct traverse_start_state);
if (state == NULL) {
return -1;

View File

@ -63,7 +63,8 @@ static const struct {
{ "VacuumMinInterval", 60, offsetof(struct ctdb_tunable, vacuum_min_interval) },
{ "VacuumMaxInterval", 600, offsetof(struct ctdb_tunable, vacuum_max_interval) },
{ "MaxQueueDropMsg", 1000, offsetof(struct ctdb_tunable, max_queue_depth_drop_msg) },
{ "UseStatusEvents", 0, offsetof(struct ctdb_tunable, use_status_events_for_monitoring) }
{ "UseStatusEvents", 0, offsetof(struct ctdb_tunable, use_status_events_for_monitoring) },
{ "AllowUnhealthyDBRead", 0, offsetof(struct ctdb_tunable, allow_unhealthy_db_read) }
};
/*

View File

@ -518,8 +518,8 @@ static int update_tuning_db(struct ctdb_db_context *ctdb_db, struct vacuum_data
char *vac_dbname;
vac_dbname = talloc_asprintf(tmp_ctx, "%s/%s.%u",
ctdb_db->ctdb->db_directory,
TUNINGDBNAME, ctdb_db->ctdb->pnn);
ctdb_db->ctdb->db_directory_state,
TUNINGDBNAME, ctdb_db->ctdb->pnn);
if (vac_dbname == NULL) {
DEBUG(DEBUG_CRIT,(__location__ " Out of memory error while allocating '%s'\n", vac_dbname));
talloc_free(tmp_ctx);
@ -528,7 +528,7 @@ static int update_tuning_db(struct ctdb_db_context *ctdb_db, struct vacuum_data
tune_tdb = tdb_open(vac_dbname, 0,
TDB_DISALLOW_NESTING,
O_RDWR|O_CREAT, 0644);
O_RDWR|O_CREAT, 0600);
if (tune_tdb == NULL) {
DEBUG(DEBUG_ERR,(__location__ " Failed to create/open %s\n", TUNINGDBNAME));
talloc_free(tmp_ctx);

View File

@ -38,6 +38,7 @@ static struct {
const char *recovery_lock_file;
const char *db_dir;
const char *db_dir_persistent;
const char *db_dir_state;
const char *public_interface;
const char *single_public_ip;
const char *node_ip;
@ -50,6 +51,7 @@ static struct {
int lvs;
int script_log_level;
int no_publicipcheck;
int max_persistent_check_errors;
} options = {
.nlist = ETCDIR "/ctdb/nodes",
.transport = "tcp",
@ -57,6 +59,7 @@ static struct {
.logfile = LOGDIR "/log.ctdb",
.db_dir = VARDIR "/ctdb",
.db_dir_persistent = VARDIR "/ctdb/persistent",
.db_dir_state = VARDIR "/ctdb/state",
.script_log_level = DEBUG_ERR,
};
@ -126,6 +129,7 @@ int main(int argc, const char *argv[])
{ "transport", 0, POPT_ARG_STRING, &options.transport, 0, "protocol transport", NULL },
{ "dbdir", 0, POPT_ARG_STRING, &options.db_dir, 0, "directory for the tdb files", NULL },
{ "dbdir-persistent", 0, POPT_ARG_STRING, &options.db_dir_persistent, 0, "directory for persistent tdb files", NULL },
{ "dbdir-state", 0, POPT_ARG_STRING, &options.db_dir_state, 0, "directory for internal state tdb files", NULL },
{ "reclock", 0, POPT_ARG_STRING, &options.recovery_lock_file, 0, "location of recovery lock file", "filename" },
{ "nosetsched", 0, POPT_ARG_NONE, &options.no_setsched, 0, "disable setscheduler SCHED_FIFO call", NULL },
{ "syslog", 0, POPT_ARG_NONE, &options.use_syslog, 0, "log messages to syslog", NULL },
@ -136,6 +140,9 @@ int main(int argc, const char *argv[])
{ "lvs", 0, POPT_ARG_NONE, &options.lvs, 0, "lvs is enabled on this node", NULL },
{ "script-log-level", 0, POPT_ARG_INT, &options.script_log_level, DEBUG_ERR, "log level of event script output", NULL },
{ "nopublicipcheck", 0, POPT_ARG_NONE, &options.no_publicipcheck, 0, "dont check we have/dont have the correct public ip addresses", NULL },
{ "max-persistent-check-errors", 0, POPT_ARG_INT,
&options.max_persistent_check_errors, 0,
"max allowed persistent check errors (default 0)", NULL },
POPT_TABLEEND
};
int opt, ret;
@ -259,6 +266,13 @@ int main(int argc, const char *argv[])
exit(1);
}
}
if (options.db_dir_state) {
ret = ctdb_set_tdb_dir_state(ctdb, options.db_dir_state);
if (ret == -1) {
DEBUG(DEBUG_ALERT,("ctdb_set_tdb_dir_state failed - %s\n", ctdb_errstr(ctdb)));
exit(1);
}
}
if (options.public_interface) {
ctdb->default_public_interface = talloc_strdup(ctdb, options.public_interface);
@ -315,6 +329,12 @@ int main(int argc, const char *argv[])
ctdb->do_checkpublicip = !options.no_publicipcheck;
if (options.max_persistent_check_errors < 0) {
ctdb->max_persistent_check_errors = 0xFFFFFFFFFFFFFFFFLL;
} else {
ctdb->max_persistent_check_errors = (uint64_t)options.max_persistent_check_errors;
}
if (getenv("CTDB_BASE") == NULL) {
/* setup a environment variable for the event scripts to use
to find the installation directory */

View File

@ -679,7 +679,7 @@ daemons_start_1 ()
echo "Node $no_public_ips will have no public IPs."
fi
local ctdb_options="--reclock=$var_dir/rec.lock --nlist $nodes --nopublicipcheck --event-script-dir=$CTDB_DIR/tests/events.d --logfile=$var_dir/daemons.log -d 0 --dbdir=$var_dir/test.db --dbdir-persistent=$var_dir/test.db/persistent"
local ctdb_options="--reclock=$var_dir/rec.lock --nlist $nodes --nopublicipcheck --event-script-dir=$CTDB_DIR/tests/events.d --logfile=$var_dir/daemons.log -d 0 --dbdir=$var_dir/test.db --dbdir-persistent=$var_dir/test.db/persistent --dbdir-state=$var_dir/test.db/state"
if [ $(id -u) -eq 0 ]; then
ctdb_options="$ctdb_options --public-interface=lo"

View File

@ -2438,6 +2438,9 @@ static int control_catdb(struct ctdb_context *ctdb, int argc, const char **argv)
ret = ctdb_dump_db(ctdb_db, stdout);
if (ret == -1) {
DEBUG(DEBUG_ERR, ("Unable to dump database\n"));
DEBUG(DEBUG_ERR, ("Maybe try 'ctdb getdbstatus %s'"
" and 'ctdb getvar AllowUnhealthyDBRead'\n",
db_name));
return -1;
}
talloc_free(ctdb_db);
@ -2555,22 +2558,94 @@ static int control_getdbmap(struct ctdb_context *ctdb, int argc, const char **ar
return ret;
}
if(options.machinereadable){
printf(":ID:Name:Path:Persistent:Unhealthy:\n");
for(i=0;i<dbmap->num;i++){
const char *path;
const char *name;
const char *health;
bool persistent;
ctdb_ctrl_getdbpath(ctdb, TIMELIMIT(), options.pnn,
dbmap->dbs[i].dbid, ctdb, &path);
ctdb_ctrl_getdbname(ctdb, TIMELIMIT(), options.pnn,
dbmap->dbs[i].dbid, ctdb, &name);
ctdb_ctrl_getdbhealth(ctdb, TIMELIMIT(), options.pnn,
dbmap->dbs[i].dbid, ctdb, &health);
persistent = dbmap->dbs[i].persistent;
printf(":0x%08X:%s:%s:%d:%d:\n",
dbmap->dbs[i].dbid, name, path,
!!(persistent), !!(health));
}
return 0;
}
printf("Number of databases:%d\n", dbmap->num);
for(i=0;i<dbmap->num;i++){
const char *path;
const char *name;
const char *health;
bool persistent;
ctdb_ctrl_getdbpath(ctdb, TIMELIMIT(), options.pnn, dbmap->dbs[i].dbid, ctdb, &path);
ctdb_ctrl_getdbname(ctdb, TIMELIMIT(), options.pnn, dbmap->dbs[i].dbid, ctdb, &name);
ctdb_ctrl_getdbhealth(ctdb, TIMELIMIT(), options.pnn, dbmap->dbs[i].dbid, ctdb, &health);
persistent = dbmap->dbs[i].persistent;
printf("dbid:0x%08x name:%s path:%s %s\n", dbmap->dbs[i].dbid, name,
path, persistent?"PERSISTENT":"");
printf("dbid:0x%08x name:%s path:%s%s%s\n",
dbmap->dbs[i].dbid, name, path,
persistent?" PERSISTENT":"",
health?" UNHEALTHY":"");
}
return 0;
}
/*
display the status of a database on a remote ctdb
*/
static int control_getdbstatus(struct ctdb_context *ctdb, int argc, const char **argv)
{
int i, ret;
struct ctdb_dbid_map *dbmap=NULL;
const char *db_name;
if (argc < 1) {
usage();
}
db_name = argv[0];
ret = ctdb_ctrl_getdbmap(ctdb, TIMELIMIT(), options.pnn, ctdb, &dbmap);
if (ret != 0) {
DEBUG(DEBUG_ERR, ("Unable to get dbids from node %u\n", options.pnn));
return ret;
}
for(i=0;i<dbmap->num;i++){
const char *path;
const char *name;
const char *health;
bool persistent;
ctdb_ctrl_getdbname(ctdb, TIMELIMIT(), options.pnn, dbmap->dbs[i].dbid, ctdb, &name);
if (strcmp(name, db_name) != 0) {
continue;
}
ctdb_ctrl_getdbpath(ctdb, TIMELIMIT(), options.pnn, dbmap->dbs[i].dbid, ctdb, &path);
ctdb_ctrl_getdbhealth(ctdb, TIMELIMIT(), options.pnn, dbmap->dbs[i].dbid, ctdb, &health);
persistent = dbmap->dbs[i].persistent;
printf("dbid: 0x%08x\nname: %s\npath: %s\nPERSISTENT: %s\nHEALTH: %s\n",
dbmap->dbs[i].dbid, name, path,
persistent?"yes":"no",
health?health:"OK");
return 0;
}
DEBUG(DEBUG_ERR, ("db %s doesn't exist on node %u\n", db_name, options.pnn));
return 0;
}
/*
check if the local node is recmaster or not
it will return 1 if this node is the recmaster and 0 if it is not
@ -3104,6 +3179,7 @@ static int control_backupdb(struct ctdb_context *ctdb, int argc, const char **ar
struct backup_data *bd;
int fh = -1;
int status = -1;
const char *reason = NULL;
if (argc != 2) {
DEBUG(DEBUG_ERR,("Invalid arguments\n"));
@ -3132,6 +3208,37 @@ static int control_backupdb(struct ctdb_context *ctdb, int argc, const char **ar
return -1;
}
ret = ctdb_ctrl_getdbhealth(ctdb, TIMELIMIT(), options.pnn,
dbmap->dbs[i].dbid, tmp_ctx, &reason);
if (ret != 0) {
DEBUG(DEBUG_ERR,("Unable to get dbhealth for database '%s'\n",
argv[0]));
talloc_free(tmp_ctx);
return -1;
}
if (reason) {
uint32_t allow_unhealthy = 0;
ctdb_ctrl_get_tunable(ctdb, TIMELIMIT(), options.pnn,
"AllowUnhealthyDBRead",
&allow_unhealthy);
if (allow_unhealthy != 1) {
DEBUG(DEBUG_ERR,("database '%s' is unhealthy: %s\n",
argv[0], reason));
DEBUG(DEBUG_ERR,("disallow backup : tunnable AllowUnhealthyDBRead = %u\n",
allow_unhealthy));
talloc_free(tmp_ctx);
return -1;
}
DEBUG(DEBUG_WARNING,("WARNING database '%s' is unhealthy - see 'ctdb getdbstatus %s'\n",
argv[0], argv[0]));
DEBUG(DEBUG_WARNING,("WARNING! allow backup of unhealthy database: "
"tunnable AllowUnhealthyDBRead = %u\n",
allow_unhealthy));
}
ctdb_db = ctdb_attach(ctdb, argv[0], dbmap->dbs[i].persistent, 0);
if (ctdb_db == NULL) {
@ -3357,6 +3464,22 @@ static int control_restoredb(struct ctdb_context *ctdb, int argc, const char **a
return -1;
}
data.dptr = (void *)&ctdb_db->db_id;
data.dsize = sizeof(ctdb_db->db_id);
/* mark the database as healthy */
nodes = list_of_active_nodes(ctdb, nodemap, tmp_ctx, true);
if (ctdb_client_async_control(ctdb, CTDB_CONTROL_DB_SET_HEALTHY,
nodes, 0,
TIMELIMIT(), false, data,
NULL, NULL,
NULL) != 0) {
DEBUG(DEBUG_ERR, ("Failed to mark database as healthy.\n"));
ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn, CTDB_RECOVERY_ACTIVE);
talloc_free(tmp_ctx);
return -1;
}
data.dptr = (void *)&generation;
data.dsize = sizeof(generation);
@ -3392,6 +3515,72 @@ static int control_restoredb(struct ctdb_context *ctdb, int argc, const char **a
return 0;
}
/*
* dump a database backup from a file
*/
static int control_dumpdbbackup(struct ctdb_context *ctdb, int argc, const char **argv)
{
TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
TDB_DATA outdata;
struct db_file_header dbhdr;
int i, fh;
struct tm *tm;
char tbuf[100];
struct ctdb_rec_data *rec = NULL;
struct ctdb_marshall_buffer *m;
if (argc != 1) {
DEBUG(DEBUG_ERR,("Invalid arguments\n"));
return -1;
}
fh = open(argv[0], O_RDONLY);
if (fh == -1) {
DEBUG(DEBUG_ERR,("Failed to open file '%s'\n", argv[0]));
talloc_free(tmp_ctx);
return -1;
}
read(fh, &dbhdr, sizeof(dbhdr));
if (dbhdr.version != DB_VERSION) {
DEBUG(DEBUG_ERR,("Invalid version of database dump. File is version %lu but expected version was %u\n", dbhdr.version, DB_VERSION));
talloc_free(tmp_ctx);
return -1;
}
outdata.dsize = dbhdr.size;
outdata.dptr = talloc_size(tmp_ctx, outdata.dsize);
if (outdata.dptr == NULL) {
DEBUG(DEBUG_ERR,("Failed to allocate data of size '%lu'\n", dbhdr.size));
close(fh);
talloc_free(tmp_ctx);
return -1;
}
read(fh, outdata.dptr, outdata.dsize);
close(fh);
m = (struct ctdb_marshall_buffer *)outdata.dptr;
tm = localtime(&dbhdr.timestamp);
strftime(tbuf,sizeof(tbuf)-1,"%Y/%m/%d %H:%M:%S", tm);
printf("Backup of database name:'%s' dbid:0x%x08x from @ %s\n",
dbhdr.name, m->db_id, tbuf);
for (i=0; i < m->count; i++) {
uint32_t reqid = 0;
TDB_DATA key, data;
/* we do not want the header splitted, so we pass NULL*/
rec = ctdb_marshall_loop_next(m, rec, &reqid,
NULL, &key, &data);
ctdb_dumpdb_record(ctdb, key, data, stdout);
}
printf("Dumped %d records\n", i);
talloc_free(tmp_ctx);
return 0;
}
/*
* wipe a database from a file
*/
@ -3521,6 +3710,22 @@ static int control_wipedb(struct ctdb_context *ctdb, int argc,
return -1;
}
data.dptr = (void *)&ctdb_db->db_id;
data.dsize = sizeof(ctdb_db->db_id);
/* mark the database as healthy */
nodes = list_of_active_nodes(ctdb, nodemap, tmp_ctx, true);
if (ctdb_client_async_control(ctdb, CTDB_CONTROL_DB_SET_HEALTHY,
nodes, 0,
TIMELIMIT(), false, data,
NULL, NULL,
NULL) != 0) {
DEBUG(DEBUG_ERR, ("Failed to mark database as healthy.\n"));
ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn, CTDB_RECOVERY_ACTIVE);
talloc_free(tmp_ctx);
return -1;
}
data.dptr = (void *)&generation;
data.dsize = sizeof(generation);
@ -3799,6 +4004,7 @@ static const struct {
{ "ip", control_ip, false, false, "show which public ip's that ctdb manages" },
{ "process-exists", control_process_exists, true, false, "check if a process exists on a node", "<pid>"},
{ "getdbmap", control_getdbmap, true, false, "show the database map" },
{ "getdbstatus", control_getdbstatus, true, false, "show the status of a database", "<dbname>" },
{ "catdb", control_catdb, true, false, "dump a database" , "<dbname>"},
{ "getmonmode", control_getmonmode, true, false, "show monitoring mode" },
{ "getcapabilities", control_getcapabilities, true, false, "show node capabilities" },
@ -3847,6 +4053,7 @@ static const struct {
{ "eventscript", control_eventscript, true, false, "run the eventscript with the given parameters on a node", "<arguments>"},
{ "backupdb", control_backupdb, false, false, "backup the database into a file.", "<database> <file>"},
{ "restoredb", control_restoredb, false, false, "restore the database from a file.", "<file>"},
{ "dumpdbbackup", control_dumpdbbackup, false, true, "dump database backup from a file.", "<file>"},
{ "wipedb", control_wipedb, false, false, "wipe the contents of a database.", "<dbname>"},
{ "recmaster", control_recmaster, false, false, "show the pnn for the recovery master."},
{ "setflags", control_setflags, false, false, "set flags for a node in the nodemap.", "<node> <flags>"},