2 Unix SMB/CIFS implementation.
4 trivial database library
6 Copyright (C) Andrew Tridgell 1999-2005
7 Copyright (C) Paul `Rusty' Russell 2000
8 Copyright (C) Jeremy Allison 2000-2003
10 ** NOTE! The following LGPL license applies to the tdb
11 ** library. This does NOT imply that all of Samba is released
14 This library is free software; you can redistribute it and/or
15 modify it under the terms of the GNU Lesser General Public
16 License as published by the Free Software Foundation; either
17 version 2 of the License, or (at your option) any later version.
19 This library is distributed in the hope that it will be useful,
20 but WITHOUT ANY WARRANTY; without even the implied warranty of
21 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
22 Lesser General Public License for more details.
24 You should have received a copy of the GNU Lesser General Public
25 License along with this library; if not, write to the Free Software
26 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
29 #include "tdb_private.h"
31 /* Uses traverse lock: 0 = finish, -1 = error, other = record offset */
32 static int tdb_next_lock(struct tdb_context *tdb, struct tdb_traverse_lock *tlock,
33 struct list_struct *rec)
35 int want_next = (tlock->off != 0);
37 /* Lock each chain from the start one. */
38 for (; tlock->hash < tdb->header.hash_size; tlock->hash++) {
39 if (!tlock->off && tlock->hash != 0) {
40 /* this is an optimisation for the common case where
41 the hash chain is empty, which is particularly
42 common for the use of tdb with ldb, where large
43 hashes are used. In that case we spend most of our
44 time in tdb_brlock(), locking empty hash chains.
46 To avoid this, we do an unlocked pre-check to see
47 if the hash chain is empty before starting to look
48 inside it. If it is empty then we can avoid that
49 hash chain. If it isn't empty then we can't believe
50 the value we get back, as we read it without a
51 lock, so instead we get the lock and re-fetch the
54 Notice that not doing this optimisation on the
55 first hash chain is critical. We must guarantee
56 that we have done at least one fcntl lock at the
57 start of a search to guarantee that memory is
58 coherent on SMP systems. If records are added by
59 others during the search then thats OK, and we
60 could possibly miss those with this trick, but we
61 could miss them anyway without this trick, so the
62 semantics don't change.
64 With a non-indexed ldb search this trick gains us a
65 factor of around 80 in speed on a linux 2.6.x
66 system (testing using ldbtest).
68 tdb->methods->next_hash_chain(tdb, &tlock->hash);
69 if (tlock->hash == tdb->header.hash_size) {
74 if (tdb_lock(tdb, tlock->hash, F_WRLCK) == -1)
77 /* No previous record? Start at top of chain. */
79 if (tdb_ofs_read(tdb, TDB_HASH_TOP(tlock->hash),
83 /* Otherwise unlock the previous record. */
84 if (tdb_unlock_record(tdb, tlock->off) != 0)
89 /* We have offset of old record: grab next */
90 if (tdb_rec_read(tdb, tlock->off, rec) == -1)
92 tlock->off = rec->next;
95 /* Iterate through chain */
98 if (tdb_rec_read(tdb, tlock->off, rec) == -1)
101 /* Detect infinite loops. From "Shlomi Yaakobovich" <Shlomi@exanet.com>. */
102 if (tlock->off == rec->next) {
103 TDB_LOG((tdb, 0, "tdb_next_lock: loop detected.\n"));
107 if (!TDB_DEAD(rec)) {
108 /* Woohoo: we found one! */
109 if (tdb_lock_record(tdb, tlock->off) != 0)
114 /* Try to clean dead ones from old traverses */
115 current = tlock->off;
116 tlock->off = rec->next;
117 if (!tdb->read_only &&
118 tdb_do_delete(tdb, current, rec) != 0)
121 tdb_unlock(tdb, tlock->hash, F_WRLCK);
124 /* We finished iteration without finding anything */
125 return TDB_ERRCODE(TDB_SUCCESS, 0);
129 if (tdb_unlock(tdb, tlock->hash, F_WRLCK) != 0)
130 TDB_LOG((tdb, 0, "tdb_next_lock: On error unlock failed!\n"));
134 /* traverse the entire database - calling fn(tdb, key, data) on each element.
135 return -1 on error or the record count traversed
136 if fn is NULL then it is not called
137 a non-zero return value from fn() indicates that the traversal should stop
139 int tdb_traverse(struct tdb_context *tdb, tdb_traverse_func fn, void *private)
142 struct list_struct rec;
143 struct tdb_traverse_lock tl = { NULL, 0, 0 };
146 /* This was in the initializaton, above, but the IRIX compiler
147 * did not like it. crh
149 tl.next = tdb->travlocks.next;
151 /* fcntl locks don't stack: beware traverse inside traverse */
152 tdb->travlocks.next = &tl;
154 /* tdb_next_lock places locks on the record returned, and its chain */
155 while ((ret = tdb_next_lock(tdb, &tl, &rec)) > 0) {
157 /* now read the full record */
158 key.dptr = tdb_alloc_read(tdb, tl.off + sizeof(rec),
159 rec.key_len + rec.data_len);
162 if (tdb_unlock(tdb, tl.hash, F_WRLCK) != 0)
164 if (tdb_unlock_record(tdb, tl.off) != 0)
165 TDB_LOG((tdb, 0, "tdb_traverse: key.dptr == NULL and unlock_record failed!\n"));
168 key.dsize = rec.key_len;
169 dbuf.dptr = key.dptr + rec.key_len;
170 dbuf.dsize = rec.data_len;
172 /* Drop chain lock, call out */
173 if (tdb_unlock(tdb, tl.hash, F_WRLCK) != 0) {
177 if (fn && fn(tdb, key, dbuf, private)) {
178 /* They want us to terminate traversal */
180 if (tdb_unlock_record(tdb, tl.off) != 0) {
181 TDB_LOG((tdb, 0, "tdb_traverse: unlock_record failed!\n"));;
184 tdb->travlocks.next = tl.next;
191 tdb->travlocks.next = tl.next;
198 /* find the first entry in the database and return its key */
199 TDB_DATA tdb_firstkey(struct tdb_context *tdb)
202 struct list_struct rec;
204 /* release any old lock */
205 if (tdb_unlock_record(tdb, tdb->travlocks.off) != 0)
207 tdb->travlocks.off = tdb->travlocks.hash = 0;
209 if (tdb_next_lock(tdb, &tdb->travlocks, &rec) <= 0)
211 /* now read the key */
212 key.dsize = rec.key_len;
213 key.dptr =tdb_alloc_read(tdb,tdb->travlocks.off+sizeof(rec),key.dsize);
214 if (tdb_unlock(tdb, BUCKET(tdb->travlocks.hash), F_WRLCK) != 0)
215 TDB_LOG((tdb, 0, "tdb_firstkey: error occurred while tdb_unlocking!\n"));
219 /* find the next entry in the database, returning its key */
220 TDB_DATA tdb_nextkey(struct tdb_context *tdb, TDB_DATA oldkey)
223 TDB_DATA key = tdb_null;
224 struct list_struct rec;
225 unsigned char *k = NULL;
227 /* Is locked key the old key? If so, traverse will be reliable. */
228 if (tdb->travlocks.off) {
229 if (tdb_lock(tdb,tdb->travlocks.hash,F_WRLCK))
231 if (tdb_rec_read(tdb, tdb->travlocks.off, &rec) == -1
232 || !(k = tdb_alloc_read(tdb,tdb->travlocks.off+sizeof(rec),
234 || memcmp(k, oldkey.dptr, oldkey.dsize) != 0) {
235 /* No, it wasn't: unlock it and start from scratch */
236 if (tdb_unlock_record(tdb, tdb->travlocks.off) != 0)
238 if (tdb_unlock(tdb, tdb->travlocks.hash, F_WRLCK) != 0)
240 tdb->travlocks.off = 0;
246 if (!tdb->travlocks.off) {
247 /* No previous element: do normal find, and lock record */
248 tdb->travlocks.off = tdb_find_lock_hash(tdb, oldkey, tdb->hash_fn(&oldkey), F_WRLCK, &rec);
249 if (!tdb->travlocks.off)
251 tdb->travlocks.hash = BUCKET(rec.full_hash);
252 if (tdb_lock_record(tdb, tdb->travlocks.off) != 0) {
253 TDB_LOG((tdb, 0, "tdb_nextkey: lock_record failed (%s)!\n", strerror(errno)));
257 oldhash = tdb->travlocks.hash;
259 /* Grab next record: locks chain and returned record,
260 unlocks old record */
261 if (tdb_next_lock(tdb, &tdb->travlocks, &rec) > 0) {
262 key.dsize = rec.key_len;
263 key.dptr = tdb_alloc_read(tdb, tdb->travlocks.off+sizeof(rec),
265 /* Unlock the chain of this new record */
266 if (tdb_unlock(tdb, tdb->travlocks.hash, F_WRLCK) != 0)
267 TDB_LOG((tdb, 0, "tdb_nextkey: WARNING tdb_unlock failed!\n"));
269 /* Unlock the chain of old record */
270 if (tdb_unlock(tdb, BUCKET(oldhash), F_WRLCK) != 0)
271 TDB_LOG((tdb, 0, "tdb_nextkey: WARNING tdb_unlock failed!\n"));