1 | /*
|
---|
2 | Unix SMB/CIFS implementation.
|
---|
3 |
|
---|
4 | trivial database library
|
---|
5 |
|
---|
6 | Copyright (C) Andrew Tridgell 1999-2005
|
---|
7 | Copyright (C) Paul `Rusty' Russell 2000
|
---|
8 | Copyright (C) Jeremy Allison 2000-2003
|
---|
9 |
|
---|
10 | ** NOTE! The following LGPL license applies to the tdb
|
---|
11 | ** library. This does NOT imply that all of Samba is released
|
---|
12 | ** under the LGPL
|
---|
13 |
|
---|
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.
|
---|
18 |
|
---|
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.
|
---|
23 |
|
---|
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/>.
|
---|
26 | */
|
---|
27 |
|
---|
28 | #include "tdb_private.h"
|
---|
29 |
|
---|
30 | #define TDB_NEXT_LOCK_ERR ((tdb_off_t)-1)
|
---|
31 |
|
---|
32 | /* Uses traverse lock: 0 = finish, TDB_NEXT_LOCK_ERR = error,
|
---|
33 | other = record offset */
|
---|
34 | static tdb_off_t tdb_next_lock(struct tdb_context *tdb, struct tdb_traverse_lock *tlock,
|
---|
35 | struct tdb_record *rec)
|
---|
36 | {
|
---|
37 | int want_next = (tlock->off != 0);
|
---|
38 |
|
---|
39 | /* Lock each chain from the start one. */
|
---|
40 | for (; tlock->hash < tdb->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 tdb_brlock(), locking empty hash chains.
|
---|
47 |
|
---|
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
|
---|
54 | value below.
|
---|
55 |
|
---|
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.
|
---|
65 |
|
---|
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).
|
---|
69 | */
|
---|
70 | tdb->methods->next_hash_chain(tdb, &tlock->hash);
|
---|
71 | if (tlock->hash == tdb->hash_size) {
|
---|
72 | continue;
|
---|
73 | }
|
---|
74 | }
|
---|
75 |
|
---|
76 | if (tdb_lock(tdb, tlock->hash, tlock->lock_rw) == -1)
|
---|
77 | return TDB_NEXT_LOCK_ERR;
|
---|
78 |
|
---|
79 | /* No previous record? Start at top of chain. */
|
---|
80 | if (!tlock->off) {
|
---|
81 | if (tdb_ofs_read(tdb, TDB_HASH_TOP(tlock->hash),
|
---|
82 | &tlock->off) == -1)
|
---|
83 | goto fail;
|
---|
84 | } else {
|
---|
85 | /* Otherwise unlock the previous record. */
|
---|
86 | if (tdb_unlock_record(tdb, tlock->off) != 0)
|
---|
87 | goto fail;
|
---|
88 | }
|
---|
89 |
|
---|
90 | if (want_next) {
|
---|
91 | /* We have offset of old record: grab next */
|
---|
92 | if (tdb_rec_read(tdb, tlock->off, rec) == -1)
|
---|
93 | goto fail;
|
---|
94 | tlock->off = rec->next;
|
---|
95 | }
|
---|
96 |
|
---|
97 | /* Iterate through chain */
|
---|
98 | while( tlock->off) {
|
---|
99 | tdb_off_t current;
|
---|
100 | if (tdb_rec_read(tdb, tlock->off, rec) == -1)
|
---|
101 | goto fail;
|
---|
102 |
|
---|
103 | /* Detect infinite loops. From "Shlomi Yaakobovich" <Shlomi@exanet.com>. */
|
---|
104 | if (tlock->off == rec->next) {
|
---|
105 | tdb->ecode = TDB_ERR_CORRUPT;
|
---|
106 | TDB_LOG((tdb, TDB_DEBUG_FATAL, "tdb_next_lock: loop detected.\n"));
|
---|
107 | goto fail;
|
---|
108 | }
|
---|
109 |
|
---|
110 | if (!TDB_DEAD(rec)) {
|
---|
111 | /* Woohoo: we found one! */
|
---|
112 | if (tdb_lock_record(tdb, tlock->off) != 0)
|
---|
113 | goto fail;
|
---|
114 | return tlock->off;
|
---|
115 | }
|
---|
116 |
|
---|
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 | tdb_do_delete(tdb, current, rec) != 0)
|
---|
122 | goto fail;
|
---|
123 | }
|
---|
124 | tdb_unlock(tdb, tlock->hash, tlock->lock_rw);
|
---|
125 | want_next = 0;
|
---|
126 | }
|
---|
127 | /* We finished iteration without finding anything */
|
---|
128 | tdb->ecode = TDB_SUCCESS;
|
---|
129 | return 0;
|
---|
130 |
|
---|
131 | fail:
|
---|
132 | tlock->off = 0;
|
---|
133 | if (tdb_unlock(tdb, tlock->hash, tlock->lock_rw) != 0)
|
---|
134 | TDB_LOG((tdb, TDB_DEBUG_FATAL, "tdb_next_lock: On error unlock failed!\n"));
|
---|
135 | return TDB_NEXT_LOCK_ERR;
|
---|
136 | }
|
---|
137 |
|
---|
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
|
---|
142 | */
|
---|
143 | static int tdb_traverse_internal(struct tdb_context *tdb,
|
---|
144 | tdb_traverse_func fn, void *private_data,
|
---|
145 | struct tdb_traverse_lock *tl)
|
---|
146 | {
|
---|
147 | TDB_DATA key, dbuf;
|
---|
148 | struct tdb_record rec;
|
---|
149 | int ret = 0, count = 0;
|
---|
150 | tdb_off_t off;
|
---|
151 |
|
---|
152 | /* This was in the initialization, above, but the IRIX compiler
|
---|
153 | * did not like it. crh
|
---|
154 | */
|
---|
155 | tl->next = tdb->travlocks.next;
|
---|
156 |
|
---|
157 | /* fcntl locks don't stack: beware traverse inside traverse */
|
---|
158 | tdb->travlocks.next = tl;
|
---|
159 |
|
---|
160 | /* tdb_next_lock places locks on the record returned, and its chain */
|
---|
161 | while ((off = tdb_next_lock(tdb, tl, &rec)) != 0) {
|
---|
162 | if (off == TDB_NEXT_LOCK_ERR) {
|
---|
163 | ret = -1;
|
---|
164 | goto out;
|
---|
165 | }
|
---|
166 | count++;
|
---|
167 | /* now read the full record */
|
---|
168 | key.dptr = tdb_alloc_read(tdb, tl->off + sizeof(rec),
|
---|
169 | rec.key_len + rec.data_len);
|
---|
170 | if (!key.dptr) {
|
---|
171 | ret = -1;
|
---|
172 | if (tdb_unlock(tdb, tl->hash, tl->lock_rw) != 0)
|
---|
173 | goto out;
|
---|
174 | if (tdb_unlock_record(tdb, tl->off) != 0)
|
---|
175 | TDB_LOG((tdb, TDB_DEBUG_FATAL, "tdb_traverse: key.dptr == NULL and unlock_record failed!\n"));
|
---|
176 | goto out;
|
---|
177 | }
|
---|
178 | key.dsize = rec.key_len;
|
---|
179 | dbuf.dptr = key.dptr + rec.key_len;
|
---|
180 | dbuf.dsize = rec.data_len;
|
---|
181 |
|
---|
182 | tdb_trace_1rec_retrec(tdb, "traverse", key, dbuf);
|
---|
183 |
|
---|
184 | /* Drop chain lock, call out */
|
---|
185 | if (tdb_unlock(tdb, tl->hash, tl->lock_rw) != 0) {
|
---|
186 | ret = -1;
|
---|
187 | SAFE_FREE(key.dptr);
|
---|
188 | goto out;
|
---|
189 | }
|
---|
190 | if (fn && fn(tdb, key, dbuf, private_data)) {
|
---|
191 | /* They want us to terminate traversal */
|
---|
192 | tdb_trace_ret(tdb, "tdb_traverse_end", count);
|
---|
193 | if (tdb_unlock_record(tdb, tl->off) != 0) {
|
---|
194 | TDB_LOG((tdb, TDB_DEBUG_FATAL, "tdb_traverse: unlock_record failed!\n"));;
|
---|
195 | ret = -1;
|
---|
196 | }
|
---|
197 | SAFE_FREE(key.dptr);
|
---|
198 | goto out;
|
---|
199 | }
|
---|
200 | SAFE_FREE(key.dptr);
|
---|
201 | }
|
---|
202 | tdb_trace(tdb, "tdb_traverse_end");
|
---|
203 | out:
|
---|
204 | tdb->travlocks.next = tl->next;
|
---|
205 | if (ret < 0)
|
---|
206 | return -1;
|
---|
207 | else
|
---|
208 | return count;
|
---|
209 | }
|
---|
210 |
|
---|
211 |
|
---|
212 | /*
|
---|
213 | a read style traverse - temporarily marks the db read only
|
---|
214 | */
|
---|
215 | _PUBLIC_ int tdb_traverse_read(struct tdb_context *tdb,
|
---|
216 | tdb_traverse_func fn, void *private_data)
|
---|
217 | {
|
---|
218 | struct tdb_traverse_lock tl = { NULL, 0, 0, F_RDLCK };
|
---|
219 | int ret;
|
---|
220 |
|
---|
221 | /* we need to get a read lock on the transaction lock here to
|
---|
222 | cope with the lock ordering semantics of solaris10 */
|
---|
223 | if (tdb_transaction_lock(tdb, F_RDLCK, TDB_LOCK_WAIT)) {
|
---|
224 | return -1;
|
---|
225 | }
|
---|
226 |
|
---|
227 | tdb->traverse_read++;
|
---|
228 | tdb_trace(tdb, "tdb_traverse_read_start");
|
---|
229 | ret = tdb_traverse_internal(tdb, fn, private_data, &tl);
|
---|
230 | tdb->traverse_read--;
|
---|
231 |
|
---|
232 | tdb_transaction_unlock(tdb, F_RDLCK);
|
---|
233 |
|
---|
234 | return ret;
|
---|
235 | }
|
---|
236 |
|
---|
237 | /*
|
---|
238 | a write style traverse - needs to get the transaction lock to
|
---|
239 | prevent deadlocks
|
---|
240 |
|
---|
241 | WARNING: The data buffer given to the callback fn does NOT meet the
|
---|
242 | alignment guarantees malloc gives you.
|
---|
243 | */
|
---|
244 | _PUBLIC_ int tdb_traverse(struct tdb_context *tdb,
|
---|
245 | tdb_traverse_func fn, void *private_data)
|
---|
246 | {
|
---|
247 | struct tdb_traverse_lock tl = { NULL, 0, 0, F_WRLCK };
|
---|
248 | enum tdb_lock_flags lock_flags;
|
---|
249 | int ret;
|
---|
250 |
|
---|
251 | if (tdb->read_only || tdb->traverse_read) {
|
---|
252 | return tdb_traverse_read(tdb, fn, private_data);
|
---|
253 | }
|
---|
254 |
|
---|
255 | lock_flags = TDB_LOCK_WAIT;
|
---|
256 |
|
---|
257 | if (tdb->allrecord_lock.count != 0) {
|
---|
258 | /*
|
---|
259 | * This avoids a deadlock between tdb_lockall() and
|
---|
260 | * tdb_traverse(). See
|
---|
261 | * https://bugzilla.samba.org/show_bug.cgi?id=11381
|
---|
262 | */
|
---|
263 | lock_flags = TDB_LOCK_NOWAIT;
|
---|
264 | }
|
---|
265 |
|
---|
266 | if (tdb_transaction_lock(tdb, F_WRLCK, lock_flags)) {
|
---|
267 | return -1;
|
---|
268 | }
|
---|
269 |
|
---|
270 | tdb->traverse_write++;
|
---|
271 | tdb_trace(tdb, "tdb_traverse_start");
|
---|
272 | ret = tdb_traverse_internal(tdb, fn, private_data, &tl);
|
---|
273 | tdb->traverse_write--;
|
---|
274 |
|
---|
275 | tdb_transaction_unlock(tdb, F_WRLCK);
|
---|
276 |
|
---|
277 | return ret;
|
---|
278 | }
|
---|
279 |
|
---|
280 |
|
---|
281 | /* find the first entry in the database and return its key */
|
---|
282 | _PUBLIC_ TDB_DATA tdb_firstkey(struct tdb_context *tdb)
|
---|
283 | {
|
---|
284 | TDB_DATA key;
|
---|
285 | struct tdb_record rec;
|
---|
286 | tdb_off_t off;
|
---|
287 |
|
---|
288 | /* release any old lock */
|
---|
289 | if (tdb_unlock_record(tdb, tdb->travlocks.off) != 0)
|
---|
290 | return tdb_null;
|
---|
291 | tdb->travlocks.off = tdb->travlocks.hash = 0;
|
---|
292 | tdb->travlocks.lock_rw = F_RDLCK;
|
---|
293 |
|
---|
294 | /* Grab first record: locks chain and returned record. */
|
---|
295 | off = tdb_next_lock(tdb, &tdb->travlocks, &rec);
|
---|
296 | if (off == 0 || off == TDB_NEXT_LOCK_ERR) {
|
---|
297 | tdb_trace_retrec(tdb, "tdb_firstkey", tdb_null);
|
---|
298 | return tdb_null;
|
---|
299 | }
|
---|
300 | /* now read the key */
|
---|
301 | key.dsize = rec.key_len;
|
---|
302 | key.dptr =tdb_alloc_read(tdb,tdb->travlocks.off+sizeof(rec),key.dsize);
|
---|
303 |
|
---|
304 | tdb_trace_retrec(tdb, "tdb_firstkey", key);
|
---|
305 |
|
---|
306 | /* Unlock the hash chain of the record we just read. */
|
---|
307 | if (tdb_unlock(tdb, tdb->travlocks.hash, tdb->travlocks.lock_rw) != 0)
|
---|
308 | TDB_LOG((tdb, TDB_DEBUG_FATAL, "tdb_firstkey: error occurred while tdb_unlocking!\n"));
|
---|
309 | return key;
|
---|
310 | }
|
---|
311 |
|
---|
312 | /* find the next entry in the database, returning its key */
|
---|
313 | _PUBLIC_ TDB_DATA tdb_nextkey(struct tdb_context *tdb, TDB_DATA oldkey)
|
---|
314 | {
|
---|
315 | uint32_t oldhash;
|
---|
316 | TDB_DATA key = tdb_null;
|
---|
317 | struct tdb_record rec;
|
---|
318 | unsigned char *k = NULL;
|
---|
319 | tdb_off_t off;
|
---|
320 |
|
---|
321 | /* Is locked key the old key? If so, traverse will be reliable. */
|
---|
322 | if (tdb->travlocks.off) {
|
---|
323 | if (tdb_lock(tdb,tdb->travlocks.hash,tdb->travlocks.lock_rw))
|
---|
324 | return tdb_null;
|
---|
325 | if (tdb_rec_read(tdb, tdb->travlocks.off, &rec) == -1
|
---|
326 | || !(k = tdb_alloc_read(tdb,tdb->travlocks.off+sizeof(rec),
|
---|
327 | rec.key_len))
|
---|
328 | || memcmp(k, oldkey.dptr, oldkey.dsize) != 0) {
|
---|
329 | /* No, it wasn't: unlock it and start from scratch */
|
---|
330 | if (tdb_unlock_record(tdb, tdb->travlocks.off) != 0) {
|
---|
331 | tdb_trace_1rec_retrec(tdb, "tdb_nextkey",
|
---|
332 | oldkey, tdb_null);
|
---|
333 | SAFE_FREE(k);
|
---|
334 | return tdb_null;
|
---|
335 | }
|
---|
336 | if (tdb_unlock(tdb, tdb->travlocks.hash, tdb->travlocks.lock_rw) != 0) {
|
---|
337 | SAFE_FREE(k);
|
---|
338 | return tdb_null;
|
---|
339 | }
|
---|
340 | tdb->travlocks.off = 0;
|
---|
341 | }
|
---|
342 |
|
---|
343 | SAFE_FREE(k);
|
---|
344 | }
|
---|
345 |
|
---|
346 | if (!tdb->travlocks.off) {
|
---|
347 | /* No previous element: do normal find, and lock record */
|
---|
348 | tdb->travlocks.off = tdb_find_lock_hash(tdb, oldkey, tdb->hash_fn(&oldkey), tdb->travlocks.lock_rw, &rec);
|
---|
349 | if (!tdb->travlocks.off) {
|
---|
350 | tdb_trace_1rec_retrec(tdb, "tdb_nextkey", oldkey, tdb_null);
|
---|
351 | return tdb_null;
|
---|
352 | }
|
---|
353 | tdb->travlocks.hash = BUCKET(rec.full_hash);
|
---|
354 | if (tdb_lock_record(tdb, tdb->travlocks.off) != 0) {
|
---|
355 | TDB_LOG((tdb, TDB_DEBUG_FATAL, "tdb_nextkey: lock_record failed (%s)!\n", strerror(errno)));
|
---|
356 | return tdb_null;
|
---|
357 | }
|
---|
358 | }
|
---|
359 | oldhash = tdb->travlocks.hash;
|
---|
360 |
|
---|
361 | /* Grab next record: locks chain and returned record,
|
---|
362 | unlocks old record */
|
---|
363 | off = tdb_next_lock(tdb, &tdb->travlocks, &rec);
|
---|
364 | if (off != TDB_NEXT_LOCK_ERR && off != 0) {
|
---|
365 | key.dsize = rec.key_len;
|
---|
366 | key.dptr = tdb_alloc_read(tdb, tdb->travlocks.off+sizeof(rec),
|
---|
367 | key.dsize);
|
---|
368 | /* Unlock the chain of this new record */
|
---|
369 | if (tdb_unlock(tdb, tdb->travlocks.hash, tdb->travlocks.lock_rw) != 0)
|
---|
370 | TDB_LOG((tdb, TDB_DEBUG_FATAL, "tdb_nextkey: WARNING tdb_unlock failed!\n"));
|
---|
371 | }
|
---|
372 | /* Unlock the chain of old record */
|
---|
373 | if (tdb_unlock(tdb, BUCKET(oldhash), tdb->travlocks.lock_rw) != 0)
|
---|
374 | TDB_LOG((tdb, TDB_DEBUG_FATAL, "tdb_nextkey: WARNING tdb_unlock failed!\n"));
|
---|
375 | tdb_trace_1rec_retrec(tdb, "tdb_nextkey", oldkey, key);
|
---|
376 | return key;
|
---|
377 | }
|
---|
378 |
|
---|