]> git.sur5r.net Git - openldap/blob - servers/slapd/back-ldbm/dbcache.c
269e3d152c924d97b50b33141ef00152add61374
[openldap] / servers / slapd / back-ldbm / dbcache.c
1 /* ldbmcache.c - maintain a cache of open ldbm files */
2
3 #define DISABLE_BRIDGE /* disable LDAP_BRIDGE code */
4 #include "portable.h"
5
6 #include <stdio.h>
7 #include <ac/string.h>
8 #include <ac/time.h>
9
10 #include <sys/types.h>
11 #include <sys/socket.h>
12 #include <sys/param.h>
13 #include <sys/stat.h>
14 #include <errno.h>
15
16 #include "slap.h"
17 #include "back-ldbm.h"
18 #include "ldapconfig.h"
19
20 #ifdef DECL_SYS_ERRLIST
21 extern int              sys_nerr;
22 extern char             *sys_errlist[];
23 #endif
24
25 extern time_t           currenttime;
26 extern pthread_mutex_t  currenttime_mutex;
27
28 struct dbcache *
29 ldbm_cache_open(
30     Backend     *be,
31     char        *name,
32     char        *suffix,
33     int         flags
34 )
35 {
36         struct ldbminfo *li = (struct ldbminfo *) be->be_private;
37         int             i, lru;
38         time_t          oldtime, curtime;
39         char            buf[MAXPATHLEN];
40         LDBM            db;
41         struct stat     st;
42
43         sprintf( buf, "%s/%s%s", li->li_directory, name, suffix );
44
45         Debug( LDAP_DEBUG_TRACE, "=> ldbm_cache_open( \"%s\", %d, %o )\n", buf,
46             flags, li->li_mode );
47
48         lru = 0;
49         pthread_mutex_lock( &currenttime_mutex );
50         curtime = currenttime;
51         pthread_mutex_unlock( &currenttime_mutex );
52         oldtime = curtime;
53
54         pthread_mutex_lock( &li->li_dbcache_mutex );
55         for ( i = 0; i < MAXDBCACHE && li->li_dbcache[i].dbc_name != NULL;
56             i++ ) {
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] );
64                 }
65
66                 /* keep track of lru db */
67                 if ( li->li_dbcache[i].dbc_lastref < oldtime &&
68                     li->li_dbcache[i].dbc_refcnt == 0 ) {
69                         lru = i;
70                         oldtime = li->li_dbcache[i].dbc_lastref;
71                 }
72         }
73
74         /* no empty slots, not already open - close lru and use that slot */
75         if ( i == MAXDBCACHE ) {
76                 i = lru;
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",
80                             0, 0, 0 );
81                         lru = -1;
82                         while ( lru == -1 ) {
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
87                                             == 0 ) {
88                                                 lru = i;
89                                                 break;
90                                         }
91                                 }
92                         }
93                         i = lru;
94                 }
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;
98         }
99
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 );
107                 return( NULL );
108         }
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;
114         } else {
115                 li->li_dbcache[i].dbc_blksize = DEFAULT_BLOCKSIZE;
116         }
117         li->li_dbcache[i].dbc_maxids = (li->li_dbcache[i].dbc_blksize /
118             sizeof(ID)) - 2;
119         li->li_dbcache[i].dbc_maxindirect = (SLAPD_LDBM_MIN_MAXIDS /
120             li->li_dbcache[i].dbc_maxids) + 1;
121
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] );
129 }
130
131 void
132 ldbm_cache_close( Backend *be, struct dbcache *db )
133 {
134         struct ldbminfo *li = (struct ldbminfo *) be->be_private;
135
136         pthread_mutex_lock( &li->li_dbcache_mutex );
137         if ( --db->dbc_refcnt == 0 ) {
138                 pthread_cond_signal( &li->li_dbcache_cv );
139         }
140         pthread_mutex_unlock( &li->li_dbcache_mutex );
141 }
142
143 void
144 ldbm_cache_really_close( Backend *be, struct dbcache *db )
145 {
146         struct ldbminfo *li = (struct ldbminfo *) be->be_private;
147
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 );
153                 db->dbc_name = NULL;
154         }
155         pthread_mutex_unlock( &li->li_dbcache_mutex );
156 }
157
158 void
159 ldbm_cache_flush_all( Backend *be )
160 {
161         struct ldbminfo *li = (struct ldbminfo *) be->be_private;
162         int             i;
163
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 );
172                 }
173         }
174         pthread_mutex_unlock( &li->li_dbcache_mutex );
175 }
176
177 Datum
178 ldbm_cache_fetch(
179     struct dbcache      *db,
180     Datum               key
181 )
182 {
183         Datum   data;
184 #ifdef LDBM_USE_DB2
185         memset( &data, 0, sizeof( data ) );
186 #endif
187
188         pthread_mutex_lock( &db->dbc_mutex );
189 #ifdef reentrant_database
190         /* increment reader count */
191         db->dbc_readers++
192         pthread_mutex_unlock( &db->dbc_mutex );
193 #endif
194
195         data = ldbm_fetch( db->dbc_db, key );
196
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 );
202         }
203 #endif
204         pthread_mutex_unlock( &db->dbc_mutex );
205
206         return( data );
207 }
208
209 int
210 ldbm_cache_store(
211     struct dbcache      *db,
212     Datum               key,
213     Datum               data,
214     int                 flags
215 )
216 {
217         int     rc;
218
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 );
224         }
225 #endif
226
227 #ifdef LDBM_DEBUG
228         Statslog( LDAP_DEBUG_STATS,
229                 "=> ldbm_cache_store(): key.dptr=%s, key.dsize=%d\n",
230                 key.dptr, key.dsize, 0, 0, 0 );
231
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 );
235
236         Statslog( LDAP_DEBUG_STATS,
237                 "=> ldbm_cache_store(): data.dptr=%s, data.dsize=%d\n",
238                 data.dptr, data.dsize, 0, 0, 0 );
239
240         Statslog( LDAP_DEBUG_STATS,
241                 "=> ldbm_cache_store(): flags=0x%08x\n",
242                 flags, 0, 0, 0, 0 );
243 #endif /* LDBM_DEBUG */
244
245         rc = ldbm_store( db->dbc_db, key, data, flags );
246
247         pthread_mutex_unlock( &db->dbc_mutex );
248
249         return( rc );
250 }
251
252 int
253 ldbm_cache_delete(
254     struct dbcache      *db,
255     Datum               key
256 )
257 {
258         int     rc;
259
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 );
265         }
266 #endif
267
268         rc = ldbm_delete( db->dbc_db, key );
269
270         pthread_mutex_unlock( &db->dbc_mutex );
271
272         return( rc );
273 }