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;
34 #ifdef HAVE_ST_BLKSIZE
38 sprintf( buf, "%s%s%s%s", li->li_directory, DEFAULT_DIRSEP, name, suffix );
40 Debug( LDAP_DEBUG_TRACE, "=> ldbm_cache_open( \"%s\", %d, %o )\n", buf,
44 ldap_pvt_thread_mutex_lock( ¤ttime_mutex );
45 curtime = currenttime;
46 ldap_pvt_thread_mutex_unlock( ¤ttime_mutex );
49 ldap_pvt_thread_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 ldap_pvt_thread_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 ldap_pvt_thread_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 ldap_pvt_thread_mutex_unlock( &li->li_dbcache_mutex );
104 li->li_dbcache[i].dbc_name = ch_strdup( buf );
105 li->li_dbcache[i].dbc_refcnt = 1;
106 li->li_dbcache[i].dbc_lastref = curtime;
107 #ifdef HAVE_ST_BLKSIZE
108 if ( stat( buf, &st ) == 0 ) {
109 li->li_dbcache[i].dbc_blksize = st.st_blksize;
113 li->li_dbcache[i].dbc_blksize = DEFAULT_BLOCKSIZE;
115 li->li_dbcache[i].dbc_maxids = (li->li_dbcache[i].dbc_blksize /
116 sizeof(ID)) - ID_BLOCK_IDS_OFFSET;
117 li->li_dbcache[i].dbc_maxindirect = (SLAPD_LDBM_MIN_MAXIDS /
118 li->li_dbcache[i].dbc_maxids) + 1;
120 Debug( LDAP_DEBUG_ARGS,
121 "ldbm_cache_open (blksize %ld) (maxids %d) (maxindirect %d)\n",
122 li->li_dbcache[i].dbc_blksize, li->li_dbcache[i].dbc_maxids,
123 li->li_dbcache[i].dbc_maxindirect );
124 Debug( LDAP_DEBUG_TRACE, "<= ldbm_cache_open (opened %d)\n", i, 0, 0 );
125 ldap_pvt_thread_mutex_unlock( &li->li_dbcache_mutex );
126 return( &li->li_dbcache[i] );
130 ldbm_cache_close( Backend *be, struct dbcache *db )
132 struct ldbminfo *li = (struct ldbminfo *) be->be_private;
134 ldap_pvt_thread_mutex_lock( &li->li_dbcache_mutex );
135 if ( --db->dbc_refcnt == 0 ) {
136 ldap_pvt_thread_cond_signal( &li->li_dbcache_cv );
138 ldap_pvt_thread_mutex_unlock( &li->li_dbcache_mutex );
142 ldbm_cache_really_close( Backend *be, struct dbcache *db )
144 struct ldbminfo *li = (struct ldbminfo *) be->be_private;
146 ldap_pvt_thread_mutex_lock( &li->li_dbcache_mutex );
147 if ( --db->dbc_refcnt == 0 ) {
148 ldap_pvt_thread_cond_signal( &li->li_dbcache_cv );
149 ldbm_close( db->dbc_db );
150 free( db->dbc_name );
153 ldap_pvt_thread_mutex_unlock( &li->li_dbcache_mutex );
157 ldbm_cache_flush_all( Backend *be )
159 struct ldbminfo *li = (struct ldbminfo *) be->be_private;
162 ldap_pvt_thread_mutex_lock( &li->li_dbcache_mutex );
163 for ( i = 0; i < MAXDBCACHE; i++ ) {
164 if ( li->li_dbcache[i].dbc_name != NULL ) {
165 Debug( LDAP_DEBUG_TRACE, "ldbm flushing db (%s)\n",
166 li->li_dbcache[i].dbc_name, 0, 0 );
167 ldbm_sync( li->li_dbcache[i].dbc_db );
170 ldap_pvt_thread_mutex_unlock( &li->li_dbcache_mutex );
181 ldbm_datum_init( data );
183 data = ldbm_fetch( db->dbc_db, key );
199 Statslog( LDAP_DEBUG_STATS,
200 "=> ldbm_cache_store(): key.dptr=%s, key.dsize=%d\n",
201 key.dptr, key.dsize, 0, 0, 0 );
203 Statslog( LDAP_DEBUG_STATS,
204 "=> ldbm_cache_store(): key.dptr=0x%08x, data.dptr=0x%0 8x\n",
205 key.dptr, data.dptr, 0, 0, 0 );
207 Statslog( LDAP_DEBUG_STATS,
208 "=> ldbm_cache_store(): data.dptr=%s, data.dsize=%d\n",
209 data.dptr, data.dsize, 0, 0, 0 );
211 Statslog( LDAP_DEBUG_STATS,
212 "=> ldbm_cache_store(): flags=0x%08x\n",
214 #endif /* LDBM_DEBUG */
216 rc = ldbm_store( db->dbc_db, key, data, flags );
229 rc = ldbm_delete( db->dbc_db, key );