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