1 /* ldbmcache.c - maintain a cache of open ldbm files */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 1998-2004 The OpenLDAP Foundation.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted only as authorized by the OpenLDAP
12 * A copy of this license is available in the file LICENSE in the
13 * top-level directory of the distribution or, alternatively, at
14 * <http://www.OpenLDAP.org/license.html>.
22 #include <ac/socket.h>
23 #include <ac/string.h>
25 #include <ac/unistd.h>
29 #include "back-ldbm.h"
39 struct ldbminfo *li = (struct ldbminfo *) be->be_private;
43 #ifdef HAVE_ST_BLKSIZE
48 sprintf( buf, "%s%s", name, suffix );
50 sprintf( buf, "%s" LDAP_DIRSEP "%s%s",
51 li->li_directory, name, suffix );
53 if( li->li_dblocking ) {
54 flags |= LDBM_LOCKING;
56 flags |= LDBM_NOLOCKING;
59 if( li->li_dbwritesync ) {
66 LDAP_LOG( CACHE, ENTRY,
67 "ldbm_cache_open: \"%s\", %d, %o\n", buf, flags, li->li_mode );
69 Debug( LDAP_DEBUG_TRACE, "=> ldbm_cache_open( \"%s\", %d, %o )\n", buf,
76 ldap_pvt_thread_mutex_lock( &li->li_dbcache_mutex );
80 for ( i = 0; i < MAXDBCACHE; i++ ) {
81 /* see if this slot is free */
82 if ( li->li_dbcache[i].dbc_name == NULL) {
83 if (empty == MAXDBCACHE)
88 if ( strcmp( li->li_dbcache[i].dbc_name, buf ) == 0 ) {
89 /* already open - return it */
90 if (li->li_dbcache[i].dbc_flags != flags
91 && li->li_dbcache[i].dbc_refcnt == 0)
93 /* we don't want to use an open cache with different
94 * permissions (esp. if we need write but the open
95 * cache is read-only). So close this one if
96 * possible, and re-open below.
98 * FIXME: what about the case where the refcount
99 * is > 0? right now, we're using it anyway and
100 * just praying. Can there be more than one open
101 * cache to the same db?
103 * Also, it's really only necessary to compare the
104 * read-only flag, instead of all of the flags,
105 * but for now I'm checking all of them.
111 li->li_dbcache[i].dbc_refcnt++;
113 LDAP_LOG( CACHE, DETAIL1,
114 "ldbm_cache_open: cache %d\n", i, 0, 0 );
116 Debug( LDAP_DEBUG_TRACE,
117 "<= ldbm_cache_open (cache %d)\n", i, 0, 0 );
120 ldap_pvt_thread_mutex_unlock( &li->li_dbcache_mutex );
121 return( &li->li_dbcache[i] );
124 /* keep track of lru db */
125 if (( li->li_dbcache[i].dbc_refcnt == 0 ) &&
127 ( li->li_dbcache[i].dbc_lastref < oldtime )) )
130 oldtime = li->li_dbcache[i].dbc_lastref;
135 if ( i == MAXDBCACHE ) {
136 /* no empty slots, not already open - close lru and use that slot */
137 if ( li->li_dbcache[lru].dbc_refcnt == 0 ) {
139 ldbm_close( li->li_dbcache[i].dbc_db );
140 free( li->li_dbcache[i].dbc_name );
141 li->li_dbcache[i].dbc_name = NULL;
144 LDAP_LOG( CACHE, INFO,
145 "ldbm_cache_open: no unused db to close - waiting\n",
148 Debug( LDAP_DEBUG_ANY,
149 "ldbm_cache_open no unused db to close - waiting\n",
153 ldap_pvt_thread_cond_wait( &li->li_dbcache_cv,
154 &li->li_dbcache_mutex );
155 /* after waiting for a free slot, go back to square
156 * one: look for an open cache for this db, or an
157 * empty slot, or an unref'ed cache, or wait again.
161 } while (i == MAXDBCACHE);
163 if ( (li->li_dbcache[i].dbc_db = ldbm_open( li->li_dbenv, buf, flags, li->li_mode,
164 li->li_dbcachesize )) == NULL )
168 LDAP_LOG( CACHE, ERR,
169 "ldbm_cache_open: \"%s\" failed, errono=%d, reason=%s\n",
170 buf, err, err > -1 && err < sys_nerr ? sys_errlist[err] :
173 Debug( LDAP_DEBUG_TRACE,
174 "<= ldbm_cache_open NULL \"%s\" errno=%d reason=\"%s\")\n",
175 buf, err, err > -1 && err < sys_nerr ?
176 sys_errlist[err] : "unknown" );
179 ldap_pvt_thread_mutex_unlock( &li->li_dbcache_mutex );
182 li->li_dbcache[i].dbc_name = ch_strdup( buf );
183 li->li_dbcache[i].dbc_refcnt = 1;
184 li->li_dbcache[i].dbc_lastref = slap_get_time();
185 li->li_dbcache[i].dbc_flags = flags;
186 li->li_dbcache[i].dbc_dirty = 0;
187 #ifdef HAVE_ST_BLKSIZE
188 if ( stat( buf, &st ) == 0 ) {
189 li->li_dbcache[i].dbc_blksize = st.st_blksize;
193 li->li_dbcache[i].dbc_blksize = DEFAULT_BLOCKSIZE;
195 li->li_dbcache[i].dbc_maxids = (li->li_dbcache[i].dbc_blksize /
196 sizeof(ID)) - ID_BLOCK_IDS_OFFSET;
197 li->li_dbcache[i].dbc_maxindirect = ( SLAPD_LDBM_MIN_MAXIDS /
198 li->li_dbcache[i].dbc_maxids ) + 1;
200 assert( li->li_dbcache[i].dbc_maxindirect < 256 );
203 LDAP_LOG( CACHE, ARGS,
204 "ldbm_cache_open: blksize:%ld maxids:%d maxindirect:%d\n",
205 li->li_dbcache[i].dbc_blksize, li->li_dbcache[i].dbc_maxids,
206 li->li_dbcache[i].dbc_maxindirect );
208 Debug( LDAP_DEBUG_ARGS,
209 "ldbm_cache_open (blksize %ld) (maxids %d) (maxindirect %d)\n",
210 li->li_dbcache[i].dbc_blksize, li->li_dbcache[i].dbc_maxids,
211 li->li_dbcache[i].dbc_maxindirect );
215 LDAP_LOG( CACHE, DETAIL1, "<= ldbm_cache_open: (opened %d)\n", i, 0, 0 );
217 Debug( LDAP_DEBUG_TRACE, "<= ldbm_cache_open (opened %d)\n", i, 0, 0 );
220 ldap_pvt_thread_mutex_init( &li->li_dbcache[i].dbc_write_mutex );
222 ldap_pvt_thread_mutex_unlock( &li->li_dbcache_mutex );
223 return( &li->li_dbcache[i] );
227 ldbm_cache_close( Backend *be, DBCache *db )
229 struct ldbminfo *li = (struct ldbminfo *) be->be_private;
231 if( li->li_dbwritesync && db->dbc_dirty ) {
232 ldbm_sync( db->dbc_db );
236 ldap_pvt_thread_mutex_lock( &li->li_dbcache_mutex );
237 if ( --db->dbc_refcnt <= 0 ) {
239 ldap_pvt_thread_cond_signal( &li->li_dbcache_cv );
241 ldap_pvt_thread_mutex_unlock( &li->li_dbcache_mutex );
245 ldbm_cache_really_close( Backend *be, DBCache *db )
247 struct ldbminfo *li = (struct ldbminfo *) be->be_private;
249 ldap_pvt_thread_mutex_lock( &li->li_dbcache_mutex );
250 if ( --db->dbc_refcnt <= 0 ) {
252 ldap_pvt_thread_cond_signal( &li->li_dbcache_cv );
253 ldbm_close( db->dbc_db );
254 free( db->dbc_name );
256 ldap_pvt_thread_mutex_destroy( &db->dbc_write_mutex );
258 ldap_pvt_thread_mutex_unlock( &li->li_dbcache_mutex );
262 ldbm_cache_flush_all( Backend *be )
264 struct ldbminfo *li = (struct ldbminfo *) be->be_private;
267 ldap_pvt_thread_mutex_lock( &li->li_dbcache_mutex );
268 for ( i = 0; i < MAXDBCACHE; i++ ) {
269 if ( li->li_dbcache[i].dbc_name != NULL ) {
271 LDAP_LOG( CACHE, DETAIL1,
272 "ldbm_cache_flush_all: flushing db (%s)\n",
273 li->li_dbcache[i].dbc_name, 0, 0 );
275 Debug( LDAP_DEBUG_TRACE, "ldbm flushing db (%s)\n",
276 li->li_dbcache[i].dbc_name, 0, 0 );
279 ldbm_sync( li->li_dbcache[i].dbc_db );
280 li->li_dbcache[i].dbc_dirty = 0;
281 if ( li->li_dbcache[i].dbc_refcnt != 0 ) {
283 LDAP_LOG( CACHE, INFO,
284 "ldbm_cache_flush_all: couldn't close db (%s), refcnt=%d\n",
285 li->li_dbcache[i].dbc_name, li->li_dbcache[i].dbc_refcnt,0);
287 Debug( LDAP_DEBUG_TRACE,
288 "refcnt = %d, couldn't close db (%s)\n",
289 li->li_dbcache[i].dbc_refcnt,
290 li->li_dbcache[i].dbc_name, 0 );
295 LDAP_LOG( CACHE, DETAIL1,
296 "ldbm_cache_flush_all: ldbm closing db (%s)\n",
297 li->li_dbcache[i].dbc_name, 0, 0 );
299 Debug( LDAP_DEBUG_TRACE,
300 "ldbm closing db (%s)\n",
301 li->li_dbcache[i].dbc_name, 0, 0 );
304 ldap_pvt_thread_cond_signal( &li->li_dbcache_cv );
305 ldbm_close( li->li_dbcache[i].dbc_db );
306 free( li->li_dbcache[i].dbc_name );
307 li->li_dbcache[i].dbc_name = NULL;
311 ldap_pvt_thread_mutex_unlock( &li->li_dbcache_mutex );
315 ldbm_cache_sync( Backend *be )
317 struct ldbminfo *li = (struct ldbminfo *) be->be_private;
320 ldap_pvt_thread_mutex_lock( &li->li_dbcache_mutex );
321 for ( i = 0; i < MAXDBCACHE; i++ ) {
322 if ( li->li_dbcache[i].dbc_name != NULL && li->li_dbcache[i].dbc_dirty ) {
324 LDAP_LOG ( CACHE, DETAIL1, "ldbm_cache_sync: "
325 "ldbm syncing db (%s)\n", li->li_dbcache[i].dbc_name, 0, 0 );
327 Debug( LDAP_DEBUG_TRACE, "ldbm syncing db (%s)\n",
328 li->li_dbcache[i].dbc_name, 0, 0 );
330 ldbm_sync( li->li_dbcache[i].dbc_db );
331 li->li_dbcache[i].dbc_dirty = 0;
334 ldap_pvt_thread_mutex_unlock( &li->li_dbcache_mutex );
337 #if 0 /* macro in proto-back-ldbm.h */
344 return ldbm_fetch( db->dbc_db, key );
359 rc = ldbm_store( db->dbc_db, key, data, flags );
373 rc = ldbm_delete( db->dbc_db, key );
379 ldbm_cache_sync_daemon(
383 Backend *be = (Backend *)be_ptr;
384 struct ldbminfo *li = (struct ldbminfo *) be->be_private;
387 LDAP_LOG ( CACHE, ARGS, "ldbm_cache_sync_daemon:"
388 " synchronizer starting for %s\n", li->li_directory, 0, 0 );
390 Debug( LDAP_DEBUG_ANY, "synchronizer starting for %s\n", li->li_directory, 0, 0 );
393 while (!li->li_dbshutdown) {
394 int i = li->li_dbsyncwaitn;
396 sleep( li->li_dbsyncfreq );
398 while (i && ldap_pvt_thread_pool_backload(&connection_pool) != 0) {
400 LDAP_LOG ( CACHE, DETAIL1, "ldbm_cache_sync_daemon:"
401 " delay syncing %s\n", li->li_directory, 0, 0 );
403 Debug( LDAP_DEBUG_TRACE, "delay syncing %s\n", li->li_directory, 0, 0 );
405 sleep(li->li_dbsyncwaitinterval);
409 if (!li->li_dbshutdown) {
411 LDAP_LOG ( CACHE, DETAIL1, "ldbm_cache_sync_daemon:"
412 " syncing %s\n", li->li_directory, 0, 0 );
414 Debug( LDAP_DEBUG_TRACE, "syncing %s\n", li->li_directory, 0, 0 );
416 ldbm_cache_sync( be );
421 LDAP_LOG ( CACHE, DETAIL1, "ldbm_cache_sync_daemon:"
422 " synchronizer stopping\n", 0, 0, 0);
424 Debug( LDAP_DEBUG_ANY, "synchronizer stopping\n", 0, 0, 0 );