1 /* ldbmcache.c - maintain a cache of open ldbm files */
14 #ifdef HAVE_SYS_PARAM_H
15 #include <sys/param.h>
18 #include "ldapconfig.h"
20 #include "back-ldbm.h"
30 struct ldbminfo *li = (struct ldbminfo *) be->be_private;
32 time_t oldtime, curtime;
37 sprintf( buf, "%s/%s%s", li->li_directory, name, suffix );
39 Debug( LDAP_DEBUG_TRACE, "=> ldbm_cache_open( \"%s\", %d, %o )\n", buf,
43 pthread_mutex_lock( ¤ttime_mutex );
44 curtime = currenttime;
45 pthread_mutex_unlock( ¤ttime_mutex );
48 pthread_mutex_lock( &li->li_dbcache_mutex );
49 for ( i = 0; i < MAXDBCACHE && li->li_dbcache[i].dbc_name != NULL;
51 /* already open - return it */
52 if ( strcmp( li->li_dbcache[i].dbc_name, buf ) == 0 ) {
53 li->li_dbcache[i].dbc_refcnt++;
54 Debug( LDAP_DEBUG_TRACE,
55 "<= ldbm_cache_open (cache %d)\n", i, 0, 0 );
56 pthread_mutex_unlock( &li->li_dbcache_mutex );
57 return( &li->li_dbcache[i] );
60 /* keep track of lru db */
61 if ( li->li_dbcache[i].dbc_lastref < oldtime &&
62 li->li_dbcache[i].dbc_refcnt == 0 ) {
64 oldtime = li->li_dbcache[i].dbc_lastref;
68 /* no empty slots, not already open - close lru and use that slot */
69 if ( i == MAXDBCACHE ) {
71 if ( li->li_dbcache[i].dbc_refcnt != 0 ) {
72 Debug( LDAP_DEBUG_ANY,
73 "ldbm_cache_open no unused db to close - waiting\n",
77 pthread_cond_wait( &li->li_dbcache_cv,
78 &li->li_dbcache_mutex );
79 for ( i = 0; i < MAXDBCACHE; i++ ) {
80 if ( li->li_dbcache[i].dbc_refcnt
89 ldbm_close( li->li_dbcache[i].dbc_db );
90 free( li->li_dbcache[i].dbc_name );
91 li->li_dbcache[i].dbc_name = NULL;
94 if ( (li->li_dbcache[i].dbc_db = ldbm_open( buf, flags, li->li_mode,
95 li->li_dbcachesize )) == NULL ) {
96 Debug( LDAP_DEBUG_TRACE,
97 "<= ldbm_cache_open NULL \"%s\" errno %d reason \"%s\")\n",
98 buf, errno, errno > -1 && errno < sys_nerr ?
99 sys_errlist[errno] : "unknown" );
100 pthread_mutex_unlock( &li->li_dbcache_mutex );
103 li->li_dbcache[i].dbc_name = ch_strdup( buf );
104 li->li_dbcache[i].dbc_refcnt = 1;
105 li->li_dbcache[i].dbc_lastref = curtime;
106 if ( stat( buf, &st ) == 0 ) {
107 li->li_dbcache[i].dbc_blksize = st.st_blksize;
109 li->li_dbcache[i].dbc_blksize = DEFAULT_BLOCKSIZE;
111 li->li_dbcache[i].dbc_maxids = (li->li_dbcache[i].dbc_blksize /
113 li->li_dbcache[i].dbc_maxindirect = (SLAPD_LDBM_MIN_MAXIDS /
114 li->li_dbcache[i].dbc_maxids) + 1;
116 Debug( LDAP_DEBUG_ARGS,
117 "ldbm_cache_open (blksize %ld) (maxids %d) (maxindirect %d)\n",
118 li->li_dbcache[i].dbc_blksize, li->li_dbcache[i].dbc_maxids,
119 li->li_dbcache[i].dbc_maxindirect );
120 Debug( LDAP_DEBUG_TRACE, "<= ldbm_cache_open (opened %d)\n", i, 0, 0 );
121 pthread_mutex_unlock( &li->li_dbcache_mutex );
122 return( &li->li_dbcache[i] );
126 ldbm_cache_close( Backend *be, struct dbcache *db )
128 struct ldbminfo *li = (struct ldbminfo *) be->be_private;
130 pthread_mutex_lock( &li->li_dbcache_mutex );
131 if ( --db->dbc_refcnt == 0 ) {
132 pthread_cond_signal( &li->li_dbcache_cv );
134 pthread_mutex_unlock( &li->li_dbcache_mutex );
138 ldbm_cache_really_close( Backend *be, struct dbcache *db )
140 struct ldbminfo *li = (struct ldbminfo *) be->be_private;
142 pthread_mutex_lock( &li->li_dbcache_mutex );
143 if ( --db->dbc_refcnt == 0 ) {
144 pthread_cond_signal( &li->li_dbcache_cv );
145 ldbm_close( db->dbc_db );
146 free( db->dbc_name );
149 pthread_mutex_unlock( &li->li_dbcache_mutex );
153 ldbm_cache_flush_all( Backend *be )
155 struct ldbminfo *li = (struct ldbminfo *) be->be_private;
158 pthread_mutex_lock( &li->li_dbcache_mutex );
159 for ( i = 0; i < MAXDBCACHE; i++ ) {
160 if ( li->li_dbcache[i].dbc_name != NULL ) {
161 Debug( LDAP_DEBUG_TRACE, "ldbm flushing db (%s)\n",
162 li->li_dbcache[i].dbc_name, 0, 0 );
163 pthread_mutex_lock( &li->li_dbcache[i].dbc_mutex );
164 ldbm_sync( li->li_dbcache[i].dbc_db );
165 pthread_mutex_unlock( &li->li_dbcache[i].dbc_mutex );
168 pthread_mutex_unlock( &li->li_dbcache_mutex );
178 #ifdef HAVE_BERKELEY_DB2
179 memset( &data, 0, sizeof( data ) );
182 pthread_mutex_lock( &db->dbc_mutex );
183 #ifdef reentrant_database
184 /* increment reader count */
186 pthread_mutex_unlock( &db->dbc_mutex );
189 data = ldbm_fetch( db->dbc_db, key );
191 #ifdef reentrant_database
192 pthread_mutex_lock( &db->dbc_mutex );
193 /* decrement reader count & signal any waiting writers */
194 if ( --db->dbc_readers == 0 ) {
195 pthread_cond_signal( &db->dbc_cv );
198 pthread_mutex_unlock( &db->dbc_mutex );
213 pthread_mutex_lock( &db->dbc_mutex );
214 #ifdef reentrant_database
215 /* wait for reader count to drop to zero */
216 while ( db->dbc_readers > 0 ) {
217 pthread_cond_wait( &db->dbc_cv, &db->dbc_mutex );
222 Statslog( LDAP_DEBUG_STATS,
223 "=> ldbm_cache_store(): key.dptr=%s, key.dsize=%d\n",
224 key.dptr, key.dsize, 0, 0, 0 );
226 Statslog( LDAP_DEBUG_STATS,
227 "=> ldbm_cache_store(): key.dptr=0x%08x, data.dptr=0x%0 8x\n",
228 key.dptr, data.dptr, 0, 0, 0 );
230 Statslog( LDAP_DEBUG_STATS,
231 "=> ldbm_cache_store(): data.dptr=%s, data.dsize=%d\n",
232 data.dptr, data.dsize, 0, 0, 0 );
234 Statslog( LDAP_DEBUG_STATS,
235 "=> ldbm_cache_store(): flags=0x%08x\n",
237 #endif /* LDBM_DEBUG */
239 rc = ldbm_store( db->dbc_db, key, data, flags );
241 pthread_mutex_unlock( &db->dbc_mutex );
254 pthread_mutex_lock( &db->dbc_mutex );
255 #ifdef reentrant_database
256 /* wait for reader count to drop to zero - then write */
257 while ( db->dbc_readers > 0 ) {
258 pthread_cond_wait( &db->dbc_cv, &db->dbc_mutex );
262 rc = ldbm_delete( db->dbc_db, key );
264 pthread_mutex_unlock( &db->dbc_mutex );