1 /* ldbmcache.c - maintain a cache of open ldbm files */
4 * Copyright 1998-2000 The OpenLDAP Foundation, All Rights Reserved.
5 * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
13 #include <ac/socket.h>
14 #include <ac/string.h>
19 #include "back-ldbm.h"
29 struct ldbminfo *li = (struct ldbminfo *) be->be_private;
31 time_t oldtime, curtime;
33 #ifdef HAVE_ST_BLKSIZE
38 sprintf( buf, "%s%s", name, suffix );
40 sprintf( buf, "%s" LDAP_DIRSEP "%s%s",
41 li->li_directory, name, suffix );
43 if( li->li_dblocking ) {
44 flags |= LDBM_LOCKING;
46 flags |= LDBM_NOLOCKING;
49 if( li->li_dbwritesync && global_backendsyncfreq == 0) {
56 LDAP_LOG(( "cache", LDAP_LEVEL_ENTRY,
57 "ldbm_cache_open: \"%s\", %d, %o\n", buf, flags, li->li_mode ));
59 Debug( LDAP_DEBUG_TRACE, "=> ldbm_cache_open( \"%s\", %d, %o )\n", buf,
64 curtime = slap_get_time();
67 ldap_pvt_thread_mutex_lock( &li->li_dbcache_mutex );
71 for ( i = 0; i < MAXDBCACHE; i++ ) {
72 /* see if this slot is free */
73 if ( li->li_dbcache[i].dbc_name == NULL) {
74 if (empty == MAXDBCACHE)
79 if ( strcmp( li->li_dbcache[i].dbc_name, buf ) == 0 ) {
80 /* already open - return it */
81 if (li->li_dbcache[i].dbc_flags != flags
82 && li->li_dbcache[i].dbc_refcnt == 0)
84 /* we don't want to use an open cache with different
85 * permissions (esp. if we need write but the open
86 * cache is read-only). So close this one if
87 * possible, and re-open below.
89 * FIXME: what about the case where the refcount
90 * is > 0? right now, we're using it anyway and
91 * just praying. Can there be more than one open
92 * cache to the same db?
94 * Also, it's really only necessary to compare the
95 * read-only flag, instead of all of the flags,
96 * but for now I'm checking all of them.
102 li->li_dbcache[i].dbc_refcnt++;
104 LDAP_LOG(( "cache", LDAP_LEVEL_DETAIL1,
105 "ldbm_cache_open: cache %d\n", i ));
107 Debug( LDAP_DEBUG_TRACE,
108 "<= ldbm_cache_open (cache %d)\n", i, 0, 0 );
111 ldap_pvt_thread_mutex_unlock( &li->li_dbcache_mutex );
112 return( &li->li_dbcache[i] );
115 /* keep track of lru db */
116 if ( li->li_dbcache[i].dbc_lastref < oldtime
117 && li->li_dbcache[i].dbc_refcnt == 0 )
120 oldtime = li->li_dbcache[i].dbc_lastref;
125 if ( i == MAXDBCACHE ) {
126 /* no empty slots, not already open - close lru and use that slot */
127 if ( li->li_dbcache[lru].dbc_refcnt == 0 ) {
129 ldbm_close( li->li_dbcache[i].dbc_db );
130 free( li->li_dbcache[i].dbc_name );
131 li->li_dbcache[i].dbc_name = NULL;
134 LDAP_LOG(( "cache", LDAP_LEVEL_INFO,
135 "ldbm_cache_open: no unused db to close - waiting\n" ));
137 Debug( LDAP_DEBUG_ANY,
138 "ldbm_cache_open no unused db to close - waiting\n",
142 ldap_pvt_thread_cond_wait( &li->li_dbcache_cv,
143 &li->li_dbcache_mutex );
144 /* after waiting for a free slot, go back to square
145 * one: look for an open cache for this db, or an
146 * empty slot, or an unref'ed cache, or wait again.
150 } while (i == MAXDBCACHE);
152 if ( (li->li_dbcache[i].dbc_db = ldbm_open( li->li_dbenv, buf, flags, li->li_mode,
153 li->li_dbcachesize )) == NULL )
157 LDAP_LOG(( "cache", LDAP_LEVEL_ERR,
158 "ldbm_cache_open: \"%s\" failed, errono=%d, reason=%s\n",
159 buf, err, err > -1 && err < sys_nerr ? sys_errlist[err] :
162 Debug( LDAP_DEBUG_TRACE,
163 "<= ldbm_cache_open NULL \"%s\" errno=%d reason=\"%s\")\n",
164 buf, err, err > -1 && err < sys_nerr ?
165 sys_errlist[err] : "unknown" );
168 ldap_pvt_thread_mutex_unlock( &li->li_dbcache_mutex );
171 li->li_dbcache[i].dbc_name = ch_strdup( buf );
172 li->li_dbcache[i].dbc_refcnt = 1;
173 li->li_dbcache[i].dbc_lastref = curtime;
174 li->li_dbcache[i].dbc_flags = flags;
175 li->li_dbcache[i].dbc_dirty = 0;
176 #ifdef HAVE_ST_BLKSIZE
177 if ( stat( buf, &st ) == 0 ) {
178 li->li_dbcache[i].dbc_blksize = st.st_blksize;
182 li->li_dbcache[i].dbc_blksize = DEFAULT_BLOCKSIZE;
184 li->li_dbcache[i].dbc_maxids = (li->li_dbcache[i].dbc_blksize /
185 sizeof(ID)) - ID_BLOCK_IDS_OFFSET;
186 li->li_dbcache[i].dbc_maxindirect = ( SLAPD_LDBM_MIN_MAXIDS /
187 li->li_dbcache[i].dbc_maxids ) + 1;
189 assert( li->li_dbcache[i].dbc_maxindirect < 256 );
192 LDAP_LOG(( "cache", LDAP_LEVEL_ARGS,
193 "ldbm_cache_open: blksize:%ld maxids:%d maxindirect:%d\n",
194 li->li_dbcache[i].dbc_blksize, li->li_dbcache[i].dbc_maxids,
195 li->li_dbcache[i].dbc_maxindirect ));
197 Debug( LDAP_DEBUG_ARGS,
198 "ldbm_cache_open (blksize %ld) (maxids %d) (maxindirect %d)\n",
199 li->li_dbcache[i].dbc_blksize, li->li_dbcache[i].dbc_maxids,
200 li->li_dbcache[i].dbc_maxindirect );
204 LDAP_LOG(( "cache", LDAP_LEVEL_DETAIL1,
205 "ldbm_cache_open: opened %d\n", i ));
207 Debug( LDAP_DEBUG_TRACE, "<= ldbm_cache_open (opened %d)\n", i, 0, 0 );
210 ldap_pvt_thread_mutex_unlock( &li->li_dbcache_mutex );
211 return( &li->li_dbcache[i] );
215 ldbm_cache_close( Backend *be, DBCache *db )
217 struct ldbminfo *li = (struct ldbminfo *) be->be_private;
219 if( li->li_dbwritesync && db->dbc_dirty ) {
220 ldbm_sync( db->dbc_db );
224 ldap_pvt_thread_mutex_lock( &li->li_dbcache_mutex );
225 if ( --db->dbc_refcnt <= 0 ) {
227 ldap_pvt_thread_cond_signal( &li->li_dbcache_cv );
229 ldap_pvt_thread_mutex_unlock( &li->li_dbcache_mutex );
233 ldbm_cache_really_close( Backend *be, DBCache *db )
235 struct ldbminfo *li = (struct ldbminfo *) be->be_private;
237 ldap_pvt_thread_mutex_lock( &li->li_dbcache_mutex );
238 if ( --db->dbc_refcnt <= 0 ) {
240 ldap_pvt_thread_cond_signal( &li->li_dbcache_cv );
241 ldbm_close( db->dbc_db );
242 free( db->dbc_name );
245 ldap_pvt_thread_mutex_unlock( &li->li_dbcache_mutex );
249 ldbm_cache_flush_all( Backend *be )
251 struct ldbminfo *li = (struct ldbminfo *) be->be_private;
254 ldap_pvt_thread_mutex_lock( &li->li_dbcache_mutex );
255 for ( i = 0; i < MAXDBCACHE; i++ ) {
256 if ( li->li_dbcache[i].dbc_name != NULL ) {
258 LDAP_LOG(( "cache", LDAP_LEVEL_DETAIL1,
259 "ldbm_cache_flush_all: flushing db (%s)\n",
260 li->li_dbcache[i].dbc_name ));
262 Debug( LDAP_DEBUG_TRACE, "ldbm flushing db (%s)\n",
263 li->li_dbcache[i].dbc_name, 0, 0 );
266 ldbm_sync( li->li_dbcache[i].dbc_db );
267 li->li_dbcache[i].dbc_dirty = 0;
268 if ( li->li_dbcache[i].dbc_refcnt != 0 ) {
270 LDAP_LOG(( "cache", LDAP_LEVEL_INFO,
271 "ldbm_cache_flush_all: couldn't close db (%s), refcnt=%d\n",
272 li->li_dbcache[i].dbc_name, li->li_dbcache[i].dbc_refcnt ));
274 Debug( LDAP_DEBUG_TRACE,
275 "refcnt = %d, couldn't close db (%s)\n",
276 li->li_dbcache[i].dbc_refcnt,
277 li->li_dbcache[i].dbc_name, 0 );
282 LDAP_LOG(( "cache", LDAP_LEVEL_DETAIL1,
283 "ldbm_cache_flush_all: ldbm closing db (%s)\n",
284 li->li_dbcache[i].dbc_name ));
286 Debug( LDAP_DEBUG_TRACE,
287 "ldbm closing db (%s)\n",
288 li->li_dbcache[i].dbc_name, 0, 0 );
291 ldap_pvt_thread_cond_signal( &li->li_dbcache_cv );
292 ldbm_close( li->li_dbcache[i].dbc_db );
293 free( li->li_dbcache[i].dbc_name );
294 li->li_dbcache[i].dbc_name = NULL;
298 ldap_pvt_thread_mutex_unlock( &li->li_dbcache_mutex );
302 ldbm_cache_sync( Backend *be )
304 struct ldbminfo *li = (struct ldbminfo *) be->be_private;
307 ldap_pvt_thread_mutex_lock( &li->li_dbcache_mutex );
308 for ( i = 0; i < MAXDBCACHE; i++ ) {
309 if ( li->li_dbcache[i].dbc_name != NULL && li->li_dbcache[i].dbc_dirty ) {
310 Debug( LDAP_DEBUG_TRACE, "ldbm syncing db (%s)\n",
311 li->li_dbcache[i].dbc_name, 0, 0 );
312 ldbm_sync( li->li_dbcache[i].dbc_db );
313 li->li_dbcache[i].dbc_dirty = 0;
316 ldap_pvt_thread_mutex_unlock( &li->li_dbcache_mutex );
327 return ldbm_fetch( db->dbc_db, key );
343 Statslog( LDAP_DEBUG_STATS,
344 "=> ldbm_cache_store(): key.dptr=%s, key.dsize=%d\n",
345 key.dptr, key.dsize, 0, 0, 0 );
347 Statslog( LDAP_DEBUG_STATS,
348 "=> ldbm_cache_store(): key.dptr=0x%08x, data.dptr=0x%0 8x\n",
349 key.dptr, data.dptr, 0, 0, 0 );
351 Statslog( LDAP_DEBUG_STATS,
352 "=> ldbm_cache_store(): data.dptr=%s, data.dsize=%d\n",
353 data.dptr, data.dsize, 0, 0, 0 );
355 Statslog( LDAP_DEBUG_STATS,
356 "=> ldbm_cache_store(): flags=0x%08x\n",
358 #endif /* LDBM_DEBUG */
361 rc = ldbm_store( db->dbc_db, key, data, flags );
375 rc = ldbm_delete( db->dbc_db, key );
381 ldbm_cache_sync_daemon(
385 Backend *be = (Backend *)be_ptr;
386 struct ldbminfo *li = (struct ldbminfo *) be->be_private;
388 Debug( LDAP_DEBUG_ANY, "synchronizer starting for %s\n", li->li_directory, 0, 0 );
390 while (!li->li_dbshutdown) {
391 int i = li->li_dbsyncwaitn;
393 sleep( li->li_dbsyncfreq );
395 while (i && ldap_pvt_thread_pool_backload(&connection_pool) != 0) {
396 Debug( LDAP_DEBUG_TRACE, "delay syncing %s\n", li->li_directory, 0, 0 );
397 sleep(li->li_dbsyncwaitinterval);
401 if (!li->li_dbshutdown) {
402 Debug( LDAP_DEBUG_TRACE, "syncing %s\n", li->li_directory, 0, 0 );
403 ldbm_cache_sync( be );
407 Debug( LDAP_DEBUG_ANY, "synchronizer stopping\n", 0, 0, 0 );