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 "tdb_private.h"
30 /* Uses traverse lock: 0 = finish, -1 = error, other = record offset */
31 static int tdb_next_lock(struct tdb_context *tdb, struct tdb_traverse_lock *tlock,
32 struct list_struct *rec)
34 int want_next = (tlock->off != 0);
36 /* Lock each chain from the start one. */
37 for (; tlock->hash < tdb->header.hash_size; tlock->hash++) {
38 if (!tlock->off && tlock->hash != 0) {
39 /* this is an optimisation for the common case where
40 the hash chain is empty, which is particularly
41 common for the use of tdb with ldb, where large
42 hashes are used. In that case we spend most of our
43 time in tdb_brlock(), locking empty hash chains.
45 To avoid this, we do an unlocked pre-check to see
46 if the hash chain is empty before starting to look
47 inside it. If it is empty then we can avoid that
48 hash chain. If it isn't empty then we can't believe
49 the value we get back, as we read it without a
50 lock, so instead we get the lock and re-fetch the
53 Notice that not doing this optimisation on the
54 first hash chain is critical. We must guarantee
55 that we have done at least one fcntl lock at the
56 start of a search to guarantee that memory is
57 coherent on SMP systems. If records are added by
58 others during the search then thats OK, and we
59 could possibly miss those with this trick, but we
60 could miss them anyway without this trick, so the
61 semantics don't change.
63 With a non-indexed ldb search this trick gains us a
64 factor of around 80 in speed on a linux 2.6.x
65 system (testing using ldbtest).
67 tdb->methods->next_hash_chain(tdb, &tlock->hash);
68 if (tlock->hash == tdb->header.hash_size) {
73 if (tdb_lock(tdb, tlock->hash, tlock->lock_rw) == -1)
76 /* No previous record? Start at top of chain. */
78 if (tdb_ofs_read(tdb, TDB_HASH_TOP(tlock->hash),
82 /* Otherwise unlock the previous record. */
83 if (tdb_unlock_record(tdb, tlock->off) != 0)
88 /* We have offset of old record: grab next */
89 if (tdb_rec_read(tdb, tlock->off, rec) == -1)
91 tlock->off = rec->next;
94 /* Iterate through chain */
97 if (tdb_rec_read(tdb, tlock->off, rec) == -1)
100 /* Detect infinite loops. From "Shlomi Yaakobovich" <Shlomi@exanet.com>. */
101 if (tlock->off == rec->next) {
102 TDB_LOG((tdb, TDB_DEBUG_FATAL, "tdb_next_lock: loop detected.\n"));
106 if (!TDB_DEAD(rec)) {
107 /* Woohoo: we found one! */
108 if (tdb_lock_record(tdb, tlock->off) != 0)
113 /* Try to clean dead ones from old traverses */
114 current = tlock->off;
115 tlock->off = rec->next;
116 if (!(tdb->read_only || tdb->traverse_read) &&
117 tdb_do_delete(tdb, current, rec) != 0)
120 tdb_unlock(tdb, tlock->hash, tlock->lock_rw);
123 /* We finished iteration without finding anything */
124 tdb->ecode = TDB_SUCCESS;
129 if (tdb_unlock(tdb, tlock->hash, tlock->lock_rw) != 0)
130 TDB_LOG((tdb, TDB_DEBUG_FATAL, "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 static int tdb_traverse_internal(struct tdb_context *tdb,
140 tdb_traverse_func fn, void *private_data,
141 struct tdb_traverse_lock *tl)
144 struct list_struct rec;
147 /* This was in the initializaton, above, but the IRIX compiler
148 * did not like it. crh
150 tl->next = tdb->travlocks.next;
152 /* fcntl locks don't stack: beware traverse inside traverse */
153 tdb->travlocks.next = tl;
155 /* tdb_next_lock places locks on the record returned, and its chain */
156 while ((ret = tdb_next_lock(tdb, tl, &rec)) > 0) {
158 /* now read the full record */
159 key.dptr = tdb_alloc_read(tdb, tl->off + sizeof(rec),
160 rec.key_len + rec.data_len);
163 if (tdb_unlock(tdb, tl->hash, tl->lock_rw) != 0)
165 if (tdb_unlock_record(tdb, tl->off) != 0)
166 TDB_LOG((tdb, TDB_DEBUG_FATAL, "tdb_traverse: key.dptr == NULL and unlock_record failed!\n"));
169 key.dsize = rec.key_len;
170 dbuf.dptr = key.dptr + rec.key_len;
171 dbuf.dsize = rec.data_len;
173 tdb_trace_1rec_retrec(tdb, "traverse", key, dbuf);
175 /* Drop chain lock, call out */
176 if (tdb_unlock(tdb, tl->hash, tl->lock_rw) != 0) {
181 if (fn && fn(tdb, key, dbuf, private_data)) {
182 /* They want us to terminate traversal */
183 tdb_trace_ret(tdb, "tdb_traverse_end", count);
185 if (tdb_unlock_record(tdb, tl->off) != 0) {
186 TDB_LOG((tdb, TDB_DEBUG_FATAL, "tdb_traverse: unlock_record failed!\n"));;
194 tdb_trace(tdb, "tdb_traverse_end");
196 tdb->travlocks.next = tl->next;
205 a write style traverse - temporarily marks the db read only
207 int tdb_traverse_read(struct tdb_context *tdb,
208 tdb_traverse_func fn, void *private_data)
210 struct tdb_traverse_lock tl = { NULL, 0, 0, F_RDLCK };
213 /* we need to get a read lock on the transaction lock here to
214 cope with the lock ordering semantics of solaris10 */
215 if (tdb_transaction_lock(tdb, F_RDLCK)) {
219 tdb->traverse_read++;
220 tdb_trace(tdb, "tdb_traverse_read_start");
221 ret = tdb_traverse_internal(tdb, fn, private_data, &tl);
222 tdb->traverse_read--;
224 tdb_transaction_unlock(tdb);
230 a write style traverse - needs to get the transaction lock to
233 WARNING: The data buffer given to the callback fn does NOT meet the
234 alignment restrictions malloc gives you.
236 int tdb_traverse(struct tdb_context *tdb,
237 tdb_traverse_func fn, void *private_data)
239 struct tdb_traverse_lock tl = { NULL, 0, 0, F_WRLCK };
242 if (tdb->read_only || tdb->traverse_read) {
243 return tdb_traverse_read(tdb, fn, private_data);
246 if (tdb_transaction_lock(tdb, F_WRLCK)) {
250 tdb->traverse_write++;
251 tdb_trace(tdb, "tdb_traverse_start");
252 ret = tdb_traverse_internal(tdb, fn, private_data, &tl);
253 tdb->traverse_write--;
255 tdb_transaction_unlock(tdb);
261 /* find the first entry in the database and return its key */
262 TDB_DATA tdb_firstkey(struct tdb_context *tdb)
265 struct list_struct rec;
267 /* release any old lock */
268 if (tdb_unlock_record(tdb, tdb->travlocks.off) != 0)
270 tdb->travlocks.off = tdb->travlocks.hash = 0;
271 tdb->travlocks.lock_rw = F_RDLCK;
273 /* Grab first record: locks chain and returned record. */
274 if (tdb_next_lock(tdb, &tdb->travlocks, &rec) <= 0) {
275 tdb_trace_retrec(tdb, "tdb_firstkey", tdb_null);
278 /* now read the key */
279 key.dsize = rec.key_len;
280 key.dptr =tdb_alloc_read(tdb,tdb->travlocks.off+sizeof(rec),key.dsize);
282 tdb_trace_retrec(tdb, "tdb_firstkey", key);
284 /* Unlock the hash chain of the record we just read. */
285 if (tdb_unlock(tdb, tdb->travlocks.hash, tdb->travlocks.lock_rw) != 0)
286 TDB_LOG((tdb, TDB_DEBUG_FATAL, "tdb_firstkey: error occurred while tdb_unlocking!\n"));
290 /* find the next entry in the database, returning its key */
291 TDB_DATA tdb_nextkey(struct tdb_context *tdb, TDB_DATA oldkey)
294 TDB_DATA key = tdb_null;
295 struct list_struct rec;
296 unsigned char *k = NULL;
298 /* Is locked key the old key? If so, traverse will be reliable. */
299 if (tdb->travlocks.off) {
300 if (tdb_lock(tdb,tdb->travlocks.hash,tdb->travlocks.lock_rw))
302 if (tdb_rec_read(tdb, tdb->travlocks.off, &rec) == -1
303 || !(k = tdb_alloc_read(tdb,tdb->travlocks.off+sizeof(rec),
305 || memcmp(k, oldkey.dptr, oldkey.dsize) != 0) {
306 /* No, it wasn't: unlock it and start from scratch */
307 if (tdb_unlock_record(tdb, tdb->travlocks.off) != 0) {
308 tdb_trace_1rec_retrec(tdb, "tdb_nextkey",
313 if (tdb_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 = tdb_find_lock_hash(tdb, oldkey, tdb->hash_fn(&oldkey), tdb->travlocks.lock_rw, &rec);
326 if (!tdb->travlocks.off) {
327 tdb_trace_1rec_retrec(tdb, "tdb_nextkey", oldkey, tdb_null);
330 tdb->travlocks.hash = BUCKET(rec.full_hash);
331 if (tdb_lock_record(tdb, tdb->travlocks.off) != 0) {
332 TDB_LOG((tdb, TDB_DEBUG_FATAL, "tdb_nextkey: lock_record failed (%s)!\n", strerror(errno)));
336 oldhash = tdb->travlocks.hash;
338 /* Grab next record: locks chain and returned record,
339 unlocks old record */
340 if (tdb_next_lock(tdb, &tdb->travlocks, &rec) > 0) {
341 key.dsize = rec.key_len;
342 key.dptr = tdb_alloc_read(tdb, tdb->travlocks.off+sizeof(rec),
344 /* Unlock the chain of this new record */
345 if (tdb_unlock(tdb, tdb->travlocks.hash, tdb->travlocks.lock_rw) != 0)
346 TDB_LOG((tdb, TDB_DEBUG_FATAL, "tdb_nextkey: WARNING tdb_unlock failed!\n"));
348 /* Unlock the chain of old record */
349 if (tdb_unlock(tdb, BUCKET(oldhash), tdb->travlocks.lock_rw) != 0)
350 TDB_LOG((tdb, TDB_DEBUG_FATAL, "tdb_nextkey: WARNING tdb_unlock failed!\n"));
351 tdb_trace_1rec_retrec(tdb, "tdb_nextkey", oldkey, key);