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-2005 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"
40 struct ldbminfo *li = (struct ldbminfo *) be->be_private;
44 #ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
49 sprintf( buf, "%s%s", name, suffix );
51 sprintf( buf, "%s" LDAP_DIRSEP "%s%s",
52 li->li_directory, name, suffix );
54 if( li->li_dblocking ) {
55 flags |= LDBM_LOCKING;
57 flags |= LDBM_NOLOCKING;
60 if( li->li_dbwritesync ) {
66 Debug( LDAP_DEBUG_TRACE, "=> ldbm_cache_open( \"%s\", %d, %o )\n", buf,
72 ldap_pvt_thread_mutex_lock( &li->li_dbcache_mutex );
76 for ( i = 0; i < MAXDBCACHE; i++ ) {
77 /* see if this slot is free */
78 if ( li->li_dbcache[i].dbc_name == NULL) {
79 if (empty == MAXDBCACHE)
84 if ( strcmp( li->li_dbcache[i].dbc_name, buf ) == 0 ) {
85 /* already open - return it */
86 if (li->li_dbcache[i].dbc_flags != flags
87 && li->li_dbcache[i].dbc_refcnt == 0)
89 /* we don't want to use an open cache with different
90 * permissions (esp. if we need write but the open
91 * cache is read-only). So close this one if
92 * possible, and re-open below.
94 * FIXME: what about the case where the refcount
95 * is > 0? right now, we're using it anyway and
96 * just praying. Can there be more than one open
97 * cache to the same db?
99 * Also, it's really only necessary to compare the
100 * read-only flag, instead of all of the flags,
101 * but for now I'm checking all of them.
107 li->li_dbcache[i].dbc_refcnt++;
108 Debug( LDAP_DEBUG_TRACE,
109 "<= 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;
134 Debug( LDAP_DEBUG_ANY,
135 "ldbm_cache_open no unused db to close - waiting\n",
138 ldap_pvt_thread_cond_wait( &li->li_dbcache_cv,
139 &li->li_dbcache_mutex );
140 /* after waiting for a free slot, go back to square
141 * one: look for an open cache for this db, or an
142 * empty slot, or an unref'ed cache, or wait again.
146 } while (i == MAXDBCACHE);
148 if ( (li->li_dbcache[i].dbc_db = ldbm_open( li->li_dbenv, buf, flags, li->li_mode,
149 li->li_dbcachesize )) == NULL )
152 Debug( LDAP_DEBUG_TRACE,
153 "<= ldbm_cache_open NULL \"%s\" errno=%d reason=\"%s\")\n",
154 buf, err, err > -1 && err < sys_nerr ?
155 sys_errlist[err] : "unknown" );
157 ldap_pvt_thread_mutex_unlock( &li->li_dbcache_mutex );
160 li->li_dbcache[i].dbc_name = ch_strdup( buf );
161 li->li_dbcache[i].dbc_refcnt = 1;
162 li->li_dbcache[i].dbc_lastref = slap_get_time();
163 li->li_dbcache[i].dbc_flags = flags;
164 li->li_dbcache[i].dbc_dirty = 0;
165 #ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
166 if ( stat( buf, &st ) == 0 ) {
167 li->li_dbcache[i].dbc_blksize = st.st_blksize;
171 li->li_dbcache[i].dbc_blksize = DEFAULT_BLOCKSIZE;
173 li->li_dbcache[i].dbc_maxids = (li->li_dbcache[i].dbc_blksize /
174 sizeof(ID)) - ID_BLOCK_IDS_OFFSET;
175 li->li_dbcache[i].dbc_maxindirect = ( SLAPD_LDBM_MIN_MAXIDS /
176 li->li_dbcache[i].dbc_maxids ) + 1;
178 assert( li->li_dbcache[i].dbc_maxindirect < 256 );
180 Debug( LDAP_DEBUG_ARGS,
181 "ldbm_cache_open (blksize %ld) (maxids %d) (maxindirect %d)\n",
182 li->li_dbcache[i].dbc_blksize, li->li_dbcache[i].dbc_maxids,
183 li->li_dbcache[i].dbc_maxindirect );
185 Debug( LDAP_DEBUG_TRACE, "<= ldbm_cache_open (opened %d)\n", i, 0, 0 );
187 ldap_pvt_thread_mutex_init( &li->li_dbcache[i].dbc_write_mutex );
189 ldap_pvt_thread_mutex_unlock( &li->li_dbcache_mutex );
190 return( &li->li_dbcache[i] );
194 ldbm_cache_close( Backend *be, DBCache *db )
196 struct ldbminfo *li = (struct ldbminfo *) be->be_private;
198 if( li->li_dbwritesync && db->dbc_dirty ) {
199 ldbm_sync( db->dbc_db );
203 ldap_pvt_thread_mutex_lock( &li->li_dbcache_mutex );
204 if ( --db->dbc_refcnt <= 0 ) {
206 ldap_pvt_thread_cond_signal( &li->li_dbcache_cv );
208 ldap_pvt_thread_mutex_unlock( &li->li_dbcache_mutex );
212 ldbm_cache_really_close( Backend *be, DBCache *db )
214 struct ldbminfo *li = (struct ldbminfo *) be->be_private;
216 ldap_pvt_thread_mutex_lock( &li->li_dbcache_mutex );
217 if ( --db->dbc_refcnt <= 0 ) {
219 ldap_pvt_thread_cond_signal( &li->li_dbcache_cv );
220 ldbm_close( db->dbc_db );
221 free( db->dbc_name );
223 ldap_pvt_thread_mutex_destroy( &db->dbc_write_mutex );
225 ldap_pvt_thread_mutex_unlock( &li->li_dbcache_mutex );
229 ldbm_cache_flush_all( Backend *be )
231 struct ldbminfo *li = (struct ldbminfo *) be->be_private;
234 ldap_pvt_thread_mutex_lock( &li->li_dbcache_mutex );
235 for ( i = 0; i < MAXDBCACHE; i++ ) {
236 if ( li->li_dbcache[i].dbc_name != NULL ) {
237 Debug( LDAP_DEBUG_TRACE, "ldbm flushing db (%s)\n",
238 li->li_dbcache[i].dbc_name, 0, 0 );
240 ldbm_sync( li->li_dbcache[i].dbc_db );
241 li->li_dbcache[i].dbc_dirty = 0;
242 if ( li->li_dbcache[i].dbc_refcnt != 0 ) {
243 Debug( LDAP_DEBUG_TRACE,
244 "refcnt = %d, couldn't close db (%s)\n",
245 li->li_dbcache[i].dbc_refcnt,
246 li->li_dbcache[i].dbc_name, 0 );
249 Debug( LDAP_DEBUG_TRACE,
250 "ldbm closing db (%s)\n",
251 li->li_dbcache[i].dbc_name, 0, 0 );
253 ldap_pvt_thread_cond_signal( &li->li_dbcache_cv );
254 ldbm_close( li->li_dbcache[i].dbc_db );
255 free( li->li_dbcache[i].dbc_name );
256 li->li_dbcache[i].dbc_name = NULL;
260 ldap_pvt_thread_mutex_unlock( &li->li_dbcache_mutex );
264 ldbm_cache_sync( Backend *be )
266 struct ldbminfo *li = (struct ldbminfo *) be->be_private;
270 ldap_pvt_thread_mutex_lock( &li->li_dbcache_mutex );
271 for ( i = 0; i < MAXDBCACHE; i++ ) {
272 if ( li->li_dbcache[i].dbc_name != NULL && li->li_dbcache[i].dbc_dirty ) {
275 Debug( LDAP_DEBUG_TRACE, "syncing %s\n",
276 li->li_directory, 0, 0 );
278 Debug( LDAP_DEBUG_TRACE, "ldbm syncing db (%s)\n",
279 li->li_dbcache[i].dbc_name, 0, 0 );
280 ldbm_sync( li->li_dbcache[i].dbc_db );
281 li->li_dbcache[i].dbc_dirty = 0;
284 ldap_pvt_thread_mutex_unlock( &li->li_dbcache_mutex );
287 #if 0 /* macro in proto-back-ldbm.h */
294 return ldbm_fetch( db->dbc_db, key );
309 rc = ldbm_store( db->dbc_db, key, data, flags );
323 rc = ldbm_delete( db->dbc_db, key );
329 ldbm_cache_sync_daemon(
334 struct re_s *rtask = arg;
335 Backend *be = rtask->arg;
336 struct ldbminfo *li = (struct ldbminfo *) be->be_private;
338 /* If server is idle, or we've already waited the limit */
339 if ( li->li_dbsyncwaitcount == li->li_dbsyncwaitn ||
340 ldap_pvt_thread_pool_backload(&connection_pool) < 2 ) {
341 rtask->interval.tv_sec = li->li_dbsyncfreq;
342 li->li_dbsyncwaitcount = 0;
343 ldbm_cache_sync( be );
345 rtask->interval.tv_sec = li->li_dbsyncwaitinterval;
346 li->li_dbsyncwaitcount++;
347 Debug( LDAP_DEBUG_TRACE, "delay #%d syncing %s\n",
348 li->li_dbsyncwaitcount, li->li_directory, 0 );
351 ldap_pvt_thread_mutex_lock( &slapd_rq.rq_mutex );
352 ldap_pvt_runqueue_stoptask( &slapd_rq, rtask );
353 ldap_pvt_runqueue_resched( &slapd_rq, rtask, 0 );
354 ldap_pvt_thread_mutex_unlock( &slapd_rq.rq_mutex );