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 static TDB_DATA tdb1_null
;
34 /* Uses traverse lock: 0 = finish, TDB1_NEXT_LOCK_ERR = error,
35 other = record offset */
36 static tdb1_off_t
tdb1_next_lock(struct tdb_context
*tdb
, struct tdb1_traverse_lock
*tlock
,
37 struct tdb1_record
*rec
)
39 int want_next
= (tlock
->off
!= 0);
41 /* Lock each chain from the start one. */
42 for (; tlock
->hash
< tdb
->tdb1
.header
.hash_size
; tlock
->hash
++) {
43 if (!tlock
->off
&& tlock
->hash
!= 0) {
44 /* this is an optimisation for the common case where
45 the hash chain is empty, which is particularly
46 common for the use of tdb with ldb, where large
47 hashes are used. In that case we spend most of our
48 time in tdb1_brlock(), locking empty hash chains.
50 To avoid this, we do an unlocked pre-check to see
51 if the hash chain is empty before starting to look
52 inside it. If it is empty then we can avoid that
53 hash chain. If it isn't empty then we can't believe
54 the value we get back, as we read it without a
55 lock, so instead we get the lock and re-fetch the
58 Notice that not doing this optimisation on the
59 first hash chain is critical. We must guarantee
60 that we have done at least one fcntl lock at the
61 start of a search to guarantee that memory is
62 coherent on SMP systems. If records are added by
63 others during the search then thats OK, and we
64 could possibly miss those with this trick, but we
65 could miss them anyway without this trick, so the
66 semantics don't change.
68 With a non-indexed ldb search this trick gains us a
69 factor of around 80 in speed on a linux 2.6.x
70 system (testing using ldbtest).
72 tdb
->tdb1
.io
->next_hash_chain(tdb
, &tlock
->hash
);
73 if (tlock
->hash
== tdb
->tdb1
.header
.hash_size
) {
78 if (tdb1_lock(tdb
, tlock
->hash
, tlock
->lock_rw
) == -1)
79 return TDB1_NEXT_LOCK_ERR
;
81 /* No previous record? Start at top of chain. */
83 if (tdb1_ofs_read(tdb
, TDB1_HASH_TOP(tlock
->hash
),
87 /* Otherwise unlock the previous record. */
88 if (tdb1_unlock_record(tdb
, tlock
->off
) != 0)
93 /* We have offset of old record: grab next */
94 if (tdb1_rec_read(tdb
, tlock
->off
, rec
) == -1)
96 tlock
->off
= rec
->next
;
99 /* Iterate through chain */
102 if (tdb1_rec_read(tdb
, tlock
->off
, rec
) == -1)
105 /* Detect infinite loops. From "Shlomi Yaakobovich" <Shlomi@exanet.com>. */
106 if (tlock
->off
== rec
->next
) {
107 tdb
->last_error
= tdb_logerr(tdb
, TDB_ERR_CORRUPT
,
114 if (!TDB1_DEAD(rec
)) {
115 /* Woohoo: we found one! */
116 if (tdb1_lock_record(tdb
, tlock
->off
) != 0)
121 /* Try to clean dead ones from old traverses */
122 current
= tlock
->off
;
123 tlock
->off
= rec
->next
;
124 if (!((tdb
->flags
& TDB_RDONLY
) || tdb
->tdb1
.traverse_read
) &&
125 tdb1_do_delete(tdb
, current
, rec
) != 0)
128 tdb1_unlock(tdb
, tlock
->hash
, tlock
->lock_rw
);
131 /* We finished iteration without finding anything */
132 tdb
->last_error
= TDB_SUCCESS
;
137 if (tdb1_unlock(tdb
, tlock
->hash
, tlock
->lock_rw
) != 0)
138 tdb_logerr(tdb
, tdb
->last_error
, TDB_LOG_ERROR
,
139 "tdb1_next_lock: On error unlock failed!");
140 return TDB1_NEXT_LOCK_ERR
;
143 /* traverse the entire database - calling fn(tdb, key, data) on each element.
144 return -1 on error or the record count traversed
145 if fn is NULL then it is not called
146 a non-zero return value from fn() indicates that the traversal should stop
148 static int tdb1_traverse_internal(struct tdb_context
*tdb
,
149 int (*fn
)(struct tdb_context
*,
150 TDB_DATA
, TDB_DATA
, void *),
152 struct tdb1_traverse_lock
*tl
)
155 struct tdb1_record rec
;
156 int ret
= 0, count
= 0;
159 /* This was in the initializaton, above, but the IRIX compiler
160 * did not like it. crh
162 tl
->next
= tdb
->tdb1
.travlocks
.next
;
164 /* fcntl locks don't stack: beware traverse inside traverse */
165 tdb
->tdb1
.travlocks
.next
= tl
;
167 /* tdb1_next_lock places locks on the record returned, and its chain */
168 while ((off
= tdb1_next_lock(tdb
, tl
, &rec
)) != 0) {
169 if (off
== TDB1_NEXT_LOCK_ERR
) {
174 /* now read the full record */
175 key
.dptr
= tdb1_alloc_read(tdb
, tl
->off
+ sizeof(rec
),
176 rec
.key_len
+ rec
.data_len
);
179 if (tdb1_unlock(tdb
, tl
->hash
, tl
->lock_rw
) != 0)
181 if (tdb1_unlock_record(tdb
, tl
->off
) != 0)
182 tdb_logerr(tdb
, tdb
->last_error
, TDB_LOG_ERROR
,
183 "tdb1_traverse: key.dptr == NULL and"
184 " unlock_record failed!");
187 key
.dsize
= rec
.key_len
;
188 dbuf
.dptr
= key
.dptr
+ rec
.key_len
;
189 dbuf
.dsize
= rec
.data_len
;
191 /* Drop chain lock, call out */
192 if (tdb1_unlock(tdb
, tl
->hash
, tl
->lock_rw
) != 0) {
197 if (fn
&& fn(tdb
, key
, dbuf
, private_data
)) {
198 /* They want us to terminate traversal */
199 if (tdb1_unlock_record(tdb
, tl
->off
) != 0) {
200 tdb_logerr(tdb
, tdb
->last_error
, TDB_LOG_ERROR
,
202 " unlock_record failed!");
211 tdb
->tdb1
.travlocks
.next
= tl
->next
;
220 a read style traverse - only if db read only
222 static int tdb1_traverse_read(struct tdb_context
*tdb
,
223 int (*fn
)(struct tdb_context
*,
224 TDB_DATA
, TDB_DATA
, void *),
227 struct tdb1_traverse_lock tl
= { NULL
, 0, 0, F_RDLCK
};
230 /* we need to get a read lock on the transaction lock here to
231 cope with the lock ordering semantics of solaris10 */
232 if (tdb1_transaction_lock(tdb
, F_RDLCK
, TDB_LOCK_WAIT
)) {
236 tdb
->tdb1
.traverse_read
++;
237 ret
= tdb1_traverse_internal(tdb
, fn
, private_data
, &tl
);
238 tdb
->tdb1
.traverse_read
--;
240 tdb1_transaction_unlock(tdb
, F_RDLCK
);
246 a write style traverse - needs to get the transaction lock to
249 WARNING: The data buffer given to the callback fn does NOT meet the
250 alignment restrictions malloc gives you.
252 int tdb1_traverse(struct tdb_context
*tdb
,
253 int (*fn
)(struct tdb_context
*, TDB_DATA
, TDB_DATA
, void *),
256 struct tdb1_traverse_lock tl
= { NULL
, 0, 0, F_WRLCK
};
259 /* If we're read-only, we don't have to write-lock whole db. */
260 if (tdb
->flags
& TDB_RDONLY
) {
261 return tdb1_traverse_read(tdb
, fn
, private_data
);
264 if (tdb1_transaction_lock(tdb
, F_WRLCK
, TDB_LOCK_WAIT
)) {
268 tdb
->tdb1
.traverse_write
++;
269 ret
= tdb1_traverse_internal(tdb
, fn
, private_data
, &tl
);
270 tdb
->tdb1
.traverse_write
--;
272 tdb1_transaction_unlock(tdb
, F_WRLCK
);
278 /* find the first entry in the database and return its key */
279 TDB_DATA
tdb1_firstkey(struct tdb_context
*tdb
)
282 struct tdb1_record rec
;
285 /* release any old lock */
286 if (tdb1_unlock_record(tdb
, tdb
->tdb1
.travlocks
.off
) != 0)
288 tdb
->tdb1
.travlocks
.off
= tdb
->tdb1
.travlocks
.hash
= 0;
289 tdb
->tdb1
.travlocks
.lock_rw
= F_RDLCK
;
291 /* Grab first record: locks chain and returned record. */
292 off
= tdb1_next_lock(tdb
, &tdb
->tdb1
.travlocks
, &rec
);
293 if (off
== 0 || off
== TDB1_NEXT_LOCK_ERR
) {
296 /* now read the key */
297 key
.dsize
= rec
.key_len
;
298 key
.dptr
=tdb1_alloc_read(tdb
,tdb
->tdb1
.travlocks
.off
+sizeof(rec
),key
.dsize
);
300 /* Unlock the hash chain of the record we just read. */
301 if (tdb1_unlock(tdb
, tdb
->tdb1
.travlocks
.hash
, tdb
->tdb1
.travlocks
.lock_rw
) != 0)
302 tdb_logerr(tdb
, tdb
->last_error
, TDB_LOG_ERROR
,
304 " error occurred while tdb1_unlocking!");
308 /* find the next entry in the database, returning its key */
309 TDB_DATA
tdb1_nextkey(struct tdb_context
*tdb
, TDB_DATA oldkey
)
312 TDB_DATA key
= tdb1_null
;
313 struct tdb1_record rec
;
314 unsigned char *k
= NULL
;
317 /* Is locked key the old key? If so, traverse will be reliable. */
318 if (tdb
->tdb1
.travlocks
.off
) {
319 if (tdb1_lock(tdb
,tdb
->tdb1
.travlocks
.hash
,tdb
->tdb1
.travlocks
.lock_rw
))
321 if (tdb1_rec_read(tdb
, tdb
->tdb1
.travlocks
.off
, &rec
) == -1
322 || !(k
= tdb1_alloc_read(tdb
,tdb
->tdb1
.travlocks
.off
+sizeof(rec
),
324 || memcmp(k
, oldkey
.dptr
, oldkey
.dsize
) != 0) {
325 /* No, it wasn't: unlock it and start from scratch */
326 if (tdb1_unlock_record(tdb
, tdb
->tdb1
.travlocks
.off
) != 0) {
330 if (tdb1_unlock(tdb
, tdb
->tdb1
.travlocks
.hash
, tdb
->tdb1
.travlocks
.lock_rw
) != 0) {
334 tdb
->tdb1
.travlocks
.off
= 0;
340 if (!tdb
->tdb1
.travlocks
.off
) {
341 /* No previous element: do normal find, and lock record */
342 tdb
->tdb1
.travlocks
.off
= tdb1_find_lock_hash(tdb
, oldkey
, tdb_hash(tdb
, oldkey
.dptr
, oldkey
.dsize
), tdb
->tdb1
.travlocks
.lock_rw
, &rec
);
343 if (!tdb
->tdb1
.travlocks
.off
) {
346 tdb
->tdb1
.travlocks
.hash
= TDB1_BUCKET(rec
.full_hash
);
347 if (tdb1_lock_record(tdb
, tdb
->tdb1
.travlocks
.off
) != 0) {
348 tdb_logerr(tdb
, tdb
->last_error
, TDB_LOG_ERROR
,
349 "tdb1_nextkey: lock_record failed (%s)!",
354 oldhash
= tdb
->tdb1
.travlocks
.hash
;
356 /* Grab next record: locks chain and returned record,
357 unlocks old record */
358 off
= tdb1_next_lock(tdb
, &tdb
->tdb1
.travlocks
, &rec
);
359 if (off
!= TDB1_NEXT_LOCK_ERR
&& off
!= 0) {
360 key
.dsize
= rec
.key_len
;
361 key
.dptr
= tdb1_alloc_read(tdb
, tdb
->tdb1
.travlocks
.off
+sizeof(rec
),
363 /* Unlock the chain of this new record */
364 if (tdb1_unlock(tdb
, tdb
->tdb1
.travlocks
.hash
, tdb
->tdb1
.travlocks
.lock_rw
) != 0)
365 tdb_logerr(tdb
, tdb
->last_error
, TDB_LOG_ERROR
,
366 "tdb1_nextkey: WARNING tdb1_unlock failed!");
368 /* Unlock the chain of old record */
369 if (tdb1_unlock(tdb
, TDB1_BUCKET(oldhash
), tdb
->tdb1
.travlocks
.lock_rw
) != 0)
370 tdb_logerr(tdb
, tdb
->last_error
, TDB_LOG_ERROR
,
371 "tdb1_nextkey: WARNING tdb1_unlock failed!");