b2bddec18d9a4c1f5fcee153de8f4e0d4efed29c
[ira/wip.git] / source / lib / tdb / common / 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 2 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, write to the Free Software
26    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
27 */
28
29 #include "tdb_private.h"
30
31 /* Uses traverse lock: 0 = finish, -1 = error, other = record offset */
32 static int tdb_next_lock(struct tdb_context *tdb, struct tdb_traverse_lock *tlock,
33                          struct list_struct *rec)
34 {
35         int want_next = (tlock->off != 0);
36
37         /* Lock each chain from the start one. */
38         for (; tlock->hash < tdb->header.hash_size; tlock->hash++) {
39                 if (!tlock->off && tlock->hash != 0) {
40                         /* this is an optimisation for the common case where
41                            the hash chain is empty, which is particularly
42                            common for the use of tdb with ldb, where large
43                            hashes are used. In that case we spend most of our
44                            time in tdb_brlock(), locking empty hash chains.
45                            
46                            To avoid this, we do an unlocked pre-check to see
47                            if the hash chain is empty before starting to look
48                            inside it. If it is empty then we can avoid that
49                            hash chain. If it isn't empty then we can't believe
50                            the value we get back, as we read it without a
51                            lock, so instead we get the lock and re-fetch the
52                            value below.
53                            
54                            Notice that not doing this optimisation on the
55                            first hash chain is critical. We must guarantee
56                            that we have done at least one fcntl lock at the
57                            start of a search to guarantee that memory is
58                            coherent on SMP systems. If records are added by
59                            others during the search then thats OK, and we
60                            could possibly miss those with this trick, but we
61                            could miss them anyway without this trick, so the
62                            semantics don't change.
63                            
64                            With a non-indexed ldb search this trick gains us a
65                            factor of around 80 in speed on a linux 2.6.x
66                            system (testing using ldbtest).
67                         */
68                         tdb->methods->next_hash_chain(tdb, &tlock->hash);
69                         if (tlock->hash == tdb->header.hash_size) {
70                                 continue;
71                         }
72                 }
73
74                 if (tdb_lock(tdb, tlock->hash, tlock->lock_rw) == -1)
75                         return -1;
76
77                 /* No previous record?  Start at top of chain. */
78                 if (!tlock->off) {
79                         if (tdb_ofs_read(tdb, TDB_HASH_TOP(tlock->hash),
80                                      &tlock->off) == -1)
81                                 goto fail;
82                 } else {
83                         /* Otherwise unlock the previous record. */
84                         if (tdb_unlock_record(tdb, tlock->off) != 0)
85                                 goto fail;
86                 }
87
88                 if (want_next) {
89                         /* We have offset of old record: grab next */
90                         if (tdb_rec_read(tdb, tlock->off, rec) == -1)
91                                 goto fail;
92                         tlock->off = rec->next;
93                 }
94
95                 /* Iterate through chain */
96                 while( tlock->off) {
97                         tdb_off_t current;
98                         if (tdb_rec_read(tdb, tlock->off, rec) == -1)
99                                 goto fail;
100
101                         /* Detect infinite loops. From "Shlomi Yaakobovich" <Shlomi@exanet.com>. */
102                         if (tlock->off == rec->next) {
103                                 TDB_LOG((tdb, TDB_DEBUG_FATAL, "tdb_next_lock: loop detected.\n"));
104                                 goto fail;
105                         }
106
107                         if (!TDB_DEAD(rec)) {
108                                 /* Woohoo: we found one! */
109                                 if (tdb_lock_record(tdb, tlock->off) != 0)
110                                         goto fail;
111                                 return tlock->off;
112                         }
113
114                         /* Try to clean dead ones from old traverses */
115                         current = tlock->off;
116                         tlock->off = rec->next;
117                         if (!(tdb->read_only || tdb->traverse_read) && 
118                             tdb_do_delete(tdb, current, rec) != 0)
119                                 goto fail;
120                 }
121                 tdb_unlock(tdb, tlock->hash, tlock->lock_rw);
122                 want_next = 0;
123         }
124         /* We finished iteration without finding anything */
125         return TDB_ERRCODE(TDB_SUCCESS, 0);
126
127  fail:
128         tlock->off = 0;
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"));
131         return -1;
132 }
133
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
138   */
139 static int tdb_traverse_internal(struct tdb_context *tdb, 
140                                  tdb_traverse_func fn, void *private_data,
141                                  struct tdb_traverse_lock *tl)
142 {
143         TDB_DATA key, dbuf;
144         struct list_struct rec;
145         int ret, count = 0;
146
147         /* This was in the initializaton, above, but the IRIX compiler
148          * did not like it.  crh
149          */
150         tl->next = tdb->travlocks.next;
151
152         /* fcntl locks don't stack: beware traverse inside traverse */
153         tdb->travlocks.next = tl;
154
155         /* tdb_next_lock places locks on the record returned, and its chain */
156         while ((ret = tdb_next_lock(tdb, tl, &rec)) > 0) {
157                 count++;
158                 /* now read the full record */
159                 key.dptr = tdb_alloc_read(tdb, tl->off + sizeof(rec), 
160                                           rec.key_len + rec.data_len);
161                 if (!key.dptr) {
162                         ret = -1;
163                         if (tdb_unlock(tdb, tl->hash, tl->lock_rw) != 0)
164                                 goto out;
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"));
167                         goto out;
168                 }
169                 key.dsize = rec.key_len;
170                 dbuf.dptr = key.dptr + rec.key_len;
171                 dbuf.dsize = rec.data_len;
172
173                 /* Drop chain lock, call out */
174                 if (tdb_unlock(tdb, tl->hash, tl->lock_rw) != 0) {
175                         ret = -1;
176                         SAFE_FREE(key.dptr);
177                         goto out;
178                 }
179                 if (fn && fn(tdb, key, dbuf, private_data)) {
180                         /* They want us to terminate traversal */
181                         ret = count;
182                         if (tdb_unlock_record(tdb, tl->off) != 0) {
183                                 TDB_LOG((tdb, TDB_DEBUG_FATAL, "tdb_traverse: unlock_record failed!\n"));;
184                                 ret = -1;
185                         }
186                         SAFE_FREE(key.dptr);
187                         goto out;
188                 }
189                 SAFE_FREE(key.dptr);
190         }
191 out:
192         tdb->travlocks.next = tl->next;
193         if (ret < 0)
194                 return -1;
195         else
196                 return count;
197 }
198
199
200 /*
201   a write style traverse - temporarily marks the db read only
202 */
203 int tdb_traverse_read(struct tdb_context *tdb, 
204                       tdb_traverse_func fn, void *private_data)
205 {
206         struct tdb_traverse_lock tl = { NULL, 0, 0, F_RDLCK };
207         int ret;
208
209         /* we need to get a read lock on the transaction lock here to
210            cope with the lock ordering semantics of solaris10 */
211         if (tdb_transaction_lock(tdb, F_RDLCK)) {
212                 return -1;
213         }
214
215         tdb->traverse_read++;
216         ret = tdb_traverse_internal(tdb, fn, private_data, &tl);
217         tdb->traverse_read--;
218
219         tdb_transaction_unlock(tdb);
220
221         return ret;
222 }
223
224 /*
225   a write style traverse - needs to get the transaction lock to
226   prevent deadlocks
227 */
228 int tdb_traverse(struct tdb_context *tdb, 
229                  tdb_traverse_func fn, void *private_data)
230 {
231         struct tdb_traverse_lock tl = { NULL, 0, 0, F_WRLCK };
232         int ret;
233
234         if (tdb->read_only || tdb->traverse_read) {
235                 return tdb_traverse_read(tdb, fn, private_data);
236         }
237         
238         if (tdb_transaction_lock(tdb, F_WRLCK)) {
239                 return -1;
240         }
241
242         ret = tdb_traverse_internal(tdb, fn, private_data, &tl);
243
244         tdb_transaction_unlock(tdb);
245
246         return ret;
247 }
248
249
250 /* find the first entry in the database and return its key */
251 TDB_DATA tdb_firstkey(struct tdb_context *tdb)
252 {
253         TDB_DATA key;
254         struct list_struct rec;
255
256         /* release any old lock */
257         if (tdb_unlock_record(tdb, tdb->travlocks.off) != 0)
258                 return tdb_null;
259         tdb->travlocks.off = tdb->travlocks.hash = 0;
260         tdb->travlocks.lock_rw = F_RDLCK;
261
262         /* Grab first record: locks chain and returned record. */
263         if (tdb_next_lock(tdb, &tdb->travlocks, &rec) <= 0)
264                 return tdb_null;
265         /* now read the key */
266         key.dsize = rec.key_len;
267         key.dptr =tdb_alloc_read(tdb,tdb->travlocks.off+sizeof(rec),key.dsize);
268
269         /* Unlock the hash chain of the record we just read. */
270         if (tdb_unlock(tdb, tdb->travlocks.hash, tdb->travlocks.lock_rw) != 0)
271                 TDB_LOG((tdb, TDB_DEBUG_FATAL, "tdb_firstkey: error occurred while tdb_unlocking!\n"));
272         return key;
273 }
274
275 /* find the next entry in the database, returning its key */
276 TDB_DATA tdb_nextkey(struct tdb_context *tdb, TDB_DATA oldkey)
277 {
278         u32 oldhash;
279         TDB_DATA key = tdb_null;
280         struct list_struct rec;
281         unsigned char *k = NULL;
282
283         /* Is locked key the old key?  If so, traverse will be reliable. */
284         if (tdb->travlocks.off) {
285                 if (tdb_lock(tdb,tdb->travlocks.hash,tdb->travlocks.lock_rw))
286                         return tdb_null;
287                 if (tdb_rec_read(tdb, tdb->travlocks.off, &rec) == -1
288                     || !(k = tdb_alloc_read(tdb,tdb->travlocks.off+sizeof(rec),
289                                             rec.key_len))
290                     || memcmp(k, oldkey.dptr, oldkey.dsize) != 0) {
291                         /* No, it wasn't: unlock it and start from scratch */
292                         if (tdb_unlock_record(tdb, tdb->travlocks.off) != 0) {
293                                 SAFE_FREE(k);
294                                 return tdb_null;
295                         }
296                         if (tdb_unlock(tdb, tdb->travlocks.hash, tdb->travlocks.lock_rw) != 0) {
297                                 SAFE_FREE(k);
298                                 return tdb_null;
299                         }
300                         tdb->travlocks.off = 0;
301                 }
302
303                 SAFE_FREE(k);
304         }
305
306         if (!tdb->travlocks.off) {
307                 /* No previous element: do normal find, and lock record */
308                 tdb->travlocks.off = tdb_find_lock_hash(tdb, oldkey, tdb->hash_fn(&oldkey), tdb->travlocks.lock_rw, &rec);
309                 if (!tdb->travlocks.off)
310                         return tdb_null;
311                 tdb->travlocks.hash = BUCKET(rec.full_hash);
312                 if (tdb_lock_record(tdb, tdb->travlocks.off) != 0) {
313                         TDB_LOG((tdb, TDB_DEBUG_FATAL, "tdb_nextkey: lock_record failed (%s)!\n", strerror(errno)));
314                         return tdb_null;
315                 }
316         }
317         oldhash = tdb->travlocks.hash;
318
319         /* Grab next record: locks chain and returned record,
320            unlocks old record */
321         if (tdb_next_lock(tdb, &tdb->travlocks, &rec) > 0) {
322                 key.dsize = rec.key_len;
323                 key.dptr = tdb_alloc_read(tdb, tdb->travlocks.off+sizeof(rec),
324                                           key.dsize);
325                 /* Unlock the chain of this new record */
326                 if (tdb_unlock(tdb, tdb->travlocks.hash, tdb->travlocks.lock_rw) != 0)
327                         TDB_LOG((tdb, TDB_DEBUG_FATAL, "tdb_nextkey: WARNING tdb_unlock failed!\n"));
328         }
329         /* Unlock the chain of old record */
330         if (tdb_unlock(tdb, BUCKET(oldhash), tdb->travlocks.lock_rw) != 0)
331                 TDB_LOG((tdb, TDB_DEBUG_FATAL, "tdb_nextkey: WARNING tdb_unlock failed!\n"));
332         return key;
333 }