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 3 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, see <http://www.gnu.org/licenses/>.
28 #include "tdb1_private.h"
30 #define TDB1_NEXT_LOCK_ERR ((tdb1_off_t)-1)
32 /* Uses traverse lock: 0 = finish, TDB1_NEXT_LOCK_ERR = error,
33 other = record offset */
34 static tdb1_off_t tdb1_next_lock(struct tdb1_context *tdb, struct tdb1_traverse_lock *tlock,
35 struct tdb1_record *rec)
37 int want_next = (tlock->off != 0);
39 /* Lock each chain from the start one. */
40 for (; tlock->hash < tdb->header.hash_size; tlock->hash++) {
41 if (!tlock->off && tlock->hash != 0) {
42 /* this is an optimisation for the common case where
43 the hash chain is empty, which is particularly
44 common for the use of tdb with ldb, where large
45 hashes are used. In that case we spend most of our
46 time in tdb1_brlock(), locking empty hash chains.
48 To avoid this, we do an unlocked pre-check to see
49 if the hash chain is empty before starting to look
50 inside it. If it is empty then we can avoid that
51 hash chain. If it isn't empty then we can't believe
52 the value we get back, as we read it without a
53 lock, so instead we get the lock and re-fetch the
56 Notice that not doing this optimisation on the
57 first hash chain is critical. We must guarantee
58 that we have done at least one fcntl lock at the
59 start of a search to guarantee that memory is
60 coherent on SMP systems. If records are added by
61 others during the search then thats OK, and we
62 could possibly miss those with this trick, but we
63 could miss them anyway without this trick, so the
64 semantics don't change.
66 With a non-indexed ldb search this trick gains us a
67 factor of around 80 in speed on a linux 2.6.x
68 system (testing using ldbtest).
70 tdb->methods->next_hash_chain(tdb, &tlock->hash);
71 if (tlock->hash == tdb->header.hash_size) {
76 if (tdb1_lock(tdb, tlock->hash, tlock->lock_rw) == -1)
77 return TDB1_NEXT_LOCK_ERR;
79 /* No previous record? Start at top of chain. */
81 if (tdb1_ofs_read(tdb, TDB1_HASH_TOP(tlock->hash),
85 /* Otherwise unlock the previous record. */
86 if (tdb1_unlock_record(tdb, tlock->off) != 0)
91 /* We have offset of old record: grab next */
92 if (tdb1_rec_read(tdb, tlock->off, rec) == -1)
94 tlock->off = rec->next;
97 /* Iterate through chain */
100 if (tdb1_rec_read(tdb, tlock->off, rec) == -1)
103 /* Detect infinite loops. From "Shlomi Yaakobovich" <Shlomi@exanet.com>. */
104 if (tlock->off == rec->next) {
105 tdb->ecode = TDB1_ERR_CORRUPT;
106 TDB1_LOG((tdb, TDB1_DEBUG_FATAL, "tdb1_next_lock: loop detected.\n"));
110 if (!TDB1_DEAD(rec)) {
111 /* Woohoo: we found one! */
112 if (tdb1_lock_record(tdb, tlock->off) != 0)
117 /* Try to clean dead ones from old traverses */
118 current = tlock->off;
119 tlock->off = rec->next;
120 if (!(tdb->read_only || tdb->traverse_read) &&
121 tdb1_do_delete(tdb, current, rec) != 0)
124 tdb1_unlock(tdb, tlock->hash, tlock->lock_rw);
127 /* We finished iteration without finding anything */
128 tdb->ecode = TDB1_SUCCESS;
133 if (tdb1_unlock(tdb, tlock->hash, tlock->lock_rw) != 0)
134 TDB1_LOG((tdb, TDB1_DEBUG_FATAL, "tdb1_next_lock: On error unlock failed!\n"));
135 return TDB1_NEXT_LOCK_ERR;
138 /* traverse the entire database - calling fn(tdb, key, data) on each element.
139 return -1 on error or the record count traversed
140 if fn is NULL then it is not called
141 a non-zero return value from fn() indicates that the traversal should stop
143 static int tdb1_traverse_internal(struct tdb1_context *tdb,
144 tdb1_traverse_func fn, void *private_data,
145 struct tdb1_traverse_lock *tl)
148 struct tdb1_record rec;
149 int ret = 0, count = 0;
152 /* This was in the initializaton, above, but the IRIX compiler
153 * did not like it. crh
155 tl->next = tdb->travlocks.next;
157 /* fcntl locks don't stack: beware traverse inside traverse */
158 tdb->travlocks.next = tl;
160 /* tdb1_next_lock places locks on the record returned, and its chain */
161 while ((off = tdb1_next_lock(tdb, tl, &rec)) != 0) {
162 if (off == TDB1_NEXT_LOCK_ERR) {
167 /* now read the full record */
168 key.dptr = tdb1_alloc_read(tdb, tl->off + sizeof(rec),
169 rec.key_len + rec.data_len);
172 if (tdb1_unlock(tdb, tl->hash, tl->lock_rw) != 0)
174 if (tdb1_unlock_record(tdb, tl->off) != 0)
175 TDB1_LOG((tdb, TDB1_DEBUG_FATAL, "tdb1_traverse: key.dptr == NULL and unlock_record failed!\n"));
178 key.dsize = rec.key_len;
179 dbuf.dptr = key.dptr + rec.key_len;
180 dbuf.dsize = rec.data_len;
182 /* Drop chain lock, call out */
183 if (tdb1_unlock(tdb, tl->hash, tl->lock_rw) != 0) {
188 if (fn && fn(tdb, key, dbuf, private_data)) {
189 /* They want us to terminate traversal */
190 if (tdb1_unlock_record(tdb, tl->off) != 0) {
191 TDB1_LOG((tdb, TDB1_DEBUG_FATAL, "tdb1_traverse: unlock_record failed!\n"));;
200 tdb->travlocks.next = tl->next;
209 a write style traverse - temporarily marks the db read only
211 _PUBLIC_ int tdb1_traverse_read(struct tdb1_context *tdb,
212 tdb1_traverse_func fn, void *private_data)
214 struct tdb1_traverse_lock tl = { NULL, 0, 0, F_RDLCK };
217 /* we need to get a read lock on the transaction lock here to
218 cope with the lock ordering semantics of solaris10 */
219 if (tdb1_transaction_lock(tdb, F_RDLCK, TDB1_LOCK_WAIT)) {
223 tdb->traverse_read++;
224 ret = tdb1_traverse_internal(tdb, fn, private_data, &tl);
225 tdb->traverse_read--;
227 tdb1_transaction_unlock(tdb, F_RDLCK);
233 a write style traverse - needs to get the transaction lock to
236 WARNING: The data buffer given to the callback fn does NOT meet the
237 alignment restrictions malloc gives you.
239 _PUBLIC_ int tdb1_traverse(struct tdb1_context *tdb,
240 tdb1_traverse_func fn, void *private_data)
242 struct tdb1_traverse_lock tl = { NULL, 0, 0, F_WRLCK };
245 if (tdb->read_only || tdb->traverse_read) {
246 return tdb1_traverse_read(tdb, fn, private_data);
249 if (tdb1_transaction_lock(tdb, F_WRLCK, TDB1_LOCK_WAIT)) {
253 tdb->traverse_write++;
254 ret = tdb1_traverse_internal(tdb, fn, private_data, &tl);
255 tdb->traverse_write--;
257 tdb1_transaction_unlock(tdb, F_WRLCK);
263 /* find the first entry in the database and return its key */
264 _PUBLIC_ TDB1_DATA tdb1_firstkey(struct tdb1_context *tdb)
267 struct tdb1_record rec;
270 /* release any old lock */
271 if (tdb1_unlock_record(tdb, tdb->travlocks.off) != 0)
273 tdb->travlocks.off = tdb->travlocks.hash = 0;
274 tdb->travlocks.lock_rw = F_RDLCK;
276 /* Grab first record: locks chain and returned record. */
277 off = tdb1_next_lock(tdb, &tdb->travlocks, &rec);
278 if (off == 0 || off == TDB1_NEXT_LOCK_ERR) {
281 /* now read the key */
282 key.dsize = rec.key_len;
283 key.dptr =tdb1_alloc_read(tdb,tdb->travlocks.off+sizeof(rec),key.dsize);
285 /* Unlock the hash chain of the record we just read. */
286 if (tdb1_unlock(tdb, tdb->travlocks.hash, tdb->travlocks.lock_rw) != 0)
287 TDB1_LOG((tdb, TDB1_DEBUG_FATAL, "tdb1_firstkey: error occurred while tdb1_unlocking!\n"));
291 /* find the next entry in the database, returning its key */
292 _PUBLIC_ TDB1_DATA tdb1_nextkey(struct tdb1_context *tdb, TDB1_DATA oldkey)
295 TDB1_DATA key = tdb1_null;
296 struct tdb1_record rec;
297 unsigned char *k = NULL;
300 /* Is locked key the old key? If so, traverse will be reliable. */
301 if (tdb->travlocks.off) {
302 if (tdb1_lock(tdb,tdb->travlocks.hash,tdb->travlocks.lock_rw))
304 if (tdb1_rec_read(tdb, tdb->travlocks.off, &rec) == -1
305 || !(k = tdb1_alloc_read(tdb,tdb->travlocks.off+sizeof(rec),
307 || memcmp(k, oldkey.dptr, oldkey.dsize) != 0) {
308 /* No, it wasn't: unlock it and start from scratch */
309 if (tdb1_unlock_record(tdb, tdb->travlocks.off) != 0) {
313 if (tdb1_unlock(tdb, tdb->travlocks.hash, tdb->travlocks.lock_rw) != 0) {
317 tdb->travlocks.off = 0;
323 if (!tdb->travlocks.off) {
324 /* No previous element: do normal find, and lock record */
325 tdb->travlocks.off = tdb1_find_lock_hash(tdb, oldkey, tdb->hash_fn(&oldkey), tdb->travlocks.lock_rw, &rec);
326 if (!tdb->travlocks.off) {
329 tdb->travlocks.hash = TDB1_BUCKET(rec.full_hash);
330 if (tdb1_lock_record(tdb, tdb->travlocks.off) != 0) {
331 TDB1_LOG((tdb, TDB1_DEBUG_FATAL, "tdb1_nextkey: lock_record failed (%s)!\n", strerror(errno)));
335 oldhash = tdb->travlocks.hash;
337 /* Grab next record: locks chain and returned record,
338 unlocks old record */
339 off = tdb1_next_lock(tdb, &tdb->travlocks, &rec);
340 if (off != TDB1_NEXT_LOCK_ERR && off != 0) {
341 key.dsize = rec.key_len;
342 key.dptr = tdb1_alloc_read(tdb, tdb->travlocks.off+sizeof(rec),
344 /* Unlock the chain of this new record */
345 if (tdb1_unlock(tdb, tdb->travlocks.hash, tdb->travlocks.lock_rw) != 0)
346 TDB1_LOG((tdb, TDB1_DEBUG_FATAL, "tdb1_nextkey: WARNING tdb1_unlock failed!\n"));
348 /* Unlock the chain of old record */
349 if (tdb1_unlock(tdb, TDB1_BUCKET(oldhash), tdb->travlocks.lock_rw) != 0)
350 TDB1_LOG((tdb, TDB1_DEBUG_FATAL, "tdb1_nextkey: WARNING tdb1_unlock failed!\n"));