]> git.sur5r.net Git - openldap/blob - servers/slapd/back-ldbm/dbcache.c
Check for duplicate entries
[openldap] / servers / slapd / back-ldbm / dbcache.c
1 /* ldbmcache.c - maintain a cache of open ldbm files */
2 /* $OpenLDAP$ */
3 /*
4  * Copyright 1998-2000 The OpenLDAP Foundation, All Rights Reserved.
5  * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
6  */
7
8 #include "portable.h"
9
10 #include <stdio.h>
11
12 #include <ac/errno.h>
13 #include <ac/socket.h>
14 #include <ac/string.h>
15 #include <ac/time.h>
16 #include <sys/stat.h>
17
18 #include "slap.h"
19 #include "back-ldbm.h"
20
21 DBCache *
22 ldbm_cache_open(
23     Backend     *be,
24     const char  *name,
25     const char  *suffix,
26     int         flags
27 )
28 {
29         struct ldbminfo *li = (struct ldbminfo *) be->be_private;
30         int             i, lru, empty;
31         time_t          oldtime, curtime;
32         char            buf[MAXPATHLEN];
33 #ifdef HAVE_ST_BLKSIZE
34         struct stat     st;
35 #endif
36
37         sprintf( buf, "%s" LDAP_DIRSEP "%s%s",
38                 li->li_directory, name, suffix );
39
40         if( li->li_dblocking ) {
41                 flags |= LDBM_LOCKING;
42         } else {
43                 flags |= LDBM_NOLOCKING;
44         }
45         
46         if( li->li_dbwritesync ) {
47                 flags |= LDBM_SYNC;
48         } else {
49                 flags |= LDBM_NOSYNC;
50         }
51         
52 #ifdef NEW_LOGGING
53         LDAP_LOG(( "cache", LDAP_LEVEL_ENTRY,
54                    "ldbm_cache_open: \"%s\", %d, %o\n", buf, flags, li->li_mode ));
55 #else
56         Debug( LDAP_DEBUG_TRACE, "=> ldbm_cache_open( \"%s\", %d, %o )\n", buf,
57             flags, li->li_mode );
58 #endif
59
60
61         curtime = slap_get_time();
62         empty = MAXDBCACHE;
63
64         ldap_pvt_thread_mutex_lock( &li->li_dbcache_mutex );
65         do {
66                 lru = 0;
67                 oldtime = curtime;
68                 for ( i = 0; i < MAXDBCACHE; i++ ) {
69                         /* see if this slot is free */
70                         if ( li->li_dbcache[i].dbc_name == NULL) {
71                                 if (empty == MAXDBCACHE)
72                                         empty = i;
73                                 continue;
74                         }
75
76                         if ( strcmp( li->li_dbcache[i].dbc_name, buf ) == 0 ) {
77                                 /* already open - return it */
78                                 if (li->li_dbcache[i].dbc_flags != flags
79                                         && li->li_dbcache[i].dbc_refcnt == 0)
80                                 {
81                                         /* we don't want to use an open cache with different
82                                          * permissions (esp. if we need write but the open
83                                          * cache is read-only).  So close this one if
84                                          * possible, and re-open below.
85                                          *
86                                          * FIXME:  what about the case where the refcount
87                                          * is > 0?  right now, we're using it anyway and
88                                          * just praying.  Can there be more than one open
89                                          * cache to the same db?
90                                          *
91                                          * Also, it's really only necessary to compare the
92                                          * read-only flag, instead of all of the flags,
93                                          * but for now I'm checking all of them.
94                                          */
95                                         lru = i;
96                                         empty = MAXDBCACHE;
97                                         break;
98                                 }
99                                 li->li_dbcache[i].dbc_refcnt++;
100 #ifdef NEW_LOGGING
101                                 LDAP_LOG(( "cache", LDAP_LEVEL_DETAIL1,
102                                            "ldbm_cache_open: cache %d\n", i ));
103 #else
104                                 Debug( LDAP_DEBUG_TRACE,
105                                     "<= ldbm_cache_open (cache %d)\n", i, 0, 0 );
106 #endif
107
108                                 ldap_pvt_thread_mutex_unlock( &li->li_dbcache_mutex );
109                                 return( &li->li_dbcache[i] );
110                         }
111
112                         /* keep track of lru db */
113                         if ( li->li_dbcache[i].dbc_lastref < oldtime
114                                 && li->li_dbcache[i].dbc_refcnt == 0 )
115                         {
116                                 lru = i;
117                                 oldtime = li->li_dbcache[i].dbc_lastref;
118                         }
119                 }
120
121                 i = empty;
122                 if ( i == MAXDBCACHE ) {
123                         /* no empty slots, not already open - close lru and use that slot */
124                         if ( li->li_dbcache[lru].dbc_refcnt == 0 ) {
125                                 i = lru;
126                                 ldbm_close( li->li_dbcache[i].dbc_db );
127                                 free( li->li_dbcache[i].dbc_name );
128                                 li->li_dbcache[i].dbc_name = NULL;
129                         } else {
130 #ifdef NEW_LOGGING
131                                 LDAP_LOG(( "cache", LDAP_LEVEL_INFO,
132                                            "ldbm_cache_open: no unused db to close - waiting\n" ));
133 #else
134                                 Debug( LDAP_DEBUG_ANY,
135                                     "ldbm_cache_open no unused db to close - waiting\n",
136                                     0, 0, 0 );
137 #endif
138
139                                 ldap_pvt_thread_cond_wait( &li->li_dbcache_cv,
140                                             &li->li_dbcache_mutex );
141                                 /* after waiting for a free slot, go back to square
142                                  * one: look for an open cache for this db, or an
143                                  * empty slot, or an unref'ed cache, or wait again.
144                                  */
145                         }
146                 }
147         } while (i == MAXDBCACHE);
148
149         if ( (li->li_dbcache[i].dbc_db = ldbm_open( buf, flags, li->li_mode,
150             li->li_dbcachesize )) == NULL )
151         {
152                 int err = errno;
153 #ifdef NEW_LOGGING
154                 LDAP_LOG(( "cache", LDAP_LEVEL_ERR,
155                            "ldbm_cache_open: \"%s\" failed, errono=%d, reason=%s\n",
156                            buf, err, err > -1 && err < sys_nerr ? sys_errlist[err] :
157                            "unknown" ));
158 #else
159                 Debug( LDAP_DEBUG_TRACE,
160                     "<= ldbm_cache_open NULL \"%s\" errno=%d reason=\"%s\")\n",
161                     buf, err, err > -1 && err < sys_nerr ?
162                     sys_errlist[err] : "unknown" );
163 #endif
164
165                 ldap_pvt_thread_mutex_unlock( &li->li_dbcache_mutex );
166                 return( NULL );
167         }
168         li->li_dbcache[i].dbc_name = ch_strdup( buf );
169         li->li_dbcache[i].dbc_refcnt = 1;
170         li->li_dbcache[i].dbc_lastref = curtime;
171         li->li_dbcache[i].dbc_flags = flags;
172         li->li_dbcache[i].dbc_dirty = 0;
173 #ifdef HAVE_ST_BLKSIZE
174         if ( stat( buf, &st ) == 0 ) {
175                 li->li_dbcache[i].dbc_blksize = st.st_blksize;
176         } else
177 #endif
178         {
179                 li->li_dbcache[i].dbc_blksize = DEFAULT_BLOCKSIZE;
180         }
181         li->li_dbcache[i].dbc_maxids = (li->li_dbcache[i].dbc_blksize /
182             sizeof(ID)) - ID_BLOCK_IDS_OFFSET;
183         li->li_dbcache[i].dbc_maxindirect = ( SLAPD_LDBM_MIN_MAXIDS /
184             li->li_dbcache[i].dbc_maxids ) + 1;
185
186         assert( li->li_dbcache[i].dbc_maxindirect < 256 );
187
188 #ifdef NEW_LOGGING
189         LDAP_LOG(( "cache", LDAP_LEVEL_ARGS,
190                    "ldbm_cache_open: blksize:%ld  maxids:%d  maxindirect:%d\n",
191                    li->li_dbcache[i].dbc_blksize, li->li_dbcache[i].dbc_maxids,
192                    li->li_dbcache[i].dbc_maxindirect ));
193 #else
194         Debug( LDAP_DEBUG_ARGS,
195             "ldbm_cache_open (blksize %ld) (maxids %d) (maxindirect %d)\n",
196             li->li_dbcache[i].dbc_blksize, li->li_dbcache[i].dbc_maxids,
197             li->li_dbcache[i].dbc_maxindirect );
198 #endif
199
200 #ifdef NEW_LOGGING
201         LDAP_LOG(( "cache", LDAP_LEVEL_DETAIL1,
202                    "ldbm_cache_open: opened %d\n", i ));
203 #else
204         Debug( LDAP_DEBUG_TRACE, "<= ldbm_cache_open (opened %d)\n", i, 0, 0 );
205 #endif
206
207         ldap_pvt_thread_mutex_unlock( &li->li_dbcache_mutex );
208         return( &li->li_dbcache[i] );
209 }
210
211 void
212 ldbm_cache_close( Backend *be, DBCache *db )
213 {
214         struct ldbminfo *li = (struct ldbminfo *) be->be_private;
215
216         if( li->li_dbwritesync && db->dbc_dirty ) {
217                 ldbm_sync( db->dbc_db );
218                 db->dbc_dirty = 0;
219         }
220
221         ldap_pvt_thread_mutex_lock( &li->li_dbcache_mutex );
222         if ( --db->dbc_refcnt <= 0 ) {
223                 db->dbc_refcnt = 0;
224                 ldap_pvt_thread_cond_signal( &li->li_dbcache_cv );
225         }
226         ldap_pvt_thread_mutex_unlock( &li->li_dbcache_mutex );
227 }
228
229 void
230 ldbm_cache_really_close( Backend *be, DBCache *db )
231 {
232         struct ldbminfo *li = (struct ldbminfo *) be->be_private;
233
234         ldap_pvt_thread_mutex_lock( &li->li_dbcache_mutex );
235         if ( --db->dbc_refcnt <= 0 ) {
236                 db->dbc_refcnt = 0;
237                 ldap_pvt_thread_cond_signal( &li->li_dbcache_cv );
238                 ldbm_close( db->dbc_db );
239                 free( db->dbc_name );
240                 db->dbc_name = NULL;
241         }
242         ldap_pvt_thread_mutex_unlock( &li->li_dbcache_mutex );
243 }
244
245 void
246 ldbm_cache_flush_all( Backend *be )
247 {
248         struct ldbminfo *li = (struct ldbminfo *) be->be_private;
249         int             i;
250
251         ldap_pvt_thread_mutex_lock( &li->li_dbcache_mutex );
252         for ( i = 0; i < MAXDBCACHE; i++ ) {
253                 if ( li->li_dbcache[i].dbc_name != NULL ) {
254 #ifdef NEW_LOGGING
255                         LDAP_LOG(( "cache", LDAP_LEVEL_DETAIL1,
256                                    "ldbm_cache_flush_all: flushing db (%s)\n",
257                                    li->li_dbcache[i].dbc_name ));
258 #else
259                         Debug( LDAP_DEBUG_TRACE, "ldbm flushing db (%s)\n",
260                             li->li_dbcache[i].dbc_name, 0, 0 );
261 #endif
262
263                         ldbm_sync( li->li_dbcache[i].dbc_db );
264                         li->li_dbcache[i].dbc_dirty = 0;
265                         if ( li->li_dbcache[i].dbc_refcnt != 0 ) {
266 #ifdef NEW_LOGGING
267                                 LDAP_LOG(( "cache", LDAP_LEVEL_INFO,
268                                            "ldbm_cache_flush_all: couldn't close db (%s), refcnt=%d\n",
269                                            li->li_dbcache[i].dbc_name, li->li_dbcache[i].dbc_refcnt ));
270 #else
271                                 Debug( LDAP_DEBUG_TRACE,
272                                        "refcnt = %d, couldn't close db (%s)\n",
273                                        li->li_dbcache[i].dbc_refcnt,
274                                        li->li_dbcache[i].dbc_name, 0 );
275 #endif
276
277                         } else {
278 #ifdef NEW_LOGGING
279                                 LDAP_LOG(( "cache", LDAP_LEVEL_DETAIL1,
280                                            "ldbm_cache_flush_all: ldbm closing db (%s)\n",
281                                            li->li_dbcache[i].dbc_name ));
282 #else
283                                 Debug( LDAP_DEBUG_TRACE,
284                                        "ldbm closing db (%s)\n",
285                                        li->li_dbcache[i].dbc_name, 0, 0 );
286 #endif
287
288                                 ldap_pvt_thread_cond_signal( &li->li_dbcache_cv );
289                                 ldbm_close( li->li_dbcache[i].dbc_db );
290                                 free( li->li_dbcache[i].dbc_name );
291                                 li->li_dbcache[i].dbc_name = NULL;
292                         }
293                 }
294         }
295         ldap_pvt_thread_mutex_unlock( &li->li_dbcache_mutex );
296 }
297
298 Datum
299 ldbm_cache_fetch(
300     DBCache     *db,
301     Datum               key
302 )
303 {
304         Datum   data;
305
306         return ldbm_fetch( db->dbc_db, key );
307
308         return( data );
309 }
310
311 int
312 ldbm_cache_store(
313     DBCache     *db,
314     Datum               key,
315     Datum               data,
316     int                 flags
317 )
318 {
319         int     rc;
320
321 #ifdef LDBM_DEBUG
322         Statslog( LDAP_DEBUG_STATS,
323                 "=> ldbm_cache_store(): key.dptr=%s, key.dsize=%d\n",
324                 key.dptr, key.dsize, 0, 0, 0 );
325
326         Statslog( LDAP_DEBUG_STATS,
327                 "=> ldbm_cache_store(): key.dptr=0x%08x, data.dptr=0x%0 8x\n",
328                 key.dptr, data.dptr, 0, 0, 0 );
329
330         Statslog( LDAP_DEBUG_STATS,
331                 "=> ldbm_cache_store(): data.dptr=%s, data.dsize=%d\n",
332                 data.dptr, data.dsize, 0, 0, 0 );
333
334         Statslog( LDAP_DEBUG_STATS,
335                 "=> ldbm_cache_store(): flags=0x%08x\n",
336                 flags, 0, 0, 0, 0 );
337 #endif /* LDBM_DEBUG */
338
339         db->dbc_dirty = 1;
340         rc = ldbm_store( db->dbc_db, key, data, flags );
341
342         return( rc );
343 }
344
345 int
346 ldbm_cache_delete(
347     DBCache     *db,
348     Datum               key
349 )
350 {
351         int     rc;
352
353         db->dbc_dirty = 1;
354         rc = ldbm_delete( db->dbc_db, key );
355
356         return( rc );
357 }