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 ) {
53 LDAP_LOG(( "cache", LDAP_LEVEL_ENTRY,
54 "ldbm_cache_open: \"%s\", %d, %o\n", buf, flags, li->li_mode ));
56 Debug( LDAP_DEBUG_TRACE, "=> ldbm_cache_open( \"%s\", %d, %o )\n", buf,
61 curtime = slap_get_time();
64 ldap_pvt_thread_mutex_lock( &li->li_dbcache_mutex );
68 for ( i = 0; i < MAXDBCACHE; i++ ) {
69 /* see if this slot is free */
70 if ( li->li_dbcache[i].dbc_name == NULL) {
71 if (empty == MAXDBCACHE)
76 if ( strcmp( li->li_dbcache[i].dbc_name, buf ) == 0 ) {
77 /* already open - return it */
78 if (li->li_dbcache[i].dbc_flags != flags
79 && li->li_dbcache[i].dbc_refcnt == 0)
81 /* we don't want to use an open cache with different
82 * permissions (esp. if we need write but the open
83 * cache is read-only). So close this one if
84 * possible, and re-open below.
86 * FIXME: what about the case where the refcount
87 * is > 0? right now, we're using it anyway and
88 * just praying. Can there be more than one open
89 * cache to the same db?
91 * Also, it's really only necessary to compare the
92 * read-only flag, instead of all of the flags,
93 * but for now I'm checking all of them.
99 li->li_dbcache[i].dbc_refcnt++;
101 LDAP_LOG(( "cache", LDAP_LEVEL_DETAIL1,
102 "ldbm_cache_open: cache %d\n", i ));
104 Debug( LDAP_DEBUG_TRACE,
105 "<= ldbm_cache_open (cache %d)\n", i, 0, 0 );
108 ldap_pvt_thread_mutex_unlock( &li->li_dbcache_mutex );
109 return( &li->li_dbcache[i] );
112 /* keep track of lru db */
113 if ( li->li_dbcache[i].dbc_lastref < oldtime
114 && li->li_dbcache[i].dbc_refcnt == 0 )
117 oldtime = li->li_dbcache[i].dbc_lastref;
122 if ( i == MAXDBCACHE ) {
123 /* no empty slots, not already open - close lru and use that slot */
124 if ( li->li_dbcache[lru].dbc_refcnt == 0 ) {
126 ldbm_close( li->li_dbcache[i].dbc_db );
127 free( li->li_dbcache[i].dbc_name );
128 li->li_dbcache[i].dbc_name = NULL;
131 LDAP_LOG(( "cache", LDAP_LEVEL_INFO,
132 "ldbm_cache_open: no unused db to close - waiting\n" ));
134 Debug( LDAP_DEBUG_ANY,
135 "ldbm_cache_open no unused db to close - waiting\n",
139 ldap_pvt_thread_cond_wait( &li->li_dbcache_cv,
140 &li->li_dbcache_mutex );
141 /* after waiting for a free slot, go back to square
142 * one: look for an open cache for this db, or an
143 * empty slot, or an unref'ed cache, or wait again.
147 } while (i == MAXDBCACHE);
149 if ( (li->li_dbcache[i].dbc_db = ldbm_open( buf, flags, li->li_mode,
150 li->li_dbcachesize )) == NULL )
154 LDAP_LOG(( "cache", LDAP_LEVEL_ERR,
155 "ldbm_cache_open: \"%s\" failed, errono=%d, reason=%s\n",
156 buf, err, err > -1 && err < sys_nerr ? sys_errlist[err] :
159 Debug( LDAP_DEBUG_TRACE,
160 "<= ldbm_cache_open NULL \"%s\" errno=%d reason=\"%s\")\n",
161 buf, err, err > -1 && err < sys_nerr ?
162 sys_errlist[err] : "unknown" );
165 ldap_pvt_thread_mutex_unlock( &li->li_dbcache_mutex );
168 li->li_dbcache[i].dbc_name = ch_strdup( buf );
169 li->li_dbcache[i].dbc_refcnt = 1;
170 li->li_dbcache[i].dbc_lastref = curtime;
171 li->li_dbcache[i].dbc_flags = flags;
172 li->li_dbcache[i].dbc_dirty = 0;
173 #ifdef HAVE_ST_BLKSIZE
174 if ( stat( buf, &st ) == 0 ) {
175 li->li_dbcache[i].dbc_blksize = st.st_blksize;
179 li->li_dbcache[i].dbc_blksize = DEFAULT_BLOCKSIZE;
181 li->li_dbcache[i].dbc_maxids = (li->li_dbcache[i].dbc_blksize /
182 sizeof(ID)) - ID_BLOCK_IDS_OFFSET;
183 li->li_dbcache[i].dbc_maxindirect = ( SLAPD_LDBM_MIN_MAXIDS /
184 li->li_dbcache[i].dbc_maxids ) + 1;
186 assert( li->li_dbcache[i].dbc_maxindirect < 256 );
189 LDAP_LOG(( "cache", LDAP_LEVEL_ARGS,
190 "ldbm_cache_open: blksize:%ld maxids:%d maxindirect:%d\n",
191 li->li_dbcache[i].dbc_blksize, li->li_dbcache[i].dbc_maxids,
192 li->li_dbcache[i].dbc_maxindirect ));
194 Debug( LDAP_DEBUG_ARGS,
195 "ldbm_cache_open (blksize %ld) (maxids %d) (maxindirect %d)\n",
196 li->li_dbcache[i].dbc_blksize, li->li_dbcache[i].dbc_maxids,
197 li->li_dbcache[i].dbc_maxindirect );
201 LDAP_LOG(( "cache", LDAP_LEVEL_DETAIL1,
202 "ldbm_cache_open: opened %d\n", i ));
204 Debug( LDAP_DEBUG_TRACE, "<= ldbm_cache_open (opened %d)\n", i, 0, 0 );
207 ldap_pvt_thread_mutex_unlock( &li->li_dbcache_mutex );
208 return( &li->li_dbcache[i] );
212 ldbm_cache_close( Backend *be, DBCache *db )
214 struct ldbminfo *li = (struct ldbminfo *) be->be_private;
216 if( li->li_dbwritesync && db->dbc_dirty ) {
217 ldbm_sync( db->dbc_db );
221 ldap_pvt_thread_mutex_lock( &li->li_dbcache_mutex );
222 if ( --db->dbc_refcnt <= 0 ) {
224 ldap_pvt_thread_cond_signal( &li->li_dbcache_cv );
226 ldap_pvt_thread_mutex_unlock( &li->li_dbcache_mutex );
230 ldbm_cache_really_close( Backend *be, DBCache *db )
232 struct ldbminfo *li = (struct ldbminfo *) be->be_private;
234 ldap_pvt_thread_mutex_lock( &li->li_dbcache_mutex );
235 if ( --db->dbc_refcnt <= 0 ) {
237 ldap_pvt_thread_cond_signal( &li->li_dbcache_cv );
238 ldbm_close( db->dbc_db );
239 free( db->dbc_name );
242 ldap_pvt_thread_mutex_unlock( &li->li_dbcache_mutex );
246 ldbm_cache_flush_all( Backend *be )
248 struct ldbminfo *li = (struct ldbminfo *) be->be_private;
251 ldap_pvt_thread_mutex_lock( &li->li_dbcache_mutex );
252 for ( i = 0; i < MAXDBCACHE; i++ ) {
253 if ( li->li_dbcache[i].dbc_name != NULL ) {
255 LDAP_LOG(( "cache", LDAP_LEVEL_DETAIL1,
256 "ldbm_cache_flush_all: flushing db (%s)\n",
257 li->li_dbcache[i].dbc_name ));
259 Debug( LDAP_DEBUG_TRACE, "ldbm flushing db (%s)\n",
260 li->li_dbcache[i].dbc_name, 0, 0 );
263 ldbm_sync( li->li_dbcache[i].dbc_db );
264 li->li_dbcache[i].dbc_dirty = 0;
265 if ( li->li_dbcache[i].dbc_refcnt != 0 ) {
267 LDAP_LOG(( "cache", LDAP_LEVEL_INFO,
268 "ldbm_cache_flush_all: couldn't close db (%s), refcnt=%d\n",
269 li->li_dbcache[i].dbc_name, li->li_dbcache[i].dbc_refcnt ));
271 Debug( LDAP_DEBUG_TRACE,
272 "refcnt = %d, couldn't close db (%s)\n",
273 li->li_dbcache[i].dbc_refcnt,
274 li->li_dbcache[i].dbc_name, 0 );
279 LDAP_LOG(( "cache", LDAP_LEVEL_DETAIL1,
280 "ldbm_cache_flush_all: ldbm closing db (%s)\n",
281 li->li_dbcache[i].dbc_name ));
283 Debug( LDAP_DEBUG_TRACE,
284 "ldbm closing db (%s)\n",
285 li->li_dbcache[i].dbc_name, 0, 0 );
288 ldap_pvt_thread_cond_signal( &li->li_dbcache_cv );
289 ldbm_close( li->li_dbcache[i].dbc_db );
290 free( li->li_dbcache[i].dbc_name );
291 li->li_dbcache[i].dbc_name = NULL;
295 ldap_pvt_thread_mutex_unlock( &li->li_dbcache_mutex );
306 return ldbm_fetch( db->dbc_db, key );
322 Statslog( LDAP_DEBUG_STATS,
323 "=> ldbm_cache_store(): key.dptr=%s, key.dsize=%d\n",
324 key.dptr, key.dsize, 0, 0, 0 );
326 Statslog( LDAP_DEBUG_STATS,
327 "=> ldbm_cache_store(): key.dptr=0x%08x, data.dptr=0x%0 8x\n",
328 key.dptr, data.dptr, 0, 0, 0 );
330 Statslog( LDAP_DEBUG_STATS,
331 "=> ldbm_cache_store(): data.dptr=%s, data.dsize=%d\n",
332 data.dptr, data.dsize, 0, 0, 0 );
334 Statslog( LDAP_DEBUG_STATS,
335 "=> ldbm_cache_store(): flags=0x%08x\n",
337 #endif /* LDBM_DEBUG */
340 rc = ldbm_store( db->dbc_db, key, data, flags );
354 rc = ldbm_delete( db->dbc_db, key );