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"
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 ) {
65 Debug( LDAP_DEBUG_TRACE, "=> ldbm_cache_open( \"%s\", %d, %o )\n", buf,
71 ldap_pvt_thread_mutex_lock( &li->li_dbcache_mutex );
75 for ( i = 0; i < MAXDBCACHE; i++ ) {
76 /* see if this slot is free */
77 if ( li->li_dbcache[i].dbc_name == NULL) {
78 if (empty == MAXDBCACHE)
83 if ( strcmp( li->li_dbcache[i].dbc_name, buf ) == 0 ) {
84 /* already open - return it */
85 if (li->li_dbcache[i].dbc_flags != flags
86 && li->li_dbcache[i].dbc_refcnt == 0)
88 /* we don't want to use an open cache with different
89 * permissions (esp. if we need write but the open
90 * cache is read-only). So close this one if
91 * possible, and re-open below.
93 * FIXME: what about the case where the refcount
94 * is > 0? right now, we're using it anyway and
95 * just praying. Can there be more than one open
96 * cache to the same db?
98 * Also, it's really only necessary to compare the
99 * read-only flag, instead of all of the flags,
100 * but for now I'm checking all of them.
106 li->li_dbcache[i].dbc_refcnt++;
107 Debug( LDAP_DEBUG_TRACE,
108 "<= ldbm_cache_open (cache %d)\n", i, 0, 0 );
110 ldap_pvt_thread_mutex_unlock( &li->li_dbcache_mutex );
111 return( &li->li_dbcache[i] );
114 /* keep track of lru db */
115 if (( li->li_dbcache[i].dbc_refcnt == 0 ) &&
117 ( li->li_dbcache[i].dbc_lastref < oldtime )) )
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;
133 Debug( LDAP_DEBUG_ANY,
134 "ldbm_cache_open no unused db to close - waiting\n",
137 ldap_pvt_thread_cond_wait( &li->li_dbcache_cv,
138 &li->li_dbcache_mutex );
139 /* after waiting for a free slot, go back to square
140 * one: look for an open cache for this db, or an
141 * empty slot, or an unref'ed cache, or wait again.
145 } while (i == MAXDBCACHE);
147 if ( (li->li_dbcache[i].dbc_db = ldbm_open( li->li_dbenv, buf, flags, li->li_mode,
148 li->li_dbcachesize )) == NULL )
151 Debug( LDAP_DEBUG_TRACE,
152 "<= ldbm_cache_open NULL \"%s\" errno=%d reason=\"%s\")\n",
153 buf, err, err > -1 && err < sys_nerr ?
154 sys_errlist[err] : "unknown" );
156 ldap_pvt_thread_mutex_unlock( &li->li_dbcache_mutex );
159 li->li_dbcache[i].dbc_name = ch_strdup( buf );
160 li->li_dbcache[i].dbc_refcnt = 1;
161 li->li_dbcache[i].dbc_lastref = slap_get_time();
162 li->li_dbcache[i].dbc_flags = flags;
163 li->li_dbcache[i].dbc_dirty = 0;
164 #ifdef HAVE_ST_BLKSIZE
165 if ( stat( buf, &st ) == 0 ) {
166 li->li_dbcache[i].dbc_blksize = st.st_blksize;
170 li->li_dbcache[i].dbc_blksize = DEFAULT_BLOCKSIZE;
172 li->li_dbcache[i].dbc_maxids = (li->li_dbcache[i].dbc_blksize /
173 sizeof(ID)) - ID_BLOCK_IDS_OFFSET;
174 li->li_dbcache[i].dbc_maxindirect = ( SLAPD_LDBM_MIN_MAXIDS /
175 li->li_dbcache[i].dbc_maxids ) + 1;
177 assert( li->li_dbcache[i].dbc_maxindirect < 256 );
179 Debug( LDAP_DEBUG_ARGS,
180 "ldbm_cache_open (blksize %ld) (maxids %d) (maxindirect %d)\n",
181 li->li_dbcache[i].dbc_blksize, li->li_dbcache[i].dbc_maxids,
182 li->li_dbcache[i].dbc_maxindirect );
184 Debug( LDAP_DEBUG_TRACE, "<= ldbm_cache_open (opened %d)\n", i, 0, 0 );
186 ldap_pvt_thread_mutex_init( &li->li_dbcache[i].dbc_write_mutex );
188 ldap_pvt_thread_mutex_unlock( &li->li_dbcache_mutex );
189 return( &li->li_dbcache[i] );
193 ldbm_cache_close( Backend *be, DBCache *db )
195 struct ldbminfo *li = (struct ldbminfo *) be->be_private;
197 if( li->li_dbwritesync && db->dbc_dirty ) {
198 ldbm_sync( db->dbc_db );
202 ldap_pvt_thread_mutex_lock( &li->li_dbcache_mutex );
203 if ( --db->dbc_refcnt <= 0 ) {
205 ldap_pvt_thread_cond_signal( &li->li_dbcache_cv );
207 ldap_pvt_thread_mutex_unlock( &li->li_dbcache_mutex );
211 ldbm_cache_really_close( Backend *be, DBCache *db )
213 struct ldbminfo *li = (struct ldbminfo *) be->be_private;
215 ldap_pvt_thread_mutex_lock( &li->li_dbcache_mutex );
216 if ( --db->dbc_refcnt <= 0 ) {
218 ldap_pvt_thread_cond_signal( &li->li_dbcache_cv );
219 ldbm_close( db->dbc_db );
220 free( db->dbc_name );
222 ldap_pvt_thread_mutex_destroy( &db->dbc_write_mutex );
224 ldap_pvt_thread_mutex_unlock( &li->li_dbcache_mutex );
228 ldbm_cache_flush_all( Backend *be )
230 struct ldbminfo *li = (struct ldbminfo *) be->be_private;
233 ldap_pvt_thread_mutex_lock( &li->li_dbcache_mutex );
234 for ( i = 0; i < MAXDBCACHE; i++ ) {
235 if ( li->li_dbcache[i].dbc_name != NULL ) {
236 Debug( LDAP_DEBUG_TRACE, "ldbm flushing db (%s)\n",
237 li->li_dbcache[i].dbc_name, 0, 0 );
239 ldbm_sync( li->li_dbcache[i].dbc_db );
240 li->li_dbcache[i].dbc_dirty = 0;
241 if ( li->li_dbcache[i].dbc_refcnt != 0 ) {
242 Debug( LDAP_DEBUG_TRACE,
243 "refcnt = %d, couldn't close db (%s)\n",
244 li->li_dbcache[i].dbc_refcnt,
245 li->li_dbcache[i].dbc_name, 0 );
248 Debug( LDAP_DEBUG_TRACE,
249 "ldbm closing db (%s)\n",
250 li->li_dbcache[i].dbc_name, 0, 0 );
252 ldap_pvt_thread_cond_signal( &li->li_dbcache_cv );
253 ldbm_close( li->li_dbcache[i].dbc_db );
254 free( li->li_dbcache[i].dbc_name );
255 li->li_dbcache[i].dbc_name = NULL;
259 ldap_pvt_thread_mutex_unlock( &li->li_dbcache_mutex );
263 ldbm_cache_sync( Backend *be )
265 struct ldbminfo *li = (struct ldbminfo *) be->be_private;
268 ldap_pvt_thread_mutex_lock( &li->li_dbcache_mutex );
269 for ( i = 0; i < MAXDBCACHE; i++ ) {
270 if ( li->li_dbcache[i].dbc_name != NULL && li->li_dbcache[i].dbc_dirty ) {
271 Debug( LDAP_DEBUG_TRACE, "ldbm syncing db (%s)\n",
272 li->li_dbcache[i].dbc_name, 0, 0 );
273 ldbm_sync( li->li_dbcache[i].dbc_db );
274 li->li_dbcache[i].dbc_dirty = 0;
277 ldap_pvt_thread_mutex_unlock( &li->li_dbcache_mutex );
280 #if 0 /* macro in proto-back-ldbm.h */
287 return ldbm_fetch( db->dbc_db, key );
302 rc = ldbm_store( db->dbc_db, key, data, flags );
316 rc = ldbm_delete( db->dbc_db, key );
322 ldbm_cache_sync_daemon(
326 Backend *be = (Backend *)be_ptr;
327 struct ldbminfo *li = (struct ldbminfo *) be->be_private;
329 Debug( LDAP_DEBUG_ANY, "synchronizer starting for %s\n", li->li_directory, 0, 0 );
331 while (!li->li_dbshutdown) {
332 int i = li->li_dbsyncwaitn;
334 sleep( li->li_dbsyncfreq );
336 while (i && ldap_pvt_thread_pool_backload(&connection_pool) != 0) {
337 Debug( LDAP_DEBUG_TRACE, "delay syncing %s\n", li->li_directory, 0, 0 );
338 sleep(li->li_dbsyncwaitinterval);
342 if (!li->li_dbshutdown) {
343 Debug( LDAP_DEBUG_TRACE, "syncing %s\n", li->li_directory, 0, 0 );
344 ldbm_cache_sync( be );
348 Debug( LDAP_DEBUG_ANY, "synchronizer stopping\n", 0, 0, 0 );