63052854d5428aef78b589667f5c51d3fc3ade3f
[ccan] / ccan / tdb2 / tdb1_traverse.c
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 "tdb1_private.h"
29
30 #define TDB1_NEXT_LOCK_ERR ((tdb1_off_t)-1)
31
32 /* Uses traverse lock: 0 = finish, TDB1_NEXT_LOCK_ERR = error,
33    other = record offset */
34 static tdb1_off_t tdb1_next_lock(struct tdb_context *tdb, struct tdb1_traverse_lock *tlock,
35                          struct tdb1_record *rec)
36 {
37         int want_next = (tlock->off != 0);
38
39         /* Lock each chain from the start one. */
40         for (; tlock->hash < tdb->tdb1.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.
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->tdb1.io->next_hash_chain(tdb, &tlock->hash);
71                         if (tlock->hash == tdb->tdb1.header.hash_size) {
72                                 continue;
73                         }
74                 }
75
76                 if (tdb1_lock(tdb, tlock->hash, tlock->lock_rw) == -1)
77                         return TDB1_NEXT_LOCK_ERR;
78
79                 /* No previous record?  Start at top of chain. */
80                 if (!tlock->off) {
81                         if (tdb1_ofs_read(tdb, TDB1_HASH_TOP(tlock->hash),
82                                      &tlock->off) == -1)
83                                 goto fail;
84                 } else {
85                         /* Otherwise unlock the previous record. */
86                         if (tdb1_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 (tdb1_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                         tdb1_off_t current;
100                         if (tdb1_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->last_error = tdb_logerr(tdb, TDB_ERR_CORRUPT,
106                                                         TDB_LOG_ERROR,
107                                                         "tdb1_next_lock:"
108                                                         " loop detected.");
109                                 goto fail;
110                         }
111
112                         if (!TDB1_DEAD(rec)) {
113                                 /* Woohoo: we found one! */
114                                 if (tdb1_lock_record(tdb, tlock->off) != 0)
115                                         goto fail;
116                                 return tlock->off;
117                         }
118
119                         /* Try to clean dead ones from old traverses */
120                         current = tlock->off;
121                         tlock->off = rec->next;
122                         if (!((tdb->flags & TDB_RDONLY) || tdb->tdb1.traverse_read) &&
123                             tdb1_do_delete(tdb, current, rec) != 0)
124                                 goto fail;
125                 }
126                 tdb1_unlock(tdb, tlock->hash, tlock->lock_rw);
127                 want_next = 0;
128         }
129         /* We finished iteration without finding anything */
130         tdb->last_error = TDB_SUCCESS;
131         return 0;
132
133  fail:
134         tlock->off = 0;
135         if (tdb1_unlock(tdb, tlock->hash, tlock->lock_rw) != 0)
136                 tdb_logerr(tdb, tdb->last_error, TDB_LOG_ERROR,
137                            "tdb1_next_lock: On error unlock failed!");
138         return TDB1_NEXT_LOCK_ERR;
139 }
140
141 /* traverse the entire database - calling fn(tdb, key, data) on each element.
142    return -1 on error or the record count traversed
143    if fn is NULL then it is not called
144    a non-zero return value from fn() indicates that the traversal should stop
145   */
146 static int tdb1_traverse_internal(struct tdb_context *tdb,
147                                   int (*fn)(struct tdb_context *,
148                                             TDB_DATA, TDB_DATA, void *),
149                                   void *private_data,
150                                   struct tdb1_traverse_lock *tl)
151 {
152         TDB_DATA key, dbuf;
153         struct tdb1_record rec;
154         int ret = 0, count = 0;
155         tdb1_off_t off;
156
157         /* This was in the initializaton, above, but the IRIX compiler
158          * did not like it.  crh
159          */
160         tl->next = tdb->tdb1.travlocks.next;
161
162         /* fcntl locks don't stack: beware traverse inside traverse */
163         tdb->tdb1.travlocks.next = tl;
164
165         /* tdb1_next_lock places locks on the record returned, and its chain */
166         while ((off = tdb1_next_lock(tdb, tl, &rec)) != 0) {
167                 if (off == TDB1_NEXT_LOCK_ERR) {
168                         ret = -1;
169                         goto out;
170                 }
171                 count++;
172                 /* now read the full record */
173                 key.dptr = tdb1_alloc_read(tdb, tl->off + sizeof(rec),
174                                           rec.key_len + rec.data_len);
175                 if (!key.dptr) {
176                         ret = -1;
177                         if (tdb1_unlock(tdb, tl->hash, tl->lock_rw) != 0)
178                                 goto out;
179                         if (tdb1_unlock_record(tdb, tl->off) != 0)
180                                 tdb_logerr(tdb, tdb->last_error, TDB_LOG_ERROR,
181                                            "tdb1_traverse: key.dptr == NULL and"
182                                            " unlock_record failed!");
183                         goto out;
184                 }
185                 key.dsize = rec.key_len;
186                 dbuf.dptr = key.dptr + rec.key_len;
187                 dbuf.dsize = rec.data_len;
188
189                 /* Drop chain lock, call out */
190                 if (tdb1_unlock(tdb, tl->hash, tl->lock_rw) != 0) {
191                         ret = -1;
192                         SAFE_FREE(key.dptr);
193                         goto out;
194                 }
195                 if (fn && fn(tdb, key, dbuf, private_data)) {
196                         /* They want us to terminate traversal */
197                         if (tdb1_unlock_record(tdb, tl->off) != 0) {
198                                 tdb_logerr(tdb, tdb->last_error, TDB_LOG_ERROR,
199                                            "tdb1_traverse:"
200                                            " unlock_record failed!");
201                                 ret = -1;
202                         }
203                         SAFE_FREE(key.dptr);
204                         goto out;
205                 }
206                 SAFE_FREE(key.dptr);
207         }
208 out:
209         tdb->tdb1.travlocks.next = tl->next;
210         if (ret < 0)
211                 return -1;
212         else
213                 return count;
214 }
215
216
217 /*
218   a read style traverse - only if db read only
219 */
220 static int tdb1_traverse_read(struct tdb_context *tdb,
221                               int (*fn)(struct tdb_context *,
222                                         TDB_DATA, TDB_DATA, void *),
223                               void *private_data)
224 {
225         struct tdb1_traverse_lock tl = { NULL, 0, 0, F_RDLCK };
226         int ret;
227
228         /* we need to get a read lock on the transaction lock here to
229            cope with the lock ordering semantics of solaris10 */
230         if (tdb1_transaction_lock(tdb, F_RDLCK, TDB_LOCK_WAIT)) {
231                 return -1;
232         }
233
234         tdb->tdb1.traverse_read++;
235         ret = tdb1_traverse_internal(tdb, fn, private_data, &tl);
236         tdb->tdb1.traverse_read--;
237
238         tdb1_transaction_unlock(tdb, F_RDLCK);
239
240         return ret;
241 }
242
243 /*
244   a write style traverse - needs to get the transaction lock to
245   prevent deadlocks
246
247   WARNING: The data buffer given to the callback fn does NOT meet the
248   alignment restrictions malloc gives you.
249 */
250 int tdb1_traverse(struct tdb_context *tdb,
251                   int (*fn)(struct tdb_context *, TDB_DATA, TDB_DATA, void *),
252                   void *private_data)
253 {
254         struct tdb1_traverse_lock tl = { NULL, 0, 0, F_WRLCK };
255         int ret;
256
257         /* If we're read-only, we don't have to write-lock whole db. */
258         if (tdb->flags & TDB_RDONLY) {
259                 return tdb1_traverse_read(tdb, fn, private_data);
260         }
261
262         if (tdb1_transaction_lock(tdb, F_WRLCK, TDB_LOCK_WAIT)) {
263                 return -1;
264         }
265
266         tdb->tdb1.traverse_write++;
267         ret = tdb1_traverse_internal(tdb, fn, private_data, &tl);
268         tdb->tdb1.traverse_write--;
269
270         tdb1_transaction_unlock(tdb, F_WRLCK);
271
272         return ret;
273 }
274
275
276 /* find the first entry in the database and return its key */
277 TDB_DATA tdb1_firstkey(struct tdb_context *tdb)
278 {
279         TDB_DATA key;
280         struct tdb1_record rec;
281         tdb1_off_t off;
282
283         /* release any old lock */
284         if (tdb1_unlock_record(tdb, tdb->tdb1.travlocks.off) != 0)
285                 return tdb1_null;
286         tdb->tdb1.travlocks.off = tdb->tdb1.travlocks.hash = 0;
287         tdb->tdb1.travlocks.lock_rw = F_RDLCK;
288
289         /* Grab first record: locks chain and returned record. */
290         off = tdb1_next_lock(tdb, &tdb->tdb1.travlocks, &rec);
291         if (off == 0 || off == TDB1_NEXT_LOCK_ERR) {
292                 return tdb1_null;
293         }
294         /* now read the key */
295         key.dsize = rec.key_len;
296         key.dptr =tdb1_alloc_read(tdb,tdb->tdb1.travlocks.off+sizeof(rec),key.dsize);
297
298         /* Unlock the hash chain of the record we just read. */
299         if (tdb1_unlock(tdb, tdb->tdb1.travlocks.hash, tdb->tdb1.travlocks.lock_rw) != 0)
300                 tdb_logerr(tdb, tdb->last_error, TDB_LOG_ERROR,
301                            "tdb1_firstkey:"
302                            " error occurred while tdb1_unlocking!");
303         return key;
304 }
305
306 /* find the next entry in the database, returning its key */
307 TDB_DATA tdb1_nextkey(struct tdb_context *tdb, TDB_DATA oldkey)
308 {
309         uint32_t oldhash;
310         TDB_DATA key = tdb1_null;
311         struct tdb1_record rec;
312         unsigned char *k = NULL;
313         tdb1_off_t off;
314
315         /* Is locked key the old key?  If so, traverse will be reliable. */
316         if (tdb->tdb1.travlocks.off) {
317                 if (tdb1_lock(tdb,tdb->tdb1.travlocks.hash,tdb->tdb1.travlocks.lock_rw))
318                         return tdb1_null;
319                 if (tdb1_rec_read(tdb, tdb->tdb1.travlocks.off, &rec) == -1
320                     || !(k = tdb1_alloc_read(tdb,tdb->tdb1.travlocks.off+sizeof(rec),
321                                             rec.key_len))
322                     || memcmp(k, oldkey.dptr, oldkey.dsize) != 0) {
323                         /* No, it wasn't: unlock it and start from scratch */
324                         if (tdb1_unlock_record(tdb, tdb->tdb1.travlocks.off) != 0) {
325                                 SAFE_FREE(k);
326                                 return tdb1_null;
327                         }
328                         if (tdb1_unlock(tdb, tdb->tdb1.travlocks.hash, tdb->tdb1.travlocks.lock_rw) != 0) {
329                                 SAFE_FREE(k);
330                                 return tdb1_null;
331                         }
332                         tdb->tdb1.travlocks.off = 0;
333                 }
334
335                 SAFE_FREE(k);
336         }
337
338         if (!tdb->tdb1.travlocks.off) {
339                 /* No previous element: do normal find, and lock record */
340                 tdb->tdb1.travlocks.off = tdb1_find_lock_hash(tdb, oldkey, tdb_hash(tdb, oldkey.dptr, oldkey.dsize), tdb->tdb1.travlocks.lock_rw, &rec);
341                 if (!tdb->tdb1.travlocks.off) {
342                         return tdb1_null;
343                 }
344                 tdb->tdb1.travlocks.hash = TDB1_BUCKET(rec.full_hash);
345                 if (tdb1_lock_record(tdb, tdb->tdb1.travlocks.off) != 0) {
346                         tdb_logerr(tdb, tdb->last_error, TDB_LOG_ERROR,
347                                    "tdb1_nextkey: lock_record failed (%s)!",
348                                    strerror(errno));
349                         return tdb1_null;
350                 }
351         }
352         oldhash = tdb->tdb1.travlocks.hash;
353
354         /* Grab next record: locks chain and returned record,
355            unlocks old record */
356         off = tdb1_next_lock(tdb, &tdb->tdb1.travlocks, &rec);
357         if (off != TDB1_NEXT_LOCK_ERR && off != 0) {
358                 key.dsize = rec.key_len;
359                 key.dptr = tdb1_alloc_read(tdb, tdb->tdb1.travlocks.off+sizeof(rec),
360                                           key.dsize);
361                 /* Unlock the chain of this new record */
362                 if (tdb1_unlock(tdb, tdb->tdb1.travlocks.hash, tdb->tdb1.travlocks.lock_rw) != 0)
363                         tdb_logerr(tdb, tdb->last_error, TDB_LOG_ERROR,
364                                    "tdb1_nextkey: WARNING tdb1_unlock failed!");
365         }
366         /* Unlock the chain of old record */
367         if (tdb1_unlock(tdb, TDB1_BUCKET(oldhash), tdb->tdb1.travlocks.lock_rw) != 0)
368                 tdb_logerr(tdb, tdb->last_error, TDB_LOG_ERROR,
369                            "tdb1_nextkey: WARNING tdb1_unlock failed!");
370         return key;
371 }