]> git.sur5r.net Git - openldap/blob - servers/slapd/back-ldbm/dbcache.c
56c259c8698d6f1a4b2130f443076acacdb983c3
[openldap] / servers / slapd / back-ldbm / dbcache.c
1 /* ldbmcache.c - maintain a cache of open ldbm files */
2
3 #include <stdio.h>
4 #include <string.h>
5 #include <sys/time.h>
6 #include <sys/types.h>
7 #include <sys/socket.h>
8 #include <sys/param.h>
9 #include <sys/stat.h>
10 #include <errno.h>
11 #include "portable.h"
12 #include "slap.h"
13 #include "ldapconfig.h"
14 #include "back-ldbm.h"
15
16 #ifndef SYSERRLIST_IN_STDIO
17 extern int              sys_nerr;
18 extern char             *sys_errlist[];
19 #endif
20 extern time_t           currenttime;
21 extern pthread_mutex_t  currenttime_mutex;
22
23 struct dbcache *
24 ldbm_cache_open(
25     Backend     *be,
26     char        *name,
27     char        *suffix,
28     int         flags
29 )
30 {
31         struct ldbminfo *li = (struct ldbminfo *) be->be_private;
32         int             i, lru;
33         time_t          oldtime, curtime;
34         char            buf[MAXPATHLEN];
35         LDBM            db;
36         struct stat     st;
37
38         sprintf( buf, "%s/%s%s", li->li_directory, name, suffix );
39
40         Debug( LDAP_DEBUG_TRACE, "=> ldbm_cache_open( \"%s\", %d, %o )\n", buf,
41             flags, li->li_mode );
42
43         lru = 0;
44         pthread_mutex_lock( &currenttime_mutex );
45         curtime = currenttime;
46         pthread_mutex_unlock( &currenttime_mutex );
47         oldtime = curtime;
48
49         pthread_mutex_lock( &li->li_dbcache_mutex );
50         for ( i = 0; i < MAXDBCACHE && li->li_dbcache[i].dbc_name != NULL;
51             i++ ) {
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] );
59                 }
60
61                 /* keep track of lru db */
62                 if ( li->li_dbcache[i].dbc_lastref < oldtime &&
63                     li->li_dbcache[i].dbc_refcnt == 0 ) {
64                         lru = i;
65                         oldtime = li->li_dbcache[i].dbc_lastref;
66                 }
67         }
68
69         /* no empty slots, not already open - close lru and use that slot */
70         if ( i == MAXDBCACHE ) {
71                 i = lru;
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",
75                             0, 0, 0 );
76                         lru = -1;
77                         while ( lru == -1 ) {
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
82                                             == 0 ) {
83                                                 lru = i;
84                                                 break;
85                                         }
86                                 }
87                         }
88                         i = lru;
89                 }
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;
93         }
94
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 );
102                 return( NULL );
103         }
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;
109         } else {
110                 li->li_dbcache[i].dbc_blksize = DEFAULT_BLOCKSIZE;
111         }
112         li->li_dbcache[i].dbc_maxids = (li->li_dbcache[i].dbc_blksize /
113             sizeof(ID)) - 2;
114         li->li_dbcache[i].dbc_maxindirect = (SLAPD_LDBM_MIN_MAXIDS /
115             li->li_dbcache[i].dbc_maxids) + 1;
116
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] );
124 }
125
126 void
127 ldbm_cache_close( Backend *be, struct dbcache *db )
128 {
129         struct ldbminfo *li = (struct ldbminfo *) be->be_private;
130
131         pthread_mutex_lock( &li->li_dbcache_mutex );
132         if ( --db->dbc_refcnt == 0 ) {
133                 pthread_cond_signal( &li->li_dbcache_cv );
134         }
135         pthread_mutex_unlock( &li->li_dbcache_mutex );
136 }
137
138 void
139 ldbm_cache_really_close( Backend *be, struct dbcache *db )
140 {
141         struct ldbminfo *li = (struct ldbminfo *) be->be_private;
142
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 );
148                 db->dbc_name = NULL;
149         }
150         pthread_mutex_unlock( &li->li_dbcache_mutex );
151 }
152
153 void
154 ldbm_cache_flush_all( Backend *be )
155 {
156         struct ldbminfo *li = (struct ldbminfo *) be->be_private;
157         int             i;
158
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 );
167                 }
168         }
169         pthread_mutex_unlock( &li->li_dbcache_mutex );
170 }
171
172 Datum
173 ldbm_cache_fetch(
174     struct dbcache      *db,
175     Datum               key
176 )
177 {
178         Datum   data;
179 #ifdef LDBM_USE_DB2
180         memset( &data, 0, sizeof( data ) );
181 #endif
182
183         pthread_mutex_lock( &db->dbc_mutex );
184 #ifdef reentrant_database
185         /* increment reader count */
186         db->dbc_readers++
187         pthread_mutex_unlock( &db->dbc_mutex );
188 #endif
189
190         data = ldbm_fetch( db->dbc_db, key );
191
192 #ifdef reentrant_database
193         pthread_mutex_lock( &db->dbc_mutex );
194         /* decrement reader count & signal any waiting writers */
195         if ( --db->dbc_readers == 0 ) {
196                 pthread_cond_signal( &db->dbc_cv );
197         }
198 #endif
199         pthread_mutex_unlock( &db->dbc_mutex );
200
201         return( data );
202 }
203
204 int
205 ldbm_cache_store(
206     struct dbcache      *db,
207     Datum               key,
208     Datum               data,
209     int                 flags
210 )
211 {
212         int     rc;
213
214         pthread_mutex_lock( &db->dbc_mutex );
215 #ifdef reentrant_database
216         /* wait for reader count to drop to zero */
217         while ( db->dbc_readers > 0 ) {
218                 pthread_cond_wait( &db->dbc_cv, &db->dbc_mutex );
219         }
220 #endif
221
222 #ifdef LDBM_DEBUG
223         Statslog( LDAP_DEBUG_STATS,
224                 "=> ldbm_cache_store(): key.dptr=%s, key.dsize=%d\n",
225                 key.dptr, key.dsize, 0, 0, 0 );
226
227         Statslog( LDAP_DEBUG_STATS,
228                 "=> ldbm_cache_store(): key.dptr=0x%08x, data.dptr=0x%0 8x\n",
229                 key.dptr, data.dptr, 0, 0, 0 );
230
231         Statslog( LDAP_DEBUG_STATS,
232                 "=> ldbm_cache_store(): data.dptr=%s, data.dsize=%d\n",
233                 data.dptr, data.dsize, 0, 0, 0 );
234
235         Statslog( LDAP_DEBUG_STATS,
236                 "=> ldbm_cache_store(): flags=0x%08x\n",
237                 flags, 0, 0, 0, 0 );
238 #endif /* LDBM_DEBUG */
239
240         rc = ldbm_store( db->dbc_db, key, data, flags );
241
242         pthread_mutex_unlock( &db->dbc_mutex );
243
244         return( rc );
245 }
246
247 int
248 ldbm_cache_delete(
249     struct dbcache      *db,
250     Datum               key
251 )
252 {
253         int     rc;
254
255         pthread_mutex_lock( &db->dbc_mutex );
256 #ifdef reentrant_database
257         /* wait for reader count to drop to zero - then write */
258         while ( db->dbc_readers > 0 ) {
259                 pthread_cond_wait( &db->dbc_cv, &db->dbc_mutex );
260         }
261 #endif
262
263         rc = ldbm_delete( db->dbc_db, key );
264
265         pthread_mutex_unlock( &db->dbc_mutex );
266
267         return( rc );
268 }