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) {
69 if ( strcmp( li->li_dbcache[i].dbc_name, buf ) == 0 ) {
70 /* already open - return it */
71 if (li->li_dbcache[i].dbc_flags != flags
72 && li->li_dbcache[i].dbc_refcnt == 0)
74 /* we don't want to use an open cache with different
75 * permissions (esp. if we need write but the open
76 * cache is read-only). So close this one if
77 * possible, and re-open below.
79 * FIXME: what about the case where the refcount
80 * is > 0? right now, we're using it anyway and
81 * just praying. Can there be more than one open
82 * cache to the same db?
84 * Also, it's really only necessary to compare the
85 * read-only flag, instead of all of the flags,
86 * but for now I'm checking all of them.
92 li->li_dbcache[i].dbc_refcnt++;
93 Debug( LDAP_DEBUG_TRACE,
94 "<= ldbm_cache_open (cache %d)\n", i, 0, 0 );
95 ldap_pvt_thread_mutex_unlock( &li->li_dbcache_mutex );
96 return( &li->li_dbcache[i] );
99 /* keep track of lru db */
100 if ( li->li_dbcache[i].dbc_lastref < oldtime
101 && li->li_dbcache[i].dbc_refcnt == 0 )
104 oldtime = li->li_dbcache[i].dbc_lastref;
109 if ( i == MAXDBCACHE ) {
110 /* no empty slots, not already open - close lru and use that slot */
111 if ( li->li_dbcache[lru].dbc_refcnt == 0 ) {
113 ldbm_close( li->li_dbcache[i].dbc_db );
114 free( li->li_dbcache[i].dbc_name );
115 li->li_dbcache[i].dbc_name = NULL;
117 Debug( LDAP_DEBUG_ANY,
118 "ldbm_cache_open no unused db to close - waiting\n",
120 ldap_pvt_thread_cond_wait( &li->li_dbcache_cv,
121 &li->li_dbcache_mutex );
122 /* after waiting for a free slot, go back to square
123 * one: look for an open cache for this db, or an
124 * empty slot, or an unref'ed cache, or wait again.
128 } while (i == MAXDBCACHE);
130 if ( (li->li_dbcache[i].dbc_db = ldbm_open( buf, flags, li->li_mode,
131 li->li_dbcachesize )) == NULL )
134 Debug( LDAP_DEBUG_TRACE,
135 "<= ldbm_cache_open NULL \"%s\" errno=%d reason=\"%s\")\n",
136 buf, err, err > -1 && err < sys_nerr ?
137 sys_errlist[err] : "unknown" );
138 ldap_pvt_thread_mutex_unlock( &li->li_dbcache_mutex );
141 li->li_dbcache[i].dbc_name = ch_strdup( buf );
142 li->li_dbcache[i].dbc_refcnt = 1;
143 li->li_dbcache[i].dbc_lastref = curtime;
144 li->li_dbcache[i].dbc_flags = flags;
145 li->li_dbcache[i].dbc_dirty = 0;
146 #ifdef HAVE_ST_BLKSIZE
147 if ( stat( buf, &st ) == 0 ) {
148 li->li_dbcache[i].dbc_blksize = st.st_blksize;
152 li->li_dbcache[i].dbc_blksize = DEFAULT_BLOCKSIZE;
154 li->li_dbcache[i].dbc_maxids = (li->li_dbcache[i].dbc_blksize /
155 sizeof(ID)) - ID_BLOCK_IDS_OFFSET;
156 li->li_dbcache[i].dbc_maxindirect = ( SLAPD_LDBM_MIN_MAXIDS /
157 li->li_dbcache[i].dbc_maxids ) + 1;
159 assert( li->li_dbcache[i].dbc_maxindirect < 256 );
161 Debug( LDAP_DEBUG_ARGS,
162 "ldbm_cache_open (blksize %ld) (maxids %d) (maxindirect %d)\n",
163 li->li_dbcache[i].dbc_blksize, li->li_dbcache[i].dbc_maxids,
164 li->li_dbcache[i].dbc_maxindirect );
165 Debug( LDAP_DEBUG_TRACE, "<= ldbm_cache_open (opened %d)\n", i, 0, 0 );
166 ldap_pvt_thread_mutex_unlock( &li->li_dbcache_mutex );
167 return( &li->li_dbcache[i] );
171 ldbm_cache_close( Backend *be, DBCache *db )
173 struct ldbminfo *li = (struct ldbminfo *) be->be_private;
175 if( li->li_dbwritesync && db->dbc_dirty ) {
176 ldbm_sync( db->dbc_db );
180 ldap_pvt_thread_mutex_lock( &li->li_dbcache_mutex );
181 if ( --db->dbc_refcnt <= 0 ) {
183 ldap_pvt_thread_cond_signal( &li->li_dbcache_cv );
185 ldap_pvt_thread_mutex_unlock( &li->li_dbcache_mutex );
189 ldbm_cache_really_close( Backend *be, DBCache *db )
191 struct ldbminfo *li = (struct ldbminfo *) be->be_private;
193 ldap_pvt_thread_mutex_lock( &li->li_dbcache_mutex );
194 if ( --db->dbc_refcnt <= 0 ) {
196 ldap_pvt_thread_cond_signal( &li->li_dbcache_cv );
197 ldbm_close( db->dbc_db );
198 free( db->dbc_name );
201 ldap_pvt_thread_mutex_unlock( &li->li_dbcache_mutex );
205 ldbm_cache_flush_all( Backend *be )
207 struct ldbminfo *li = (struct ldbminfo *) be->be_private;
210 ldap_pvt_thread_mutex_lock( &li->li_dbcache_mutex );
211 for ( i = 0; i < MAXDBCACHE; i++ ) {
212 if ( li->li_dbcache[i].dbc_name != NULL ) {
213 Debug( LDAP_DEBUG_TRACE, "ldbm flushing db (%s)\n",
214 li->li_dbcache[i].dbc_name, 0, 0 );
215 ldbm_sync( li->li_dbcache[i].dbc_db );
216 li->li_dbcache[i].dbc_dirty = 0;
217 if ( li->li_dbcache[i].dbc_refcnt != 0 ) {
218 Debug( LDAP_DEBUG_TRACE,
219 "refcnt = %d, couldn't close db (%s)\n",
220 li->li_dbcache[i].dbc_refcnt,
221 li->li_dbcache[i].dbc_name, 0 );
223 Debug( LDAP_DEBUG_TRACE,
224 "ldbm closing db (%s)\n",
225 li->li_dbcache[i].dbc_name, 0, 0 );
226 ldap_pvt_thread_cond_signal( &li->li_dbcache_cv );
227 ldbm_close( li->li_dbcache[i].dbc_db );
228 free( li->li_dbcache[i].dbc_name );
229 li->li_dbcache[i].dbc_name = NULL;
233 ldap_pvt_thread_mutex_unlock( &li->li_dbcache_mutex );
244 return ldbm_fetch( db->dbc_db, key );
260 Statslog( LDAP_DEBUG_STATS,
261 "=> ldbm_cache_store(): key.dptr=%s, key.dsize=%d\n",
262 key.dptr, key.dsize, 0, 0, 0 );
264 Statslog( LDAP_DEBUG_STATS,
265 "=> ldbm_cache_store(): key.dptr=0x%08x, data.dptr=0x%0 8x\n",
266 key.dptr, data.dptr, 0, 0, 0 );
268 Statslog( LDAP_DEBUG_STATS,
269 "=> ldbm_cache_store(): data.dptr=%s, data.dsize=%d\n",
270 data.dptr, data.dsize, 0, 0, 0 );
272 Statslog( LDAP_DEBUG_STATS,
273 "=> ldbm_cache_store(): flags=0x%08x\n",
275 #endif /* LDBM_DEBUG */
278 rc = ldbm_store( db->dbc_db, key, data, flags );
292 rc = ldbm_delete( db->dbc_db, key );