1 /* ldbmcache.c - maintain a cache of open ldbm files */
7 #include <sys/socket.h>
13 #include "ldapconfig.h"
14 #include "back-ldbm.h"
16 #ifndef SYSERRLIST_IN_STDIO
18 extern char *sys_errlist[];
20 extern time_t currenttime;
21 extern pthread_mutex_t currenttime_mutex;
31 struct ldbminfo *li = (struct ldbminfo *) be->be_private;
33 time_t oldtime, curtime;
38 sprintf( buf, "%s/%s%s", li->li_directory, name, suffix );
40 Debug( LDAP_DEBUG_TRACE, "=> ldbm_cache_open( \"%s\", %d, %o )\n", buf,
44 pthread_mutex_lock( ¤ttime_mutex );
45 curtime = currenttime;
46 pthread_mutex_unlock( ¤ttime_mutex );
49 pthread_mutex_lock( &li->li_dbcache_mutex );
50 for ( i = 0; i < MAXDBCACHE && li->li_dbcache[i].dbc_name != NULL;
52 /* already open - return it */
53 if ( strcmp( li->li_dbcache[i].dbc_name, buf ) == 0 ) {
54 li->li_dbcache[i].dbc_refcnt++;
55 Debug( LDAP_DEBUG_TRACE,
56 "<= ldbm_cache_open (cache %d)\n", i, 0, 0 );
57 pthread_mutex_unlock( &li->li_dbcache_mutex );
58 return( &li->li_dbcache[i] );
61 /* keep track of lru db */
62 if ( li->li_dbcache[i].dbc_lastref < oldtime &&
63 li->li_dbcache[i].dbc_refcnt == 0 ) {
65 oldtime = li->li_dbcache[i].dbc_lastref;
69 /* no empty slots, not already open - close lru and use that slot */
70 if ( i == MAXDBCACHE ) {
72 if ( li->li_dbcache[i].dbc_refcnt != 0 ) {
73 Debug( LDAP_DEBUG_ANY,
74 "ldbm_cache_open no unused db to close - waiting\n",
78 pthread_cond_wait( &li->li_dbcache_cv,
79 &li->li_dbcache_mutex );
80 for ( i = 0; i < MAXDBCACHE; i++ ) {
81 if ( li->li_dbcache[i].dbc_refcnt
90 ldbm_close( li->li_dbcache[i].dbc_db );
91 free( li->li_dbcache[i].dbc_name );
92 li->li_dbcache[i].dbc_name = NULL;
95 if ( (li->li_dbcache[i].dbc_db = ldbm_open( buf, flags, li->li_mode,
96 li->li_dbcachesize )) == NULL ) {
97 Debug( LDAP_DEBUG_TRACE,
98 "<= ldbm_cache_open NULL \"%s\" errno %d reason \"%s\")\n",
99 buf, errno, errno > -1 && errno < sys_nerr ?
100 sys_errlist[errno] : "unknown" );
101 pthread_mutex_unlock( &li->li_dbcache_mutex );
104 li->li_dbcache[i].dbc_name = strdup( buf );
105 li->li_dbcache[i].dbc_refcnt = 1;
106 li->li_dbcache[i].dbc_lastref = curtime;
107 if ( stat( buf, &st ) == 0 ) {
108 li->li_dbcache[i].dbc_blksize = st.st_blksize;
110 li->li_dbcache[i].dbc_blksize = DEFAULT_BLOCKSIZE;
112 li->li_dbcache[i].dbc_maxids = (li->li_dbcache[i].dbc_blksize /
114 li->li_dbcache[i].dbc_maxindirect = (SLAPD_LDBM_MIN_MAXIDS /
115 li->li_dbcache[i].dbc_maxids) + 1;
117 Debug( LDAP_DEBUG_ARGS,
118 "ldbm_cache_open (blksize %d) (maxids %d) (maxindirect %d)\n",
119 li->li_dbcache[i].dbc_blksize, li->li_dbcache[i].dbc_maxids,
120 li->li_dbcache[i].dbc_maxindirect );
121 Debug( LDAP_DEBUG_TRACE, "<= ldbm_cache_open (opened %d)\n", i, 0, 0 );
122 pthread_mutex_unlock( &li->li_dbcache_mutex );
123 return( &li->li_dbcache[i] );
127 ldbm_cache_close( Backend *be, struct dbcache *db )
129 struct ldbminfo *li = (struct ldbminfo *) be->be_private;
131 pthread_mutex_lock( &li->li_dbcache_mutex );
132 if ( --db->dbc_refcnt == 0 ) {
133 pthread_cond_signal( &li->li_dbcache_cv );
135 pthread_mutex_unlock( &li->li_dbcache_mutex );
139 ldbm_cache_really_close( Backend *be, struct dbcache *db )
141 struct ldbminfo *li = (struct ldbminfo *) be->be_private;
143 pthread_mutex_lock( &li->li_dbcache_mutex );
144 if ( --db->dbc_refcnt == 0 ) {
145 pthread_cond_signal( &li->li_dbcache_cv );
146 ldbm_close( db->dbc_db );
147 free( db->dbc_name );
150 pthread_mutex_unlock( &li->li_dbcache_mutex );
154 ldbm_cache_flush_all( Backend *be )
156 struct ldbminfo *li = (struct ldbminfo *) be->be_private;
159 pthread_mutex_lock( &li->li_dbcache_mutex );
160 for ( i = 0; i < MAXDBCACHE; i++ ) {
161 if ( li->li_dbcache[i].dbc_name != NULL ) {
162 Debug( LDAP_DEBUG_TRACE, "ldbm flushing db (%s)\n",
163 li->li_dbcache[i].dbc_name, 0, 0 );
164 pthread_mutex_lock( &li->li_dbcache[i].dbc_mutex );
165 ldbm_sync( li->li_dbcache[i].dbc_db );
166 pthread_mutex_unlock( &li->li_dbcache[i].dbc_mutex );
169 pthread_mutex_unlock( &li->li_dbcache_mutex );
180 pthread_mutex_lock( &db->dbc_mutex );
181 #ifdef reentrant_database
182 /* increment reader count */
184 pthread_mutex_unlock( &db->dbc_mutex );
187 data = ldbm_fetch( db->dbc_db, key );
189 #ifdef reentrant_database
190 pthread_mutex_lock( &db->dbc_mutex );
191 /* decrement reader count & signal any waiting writers */
192 if ( --db->dbc_readers == 0 ) {
193 pthread_cond_signal( &db->dbc_cv );
196 pthread_mutex_unlock( &db->dbc_mutex );
211 pthread_mutex_lock( &db->dbc_mutex );
212 #ifdef reentrant_database
213 /* wait for reader count to drop to zero */
214 while ( db->dbc_readers > 0 ) {
215 pthread_cond_wait( &db->dbc_cv, &db->dbc_mutex );
219 rc = ldbm_store( db->dbc_db, key, data, flags );
221 pthread_mutex_unlock( &db->dbc_mutex );
234 pthread_mutex_lock( &db->dbc_mutex );
235 #ifdef reentrant_database
236 /* wait for reader count to drop to zero - then write */
237 while ( db->dbc_readers > 0 ) {
238 pthread_cond_wait( &db->dbc_cv, &db->dbc_mutex );
242 rc = ldbm_delete( db->dbc_db, key );
244 pthread_mutex_unlock( &db->dbc_mutex );