1 /* ldbmcache.c - maintain a cache of open ldbm files */
3 #define DISABLE_BRIDGE /* disable LDAP_BRIDGE code */
10 #include <sys/types.h>
11 #include <sys/socket.h>
12 #include <sys/param.h>
17 #include "back-ldbm.h"
18 #include "ldapconfig.h"
20 #ifdef DECL_SYS_ERRLIST
22 extern char *sys_errlist[];
25 extern time_t currenttime;
26 extern pthread_mutex_t currenttime_mutex;
36 struct ldbminfo *li = (struct ldbminfo *) be->be_private;
38 time_t oldtime, curtime;
43 sprintf( buf, "%s/%s%s", li->li_directory, name, suffix );
45 Debug( LDAP_DEBUG_TRACE, "=> ldbm_cache_open( \"%s\", %d, %o )\n", buf,
49 pthread_mutex_lock( ¤ttime_mutex );
50 curtime = currenttime;
51 pthread_mutex_unlock( ¤ttime_mutex );
54 pthread_mutex_lock( &li->li_dbcache_mutex );
55 for ( i = 0; i < MAXDBCACHE && li->li_dbcache[i].dbc_name != NULL;
57 /* already open - return it */
58 if ( strcmp( li->li_dbcache[i].dbc_name, buf ) == 0 ) {
59 li->li_dbcache[i].dbc_refcnt++;
60 Debug( LDAP_DEBUG_TRACE,
61 "<= ldbm_cache_open (cache %d)\n", i, 0, 0 );
62 pthread_mutex_unlock( &li->li_dbcache_mutex );
63 return( &li->li_dbcache[i] );
66 /* keep track of lru db */
67 if ( li->li_dbcache[i].dbc_lastref < oldtime &&
68 li->li_dbcache[i].dbc_refcnt == 0 ) {
70 oldtime = li->li_dbcache[i].dbc_lastref;
74 /* no empty slots, not already open - close lru and use that slot */
75 if ( i == MAXDBCACHE ) {
77 if ( li->li_dbcache[i].dbc_refcnt != 0 ) {
78 Debug( LDAP_DEBUG_ANY,
79 "ldbm_cache_open no unused db to close - waiting\n",
83 pthread_cond_wait( &li->li_dbcache_cv,
84 &li->li_dbcache_mutex );
85 for ( i = 0; i < MAXDBCACHE; i++ ) {
86 if ( li->li_dbcache[i].dbc_refcnt
95 ldbm_close( li->li_dbcache[i].dbc_db );
96 free( li->li_dbcache[i].dbc_name );
97 li->li_dbcache[i].dbc_name = NULL;
100 if ( (li->li_dbcache[i].dbc_db = ldbm_open( buf, flags, li->li_mode,
101 li->li_dbcachesize )) == NULL ) {
102 Debug( LDAP_DEBUG_TRACE,
103 "<= ldbm_cache_open NULL \"%s\" errno %d reason \"%s\")\n",
104 buf, errno, errno > -1 && errno < sys_nerr ?
105 sys_errlist[errno] : "unknown" );
106 pthread_mutex_unlock( &li->li_dbcache_mutex );
109 li->li_dbcache[i].dbc_name = strdup( buf );
110 li->li_dbcache[i].dbc_refcnt = 1;
111 li->li_dbcache[i].dbc_lastref = curtime;
112 if ( stat( buf, &st ) == 0 ) {
113 li->li_dbcache[i].dbc_blksize = st.st_blksize;
115 li->li_dbcache[i].dbc_blksize = DEFAULT_BLOCKSIZE;
117 li->li_dbcache[i].dbc_maxids = (li->li_dbcache[i].dbc_blksize /
119 li->li_dbcache[i].dbc_maxindirect = (SLAPD_LDBM_MIN_MAXIDS /
120 li->li_dbcache[i].dbc_maxids) + 1;
122 Debug( LDAP_DEBUG_ARGS,
123 "ldbm_cache_open (blksize %d) (maxids %d) (maxindirect %d)\n",
124 li->li_dbcache[i].dbc_blksize, li->li_dbcache[i].dbc_maxids,
125 li->li_dbcache[i].dbc_maxindirect );
126 Debug( LDAP_DEBUG_TRACE, "<= ldbm_cache_open (opened %d)\n", i, 0, 0 );
127 pthread_mutex_unlock( &li->li_dbcache_mutex );
128 return( &li->li_dbcache[i] );
132 ldbm_cache_close( Backend *be, struct dbcache *db )
134 struct ldbminfo *li = (struct ldbminfo *) be->be_private;
136 pthread_mutex_lock( &li->li_dbcache_mutex );
137 if ( --db->dbc_refcnt == 0 ) {
138 pthread_cond_signal( &li->li_dbcache_cv );
140 pthread_mutex_unlock( &li->li_dbcache_mutex );
144 ldbm_cache_really_close( Backend *be, struct dbcache *db )
146 struct ldbminfo *li = (struct ldbminfo *) be->be_private;
148 pthread_mutex_lock( &li->li_dbcache_mutex );
149 if ( --db->dbc_refcnt == 0 ) {
150 pthread_cond_signal( &li->li_dbcache_cv );
151 ldbm_close( db->dbc_db );
152 free( db->dbc_name );
155 pthread_mutex_unlock( &li->li_dbcache_mutex );
159 ldbm_cache_flush_all( Backend *be )
161 struct ldbminfo *li = (struct ldbminfo *) be->be_private;
164 pthread_mutex_lock( &li->li_dbcache_mutex );
165 for ( i = 0; i < MAXDBCACHE; i++ ) {
166 if ( li->li_dbcache[i].dbc_name != NULL ) {
167 Debug( LDAP_DEBUG_TRACE, "ldbm flushing db (%s)\n",
168 li->li_dbcache[i].dbc_name, 0, 0 );
169 pthread_mutex_lock( &li->li_dbcache[i].dbc_mutex );
170 ldbm_sync( li->li_dbcache[i].dbc_db );
171 pthread_mutex_unlock( &li->li_dbcache[i].dbc_mutex );
174 pthread_mutex_unlock( &li->li_dbcache_mutex );
185 memset( &data, 0, sizeof( data ) );
188 pthread_mutex_lock( &db->dbc_mutex );
189 #ifdef reentrant_database
190 /* increment reader count */
192 pthread_mutex_unlock( &db->dbc_mutex );
195 data = ldbm_fetch( db->dbc_db, key );
197 #ifdef reentrant_database
198 pthread_mutex_lock( &db->dbc_mutex );
199 /* decrement reader count & signal any waiting writers */
200 if ( --db->dbc_readers == 0 ) {
201 pthread_cond_signal( &db->dbc_cv );
204 pthread_mutex_unlock( &db->dbc_mutex );
219 pthread_mutex_lock( &db->dbc_mutex );
220 #ifdef reentrant_database
221 /* wait for reader count to drop to zero */
222 while ( db->dbc_readers > 0 ) {
223 pthread_cond_wait( &db->dbc_cv, &db->dbc_mutex );
228 Statslog( LDAP_DEBUG_STATS,
229 "=> ldbm_cache_store(): key.dptr=%s, key.dsize=%d\n",
230 key.dptr, key.dsize, 0, 0, 0 );
232 Statslog( LDAP_DEBUG_STATS,
233 "=> ldbm_cache_store(): key.dptr=0x%08x, data.dptr=0x%0 8x\n",
234 key.dptr, data.dptr, 0, 0, 0 );
236 Statslog( LDAP_DEBUG_STATS,
237 "=> ldbm_cache_store(): data.dptr=%s, data.dsize=%d\n",
238 data.dptr, data.dsize, 0, 0, 0 );
240 Statslog( LDAP_DEBUG_STATS,
241 "=> ldbm_cache_store(): flags=0x%08x\n",
243 #endif /* LDBM_DEBUG */
245 rc = ldbm_store( db->dbc_db, key, data, flags );
247 pthread_mutex_unlock( &db->dbc_mutex );
260 pthread_mutex_lock( &db->dbc_mutex );
261 #ifdef reentrant_database
262 /* wait for reader count to drop to zero - then write */
263 while ( db->dbc_readers > 0 ) {
264 pthread_cond_wait( &db->dbc_cv, &db->dbc_mutex );
268 rc = ldbm_delete( db->dbc_db, key );
270 pthread_mutex_unlock( &db->dbc_mutex );