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
37 sprintf( buf, "%s" LDAP_DIRSEP "%s%s",
38 li->li_directory, name, suffix );
40 if( li->li_dblocking ) {
41 flags |= LDBM_LOCKING;
43 flags |= LDBM_NOLOCKING;
46 if( li->li_dbwritesync ) {
52 Debug( LDAP_DEBUG_TRACE, "=> ldbm_cache_open( \"%s\", %d, %o )\n", buf,
55 curtime = slap_get_time();
58 ldap_pvt_thread_mutex_lock( &li->li_dbcache_mutex );
62 for ( i = 0; i < MAXDBCACHE; i++ ) {
63 /* see if this slot is free */
64 if ( li->li_dbcache[i].dbc_name == NULL) {
65 if (empty == MAXDBCACHE)
70 if ( strcmp( li->li_dbcache[i].dbc_name, buf ) == 0 ) {
71 /* already open - return it */
72 if (li->li_dbcache[i].dbc_flags != flags
73 && li->li_dbcache[i].dbc_refcnt == 0)
75 /* we don't want to use an open cache with different
76 * permissions (esp. if we need write but the open
77 * cache is read-only). So close this one if
78 * possible, and re-open below.
80 * FIXME: what about the case where the refcount
81 * is > 0? right now, we're using it anyway and
82 * just praying. Can there be more than one open
83 * cache to the same db?
85 * Also, it's really only necessary to compare the
86 * read-only flag, instead of all of the flags,
87 * but for now I'm checking all of them.
93 li->li_dbcache[i].dbc_refcnt++;
94 Debug( LDAP_DEBUG_TRACE,
95 "<= ldbm_cache_open (cache %d)\n", i, 0, 0 );
96 ldap_pvt_thread_mutex_unlock( &li->li_dbcache_mutex );
97 return( &li->li_dbcache[i] );
100 /* keep track of lru db */
101 if ( li->li_dbcache[i].dbc_lastref < oldtime
102 && li->li_dbcache[i].dbc_refcnt == 0 )
105 oldtime = li->li_dbcache[i].dbc_lastref;
110 if ( i == MAXDBCACHE ) {
111 /* no empty slots, not already open - close lru and use that slot */
112 if ( li->li_dbcache[lru].dbc_refcnt == 0 ) {
114 ldbm_close( li->li_dbcache[i].dbc_db );
115 free( li->li_dbcache[i].dbc_name );
116 li->li_dbcache[i].dbc_name = NULL;
118 Debug( LDAP_DEBUG_ANY,
119 "ldbm_cache_open no unused db to close - waiting\n",
121 ldap_pvt_thread_cond_wait( &li->li_dbcache_cv,
122 &li->li_dbcache_mutex );
123 /* after waiting for a free slot, go back to square
124 * one: look for an open cache for this db, or an
125 * empty slot, or an unref'ed cache, or wait again.
129 } while (i == MAXDBCACHE);
131 if ( (li->li_dbcache[i].dbc_db = ldbm_open( buf, flags, li->li_mode,
132 li->li_dbcachesize )) == NULL )
135 Debug( LDAP_DEBUG_TRACE,
136 "<= ldbm_cache_open NULL \"%s\" errno=%d reason=\"%s\")\n",
137 buf, err, err > -1 && err < sys_nerr ?
138 sys_errlist[err] : "unknown" );
139 ldap_pvt_thread_mutex_unlock( &li->li_dbcache_mutex );
142 li->li_dbcache[i].dbc_name = ch_strdup( buf );
143 li->li_dbcache[i].dbc_refcnt = 1;
144 li->li_dbcache[i].dbc_lastref = curtime;
145 li->li_dbcache[i].dbc_flags = flags;
146 li->li_dbcache[i].dbc_dirty = 0;
147 #ifdef HAVE_ST_BLKSIZE
148 if ( stat( buf, &st ) == 0 ) {
149 li->li_dbcache[i].dbc_blksize = st.st_blksize;
153 li->li_dbcache[i].dbc_blksize = DEFAULT_BLOCKSIZE;
155 li->li_dbcache[i].dbc_maxids = (li->li_dbcache[i].dbc_blksize /
156 sizeof(ID)) - ID_BLOCK_IDS_OFFSET;
157 li->li_dbcache[i].dbc_maxindirect = ( SLAPD_LDBM_MIN_MAXIDS /
158 li->li_dbcache[i].dbc_maxids ) + 1;
160 assert( li->li_dbcache[i].dbc_maxindirect < 256 );
162 Debug( LDAP_DEBUG_ARGS,
163 "ldbm_cache_open (blksize %ld) (maxids %d) (maxindirect %d)\n",
164 li->li_dbcache[i].dbc_blksize, li->li_dbcache[i].dbc_maxids,
165 li->li_dbcache[i].dbc_maxindirect );
166 Debug( LDAP_DEBUG_TRACE, "<= ldbm_cache_open (opened %d)\n", i, 0, 0 );
167 ldap_pvt_thread_mutex_unlock( &li->li_dbcache_mutex );
168 return( &li->li_dbcache[i] );
172 ldbm_cache_close( Backend *be, DBCache *db )
174 struct ldbminfo *li = (struct ldbminfo *) be->be_private;
176 if( li->li_dbwritesync && db->dbc_dirty ) {
177 ldbm_sync( db->dbc_db );
181 ldap_pvt_thread_mutex_lock( &li->li_dbcache_mutex );
182 if ( --db->dbc_refcnt <= 0 ) {
184 ldap_pvt_thread_cond_signal( &li->li_dbcache_cv );
186 ldap_pvt_thread_mutex_unlock( &li->li_dbcache_mutex );
190 ldbm_cache_really_close( Backend *be, DBCache *db )
192 struct ldbminfo *li = (struct ldbminfo *) be->be_private;
194 ldap_pvt_thread_mutex_lock( &li->li_dbcache_mutex );
195 if ( --db->dbc_refcnt <= 0 ) {
197 ldap_pvt_thread_cond_signal( &li->li_dbcache_cv );
198 ldbm_close( db->dbc_db );
199 free( db->dbc_name );
202 ldap_pvt_thread_mutex_unlock( &li->li_dbcache_mutex );
206 ldbm_cache_flush_all( Backend *be )
208 struct ldbminfo *li = (struct ldbminfo *) be->be_private;
211 ldap_pvt_thread_mutex_lock( &li->li_dbcache_mutex );
212 for ( i = 0; i < MAXDBCACHE; i++ ) {
213 if ( li->li_dbcache[i].dbc_name != NULL ) {
214 Debug( LDAP_DEBUG_TRACE, "ldbm flushing db (%s)\n",
215 li->li_dbcache[i].dbc_name, 0, 0 );
216 ldbm_sync( li->li_dbcache[i].dbc_db );
217 li->li_dbcache[i].dbc_dirty = 0;
218 if ( li->li_dbcache[i].dbc_refcnt != 0 ) {
219 Debug( LDAP_DEBUG_TRACE,
220 "refcnt = %d, couldn't close db (%s)\n",
221 li->li_dbcache[i].dbc_refcnt,
222 li->li_dbcache[i].dbc_name, 0 );
224 Debug( LDAP_DEBUG_TRACE,
225 "ldbm closing db (%s)\n",
226 li->li_dbcache[i].dbc_name, 0, 0 );
227 ldap_pvt_thread_cond_signal( &li->li_dbcache_cv );
228 ldbm_close( li->li_dbcache[i].dbc_db );
229 free( li->li_dbcache[i].dbc_name );
230 li->li_dbcache[i].dbc_name = NULL;
234 ldap_pvt_thread_mutex_unlock( &li->li_dbcache_mutex );
245 return ldbm_fetch( db->dbc_db, key );
261 Statslog( LDAP_DEBUG_STATS,
262 "=> ldbm_cache_store(): key.dptr=%s, key.dsize=%d\n",
263 key.dptr, key.dsize, 0, 0, 0 );
265 Statslog( LDAP_DEBUG_STATS,
266 "=> ldbm_cache_store(): key.dptr=0x%08x, data.dptr=0x%0 8x\n",
267 key.dptr, data.dptr, 0, 0, 0 );
269 Statslog( LDAP_DEBUG_STATS,
270 "=> ldbm_cache_store(): data.dptr=%s, data.dsize=%d\n",
271 data.dptr, data.dsize, 0, 0, 0 );
273 Statslog( LDAP_DEBUG_STATS,
274 "=> ldbm_cache_store(): flags=0x%08x\n",
276 #endif /* LDBM_DEBUG */
279 rc = ldbm_store( db->dbc_db, key, data, flags );
293 rc = ldbm_delete( db->dbc_db, key );