1 /* ldbmcache.c - maintain a cache of open ldbm files */
4 * Copyright 1998-2003 The OpenLDAP Foundation, All Rights Reserved.
5 * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
13 #include <ac/socket.h>
14 #include <ac/string.h>
16 #include <ac/unistd.h>
20 #include "back-ldbm.h"
30 struct ldbminfo *li = (struct ldbminfo *) be->be_private;
34 #ifdef HAVE_ST_BLKSIZE
39 sprintf( buf, "%s%s", name, suffix );
41 sprintf( buf, "%s" LDAP_DIRSEP "%s%s",
42 li->li_directory, name, suffix );
44 if( li->li_dblocking ) {
45 flags |= LDBM_LOCKING;
47 flags |= LDBM_NOLOCKING;
50 if( li->li_dbwritesync ) {
57 LDAP_LOG( CACHE, ENTRY,
58 "ldbm_cache_open: \"%s\", %d, %o\n", buf, flags, li->li_mode );
60 Debug( LDAP_DEBUG_TRACE, "=> ldbm_cache_open( \"%s\", %d, %o )\n", buf,
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, DETAIL1,
105 "ldbm_cache_open: cache %d\n", i, 0, 0 );
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_refcnt == 0 ) &&
118 ( li->li_dbcache[i].dbc_lastref < oldtime )) )
121 oldtime = li->li_dbcache[i].dbc_lastref;
126 if ( i == MAXDBCACHE ) {
127 /* no empty slots, not already open - close lru and use that slot */
128 if ( li->li_dbcache[lru].dbc_refcnt == 0 ) {
130 ldbm_close( li->li_dbcache[i].dbc_db );
131 free( li->li_dbcache[i].dbc_name );
132 li->li_dbcache[i].dbc_name = NULL;
135 LDAP_LOG( CACHE, INFO,
136 "ldbm_cache_open: no unused db to close - waiting\n",
139 Debug( LDAP_DEBUG_ANY,
140 "ldbm_cache_open no unused db to close - waiting\n",
144 ldap_pvt_thread_cond_wait( &li->li_dbcache_cv,
145 &li->li_dbcache_mutex );
146 /* after waiting for a free slot, go back to square
147 * one: look for an open cache for this db, or an
148 * empty slot, or an unref'ed cache, or wait again.
152 } while (i == MAXDBCACHE);
154 if ( (li->li_dbcache[i].dbc_db = ldbm_open( li->li_dbenv, buf, flags, li->li_mode,
155 li->li_dbcachesize )) == NULL )
159 LDAP_LOG( CACHE, ERR,
160 "ldbm_cache_open: \"%s\" failed, errono=%d, reason=%s\n",
161 buf, err, err > -1 && err < sys_nerr ? sys_errlist[err] :
164 Debug( LDAP_DEBUG_TRACE,
165 "<= ldbm_cache_open NULL \"%s\" errno=%d reason=\"%s\")\n",
166 buf, err, err > -1 && err < sys_nerr ?
167 sys_errlist[err] : "unknown" );
170 ldap_pvt_thread_mutex_unlock( &li->li_dbcache_mutex );
173 li->li_dbcache[i].dbc_name = ch_strdup( buf );
174 li->li_dbcache[i].dbc_refcnt = 1;
175 li->li_dbcache[i].dbc_lastref = slap_get_time();
176 li->li_dbcache[i].dbc_flags = flags;
177 li->li_dbcache[i].dbc_dirty = 0;
178 #ifdef HAVE_ST_BLKSIZE
179 if ( stat( buf, &st ) == 0 ) {
180 li->li_dbcache[i].dbc_blksize = st.st_blksize;
184 li->li_dbcache[i].dbc_blksize = DEFAULT_BLOCKSIZE;
186 li->li_dbcache[i].dbc_maxids = (li->li_dbcache[i].dbc_blksize /
187 sizeof(ID)) - ID_BLOCK_IDS_OFFSET;
188 li->li_dbcache[i].dbc_maxindirect = ( SLAPD_LDBM_MIN_MAXIDS /
189 li->li_dbcache[i].dbc_maxids ) + 1;
191 assert( li->li_dbcache[i].dbc_maxindirect < 256 );
194 LDAP_LOG( CACHE, 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 );
199 Debug( LDAP_DEBUG_ARGS,
200 "ldbm_cache_open (blksize %ld) (maxids %d) (maxindirect %d)\n",
201 li->li_dbcache[i].dbc_blksize, li->li_dbcache[i].dbc_maxids,
202 li->li_dbcache[i].dbc_maxindirect );
206 LDAP_LOG( CACHE, DETAIL1, "<= ldbm_cache_open: (opened %d)\n", i, 0, 0 );
208 Debug( LDAP_DEBUG_TRACE, "<= ldbm_cache_open (opened %d)\n", i, 0, 0 );
211 ldap_pvt_thread_mutex_init( &li->li_dbcache[i].dbc_write_mutex );
213 ldap_pvt_thread_mutex_unlock( &li->li_dbcache_mutex );
214 return( &li->li_dbcache[i] );
218 ldbm_cache_close( Backend *be, DBCache *db )
220 struct ldbminfo *li = (struct ldbminfo *) be->be_private;
222 if( li->li_dbwritesync && db->dbc_dirty ) {
223 ldbm_sync( db->dbc_db );
227 ldap_pvt_thread_mutex_lock( &li->li_dbcache_mutex );
228 if ( --db->dbc_refcnt <= 0 ) {
230 ldap_pvt_thread_cond_signal( &li->li_dbcache_cv );
232 ldap_pvt_thread_mutex_unlock( &li->li_dbcache_mutex );
236 ldbm_cache_really_close( Backend *be, DBCache *db )
238 struct ldbminfo *li = (struct ldbminfo *) be->be_private;
240 ldap_pvt_thread_mutex_lock( &li->li_dbcache_mutex );
241 if ( --db->dbc_refcnt <= 0 ) {
243 ldap_pvt_thread_cond_signal( &li->li_dbcache_cv );
244 ldbm_close( db->dbc_db );
245 free( db->dbc_name );
247 ldap_pvt_thread_mutex_destroy( &db->dbc_write_mutex );
249 ldap_pvt_thread_mutex_unlock( &li->li_dbcache_mutex );
253 ldbm_cache_flush_all( Backend *be )
255 struct ldbminfo *li = (struct ldbminfo *) be->be_private;
258 ldap_pvt_thread_mutex_lock( &li->li_dbcache_mutex );
259 for ( i = 0; i < MAXDBCACHE; i++ ) {
260 if ( li->li_dbcache[i].dbc_name != NULL ) {
262 LDAP_LOG( CACHE, DETAIL1,
263 "ldbm_cache_flush_all: flushing db (%s)\n",
264 li->li_dbcache[i].dbc_name, 0, 0 );
266 Debug( LDAP_DEBUG_TRACE, "ldbm flushing db (%s)\n",
267 li->li_dbcache[i].dbc_name, 0, 0 );
270 ldbm_sync( li->li_dbcache[i].dbc_db );
271 li->li_dbcache[i].dbc_dirty = 0;
272 if ( li->li_dbcache[i].dbc_refcnt != 0 ) {
274 LDAP_LOG( CACHE, INFO,
275 "ldbm_cache_flush_all: couldn't close db (%s), refcnt=%d\n",
276 li->li_dbcache[i].dbc_name, li->li_dbcache[i].dbc_refcnt,0);
278 Debug( LDAP_DEBUG_TRACE,
279 "refcnt = %d, couldn't close db (%s)\n",
280 li->li_dbcache[i].dbc_refcnt,
281 li->li_dbcache[i].dbc_name, 0 );
286 LDAP_LOG( CACHE, DETAIL1,
287 "ldbm_cache_flush_all: ldbm closing db (%s)\n",
288 li->li_dbcache[i].dbc_name, 0, 0 );
290 Debug( LDAP_DEBUG_TRACE,
291 "ldbm closing db (%s)\n",
292 li->li_dbcache[i].dbc_name, 0, 0 );
295 ldap_pvt_thread_cond_signal( &li->li_dbcache_cv );
296 ldbm_close( li->li_dbcache[i].dbc_db );
297 free( li->li_dbcache[i].dbc_name );
298 li->li_dbcache[i].dbc_name = NULL;
302 ldap_pvt_thread_mutex_unlock( &li->li_dbcache_mutex );
306 ldbm_cache_sync( Backend *be )
308 struct ldbminfo *li = (struct ldbminfo *) be->be_private;
311 ldap_pvt_thread_mutex_lock( &li->li_dbcache_mutex );
312 for ( i = 0; i < MAXDBCACHE; i++ ) {
313 if ( li->li_dbcache[i].dbc_name != NULL && li->li_dbcache[i].dbc_dirty ) {
315 LDAP_LOG ( CACHE, DETAIL1, "ldbm_cache_sync: "
316 "ldbm syncing db (%s)\n", li->li_dbcache[i].dbc_name, 0, 0 );
318 Debug( LDAP_DEBUG_TRACE, "ldbm syncing db (%s)\n",
319 li->li_dbcache[i].dbc_name, 0, 0 );
321 ldbm_sync( li->li_dbcache[i].dbc_db );
322 li->li_dbcache[i].dbc_dirty = 0;
325 ldap_pvt_thread_mutex_unlock( &li->li_dbcache_mutex );
328 #if 0 /* macro in proto-back-ldbm.h */
335 return ldbm_fetch( db->dbc_db, key );
350 rc = ldbm_store( db->dbc_db, key, data, flags );
364 rc = ldbm_delete( db->dbc_db, key );
370 ldbm_cache_sync_daemon(
374 Backend *be = (Backend *)be_ptr;
375 struct ldbminfo *li = (struct ldbminfo *) be->be_private;
378 LDAP_LOG ( CACHE, ARGS, "ldbm_cache_sync_daemon:"
379 " synchronizer starting for %s\n", li->li_directory, 0, 0 );
381 Debug( LDAP_DEBUG_ANY, "synchronizer starting for %s\n", li->li_directory, 0, 0 );
384 while (!li->li_dbshutdown) {
385 int i = li->li_dbsyncwaitn;
387 sleep( li->li_dbsyncfreq );
389 while (i && ldap_pvt_thread_pool_backload(&connection_pool) != 0) {
391 LDAP_LOG ( CACHE, DETAIL1, "ldbm_cache_sync_daemon:"
392 " delay syncing %s\n", li->li_directory, 0, 0 );
394 Debug( LDAP_DEBUG_TRACE, "delay syncing %s\n", li->li_directory, 0, 0 );
396 sleep(li->li_dbsyncwaitinterval);
400 if (!li->li_dbshutdown) {
402 LDAP_LOG ( CACHE, DETAIL1, "ldbm_cache_sync_daemon:"
403 " syncing %s\n", li->li_directory, 0, 0 );
405 Debug( LDAP_DEBUG_TRACE, "syncing %s\n", li->li_directory, 0, 0 );
407 ldbm_cache_sync( be );
412 LDAP_LOG ( CACHE, DETAIL1, "ldbm_cache_sync_daemon:"
413 " synchronizer stopping\n", 0, 0, 0);
415 Debug( LDAP_DEBUG_ANY, "synchronizer stopping\n", 0, 0, 0 );