]> git.sur5r.net Git - openldap/blob - servers/slapd/back-ldbm/dbcache.c
a90512127b286be007ff6d89ddc3646d19946328
[openldap] / servers / slapd / back-ldbm / dbcache.c
1 /* ldbmcache.c - maintain a cache of open ldbm files */
2 /* $OpenLDAP$ */
3 /*
4  * Copyright 1998-2000 The OpenLDAP Foundation, All Rights Reserved.
5  * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
6  */
7
8 #include "portable.h"
9
10 #include <stdio.h>
11
12 #include <ac/errno.h>
13 #include <ac/socket.h>
14 #include <ac/string.h>
15 #include <ac/time.h>
16 #include <ac/unistd.h>
17 #include <sys/stat.h>
18
19 #include "slap.h"
20 #include "back-ldbm.h"
21
22 DBCache *
23 ldbm_cache_open(
24     Backend     *be,
25     const char  *name,
26     const char  *suffix,
27     int         flags
28 )
29 {
30         struct ldbminfo *li = (struct ldbminfo *) be->be_private;
31         int             i, lru, empty;
32         time_t          oldtime;
33         char            buf[MAXPATHLEN];
34 #ifdef HAVE_ST_BLKSIZE
35         struct stat     st;
36 #endif
37
38         if (li->li_envdirok)
39                 sprintf( buf, "%s%s", name, suffix );
40         else
41                 sprintf( buf, "%s" LDAP_DIRSEP "%s%s",
42                         li->li_directory, name, suffix );
43
44         if( li->li_dblocking ) {
45                 flags |= LDBM_LOCKING;
46         } else {
47                 flags |= LDBM_NOLOCKING;
48         }
49         
50         if( li->li_dbwritesync ) {
51                 flags |= LDBM_SYNC;
52         } else {
53                 flags |= LDBM_NOSYNC;
54         }
55         
56 #ifdef NEW_LOGGING
57         LDAP_LOG(( "cache", LDAP_LEVEL_ENTRY,
58                    "ldbm_cache_open: \"%s\", %d, %o\n", buf, flags, li->li_mode ));
59 #else
60         Debug( LDAP_DEBUG_TRACE, "=> ldbm_cache_open( \"%s\", %d, %o )\n", buf,
61             flags, li->li_mode );
62 #endif
63
64
65         empty = MAXDBCACHE;
66
67         ldap_pvt_thread_mutex_lock( &li->li_dbcache_mutex );
68         do {
69                 lru = 0;
70                 oldtime = 1;
71                 for ( i = 0; i < MAXDBCACHE; i++ ) {
72                         /* see if this slot is free */
73                         if ( li->li_dbcache[i].dbc_name == NULL) {
74                                 if (empty == MAXDBCACHE)
75                                         empty = i;
76                                 continue;
77                         }
78
79                         if ( strcmp( li->li_dbcache[i].dbc_name, buf ) == 0 ) {
80                                 /* already open - return it */
81                                 if (li->li_dbcache[i].dbc_flags != flags
82                                         && li->li_dbcache[i].dbc_refcnt == 0)
83                                 {
84                                         /* we don't want to use an open cache with different
85                                          * permissions (esp. if we need write but the open
86                                          * cache is read-only).  So close this one if
87                                          * possible, and re-open below.
88                                          *
89                                          * FIXME:  what about the case where the refcount
90                                          * is > 0?  right now, we're using it anyway and
91                                          * just praying.  Can there be more than one open
92                                          * cache to the same db?
93                                          *
94                                          * Also, it's really only necessary to compare the
95                                          * read-only flag, instead of all of the flags,
96                                          * but for now I'm checking all of them.
97                                          */
98                                         lru = i;
99                                         empty = MAXDBCACHE;
100                                         break;
101                                 }
102                                 li->li_dbcache[i].dbc_refcnt++;
103 #ifdef NEW_LOGGING
104                                 LDAP_LOG(( "cache", LDAP_LEVEL_DETAIL1,
105                                            "ldbm_cache_open: cache %d\n", i ));
106 #else
107                                 Debug( LDAP_DEBUG_TRACE,
108                                     "<= ldbm_cache_open (cache %d)\n", i, 0, 0 );
109 #endif
110
111                                 ldap_pvt_thread_mutex_unlock( &li->li_dbcache_mutex );
112                                 return( &li->li_dbcache[i] );
113                         }
114
115                         /* keep track of lru db */
116                         if (( li->li_dbcache[i].dbc_refcnt == 0 ) &&
117                               (( oldtime == 1 ) ||
118                               ( li->li_dbcache[i].dbc_lastref < oldtime )) )
119                         {
120                                 lru = i;
121                                 oldtime = li->li_dbcache[i].dbc_lastref;
122                         }
123                 }
124
125                 i = empty;
126                 if ( i == MAXDBCACHE ) {
127                         /* no empty slots, not already open - close lru and use that slot */
128                         if ( li->li_dbcache[lru].dbc_refcnt == 0 ) {
129                                 i = lru;
130                                 ldbm_close( li->li_dbcache[i].dbc_db );
131                                 free( li->li_dbcache[i].dbc_name );
132                                 li->li_dbcache[i].dbc_name = NULL;
133                         } else {
134 #ifdef NEW_LOGGING
135                                 LDAP_LOG(( "cache", LDAP_LEVEL_INFO,
136                                            "ldbm_cache_open: no unused db to close - waiting\n" ));
137 #else
138                                 Debug( LDAP_DEBUG_ANY,
139                                     "ldbm_cache_open no unused db to close - waiting\n",
140                                     0, 0, 0 );
141 #endif
142
143                                 ldap_pvt_thread_cond_wait( &li->li_dbcache_cv,
144                                             &li->li_dbcache_mutex );
145                                 /* after waiting for a free slot, go back to square
146                                  * one: look for an open cache for this db, or an
147                                  * empty slot, or an unref'ed cache, or wait again.
148                                  */
149                         }
150                 }
151         } while (i == MAXDBCACHE);
152
153         if ( (li->li_dbcache[i].dbc_db = ldbm_open( li->li_dbenv, buf, flags, li->li_mode,
154             li->li_dbcachesize )) == NULL )
155         {
156                 int err = errno;
157 #ifdef NEW_LOGGING
158                 LDAP_LOG(( "cache", LDAP_LEVEL_ERR,
159                            "ldbm_cache_open: \"%s\" failed, errono=%d, reason=%s\n",
160                            buf, err, err > -1 && err < sys_nerr ? sys_errlist[err] :
161                            "unknown" ));
162 #else
163                 Debug( LDAP_DEBUG_TRACE,
164                     "<= ldbm_cache_open NULL \"%s\" errno=%d reason=\"%s\")\n",
165                     buf, err, err > -1 && err < sys_nerr ?
166                     sys_errlist[err] : "unknown" );
167 #endif
168
169                 ldap_pvt_thread_mutex_unlock( &li->li_dbcache_mutex );
170                 return( NULL );
171         }
172         li->li_dbcache[i].dbc_name = ch_strdup( buf );
173         li->li_dbcache[i].dbc_refcnt = 1;
174         li->li_dbcache[i].dbc_lastref = slap_get_time();
175         li->li_dbcache[i].dbc_flags = flags;
176         li->li_dbcache[i].dbc_dirty = 0;
177 #ifdef HAVE_ST_BLKSIZE
178         if ( stat( buf, &st ) == 0 ) {
179                 li->li_dbcache[i].dbc_blksize = st.st_blksize;
180         } else
181 #endif
182         {
183                 li->li_dbcache[i].dbc_blksize = DEFAULT_BLOCKSIZE;
184         }
185         li->li_dbcache[i].dbc_maxids = (li->li_dbcache[i].dbc_blksize /
186             sizeof(ID)) - ID_BLOCK_IDS_OFFSET;
187         li->li_dbcache[i].dbc_maxindirect = ( SLAPD_LDBM_MIN_MAXIDS /
188             li->li_dbcache[i].dbc_maxids ) + 1;
189
190         assert( li->li_dbcache[i].dbc_maxindirect < 256 );
191
192 #ifdef NEW_LOGGING
193         LDAP_LOG(( "cache", LDAP_LEVEL_ARGS,
194                    "ldbm_cache_open: blksize:%ld  maxids:%d  maxindirect:%d\n",
195                    li->li_dbcache[i].dbc_blksize, li->li_dbcache[i].dbc_maxids,
196                    li->li_dbcache[i].dbc_maxindirect ));
197 #else
198         Debug( LDAP_DEBUG_ARGS,
199             "ldbm_cache_open (blksize %ld) (maxids %d) (maxindirect %d)\n",
200             li->li_dbcache[i].dbc_blksize, li->li_dbcache[i].dbc_maxids,
201             li->li_dbcache[i].dbc_maxindirect );
202 #endif
203
204 #ifdef NEW_LOGGING
205         LDAP_LOG(( "cache", LDAP_LEVEL_DETAIL1,
206                    "ldbm_cache_open: opened %d\n", i ));
207 #else
208         Debug( LDAP_DEBUG_TRACE, "<= ldbm_cache_open (opened %d)\n", i, 0, 0 );
209 #endif
210
211         ldap_pvt_thread_mutex_init( &li->li_dbcache[i].dbc_write_mutex );
212
213         ldap_pvt_thread_mutex_unlock( &li->li_dbcache_mutex );
214         return( &li->li_dbcache[i] );
215 }
216
217 void
218 ldbm_cache_close( Backend *be, DBCache *db )
219 {
220         struct ldbminfo *li = (struct ldbminfo *) be->be_private;
221
222         if( li->li_dbwritesync && db->dbc_dirty ) {
223                 ldbm_sync( db->dbc_db );
224                 db->dbc_dirty = 0;
225         }
226
227         ldap_pvt_thread_mutex_lock( &li->li_dbcache_mutex );
228         if ( --db->dbc_refcnt <= 0 ) {
229                 db->dbc_refcnt = 0;
230                 ldap_pvt_thread_cond_signal( &li->li_dbcache_cv );
231         }
232         ldap_pvt_thread_mutex_unlock( &li->li_dbcache_mutex );
233 }
234
235 void
236 ldbm_cache_really_close( Backend *be, DBCache *db )
237 {
238         struct ldbminfo *li = (struct ldbminfo *) be->be_private;
239
240         ldap_pvt_thread_mutex_lock( &li->li_dbcache_mutex );
241         if ( --db->dbc_refcnt <= 0 ) {
242                 db->dbc_refcnt = 0;
243                 ldap_pvt_thread_cond_signal( &li->li_dbcache_cv );
244                 ldbm_close( db->dbc_db );
245                 free( db->dbc_name );
246                 db->dbc_name = NULL;
247                 ldap_pvt_thread_mutex_destroy( &db->dbc_write_mutex );
248         }
249         ldap_pvt_thread_mutex_unlock( &li->li_dbcache_mutex );
250 }
251
252 void
253 ldbm_cache_flush_all( Backend *be )
254 {
255         struct ldbminfo *li = (struct ldbminfo *) be->be_private;
256         int             i;
257
258         ldap_pvt_thread_mutex_lock( &li->li_dbcache_mutex );
259         for ( i = 0; i < MAXDBCACHE; i++ ) {
260                 if ( li->li_dbcache[i].dbc_name != NULL ) {
261 #ifdef NEW_LOGGING
262                         LDAP_LOG(( "cache", LDAP_LEVEL_DETAIL1,
263                                    "ldbm_cache_flush_all: flushing db (%s)\n",
264                                    li->li_dbcache[i].dbc_name ));
265 #else
266                         Debug( LDAP_DEBUG_TRACE, "ldbm flushing db (%s)\n",
267                             li->li_dbcache[i].dbc_name, 0, 0 );
268 #endif
269
270                         ldbm_sync( li->li_dbcache[i].dbc_db );
271                         li->li_dbcache[i].dbc_dirty = 0;
272                         if ( li->li_dbcache[i].dbc_refcnt != 0 ) {
273 #ifdef NEW_LOGGING
274                                 LDAP_LOG(( "cache", LDAP_LEVEL_INFO,
275                                            "ldbm_cache_flush_all: couldn't close db (%s), refcnt=%d\n",
276                                            li->li_dbcache[i].dbc_name, li->li_dbcache[i].dbc_refcnt ));
277 #else
278                                 Debug( LDAP_DEBUG_TRACE,
279                                        "refcnt = %d, couldn't close db (%s)\n",
280                                        li->li_dbcache[i].dbc_refcnt,
281                                        li->li_dbcache[i].dbc_name, 0 );
282 #endif
283
284                         } else {
285 #ifdef NEW_LOGGING
286                                 LDAP_LOG(( "cache", LDAP_LEVEL_DETAIL1,
287                                            "ldbm_cache_flush_all: ldbm closing db (%s)\n",
288                                            li->li_dbcache[i].dbc_name ));
289 #else
290                                 Debug( LDAP_DEBUG_TRACE,
291                                        "ldbm closing db (%s)\n",
292                                        li->li_dbcache[i].dbc_name, 0, 0 );
293 #endif
294
295                                 ldap_pvt_thread_cond_signal( &li->li_dbcache_cv );
296                                 ldbm_close( li->li_dbcache[i].dbc_db );
297                                 free( li->li_dbcache[i].dbc_name );
298                                 li->li_dbcache[i].dbc_name = NULL;
299                         }
300                 }
301         }
302         ldap_pvt_thread_mutex_unlock( &li->li_dbcache_mutex );
303 }
304
305 void
306 ldbm_cache_sync( Backend *be )
307 {
308         struct ldbminfo *li = (struct ldbminfo *) be->be_private;
309         int             i;
310
311         ldap_pvt_thread_mutex_lock( &li->li_dbcache_mutex );
312         for ( i = 0; i < MAXDBCACHE; i++ ) {
313                 if ( li->li_dbcache[i].dbc_name != NULL && li->li_dbcache[i].dbc_dirty ) {
314                         Debug(  LDAP_DEBUG_TRACE, "ldbm syncing db (%s)\n",
315                                 li->li_dbcache[i].dbc_name, 0, 0 );
316                         ldbm_sync( li->li_dbcache[i].dbc_db );
317                         li->li_dbcache[i].dbc_dirty = 0;
318                 }
319         }
320         ldap_pvt_thread_mutex_unlock( &li->li_dbcache_mutex );
321 }
322
323 Datum
324 ldbm_cache_fetch(
325     DBCache     *db,
326     Datum               key
327 )
328 {
329         return ldbm_fetch( db->dbc_db, key );
330 }
331
332 int
333 ldbm_cache_store(
334     DBCache     *db,
335     Datum               key,
336     Datum               data,
337     int                 flags
338 )
339 {
340         int     rc;
341
342 #ifdef LDBM_DEBUG
343         Statslog( LDAP_DEBUG_STATS,
344                 "=> ldbm_cache_store(): key.dptr=%s, key.dsize=%d\n",
345                 key.dptr, key.dsize, 0, 0, 0 );
346
347         Statslog( LDAP_DEBUG_STATS,
348                 "=> ldbm_cache_store(): key.dptr=0x%08x, data.dptr=0x%0 8x\n",
349                 key.dptr, data.dptr, 0, 0, 0 );
350
351         Statslog( LDAP_DEBUG_STATS,
352                 "=> ldbm_cache_store(): data.dptr=%s, data.dsize=%d\n",
353                 data.dptr, data.dsize, 0, 0, 0 );
354
355         Statslog( LDAP_DEBUG_STATS,
356                 "=> ldbm_cache_store(): flags=0x%08x\n",
357                 flags, 0, 0, 0, 0 );
358 #endif /* LDBM_DEBUG */
359
360         db->dbc_dirty = 1;
361         rc = ldbm_store( db->dbc_db, key, data, flags );
362
363         return( rc );
364 }
365
366 int
367 ldbm_cache_delete(
368     DBCache     *db,
369     Datum               key
370 )
371 {
372         int     rc;
373
374         db->dbc_dirty = 1;
375         rc = ldbm_delete( db->dbc_db, key );
376
377         return( rc );
378 }
379
380 void *
381 ldbm_cache_sync_daemon(
382         void *be_ptr
383 )
384 {
385         Backend *be = (Backend *)be_ptr;
386         struct ldbminfo *li = (struct ldbminfo *) be->be_private;
387
388         Debug( LDAP_DEBUG_ANY, "synchronizer starting for %s\n", li->li_directory, 0, 0 );
389   
390         while (!li->li_dbshutdown) {
391                 int i = li->li_dbsyncwaitn;
392
393                 sleep( li->li_dbsyncfreq );
394
395                 while (i && ldap_pvt_thread_pool_backload(&connection_pool) != 0) {
396                         Debug( LDAP_DEBUG_TRACE, "delay syncing %s\n", li->li_directory, 0, 0 );
397                         sleep(li->li_dbsyncwaitinterval);
398                         i--;
399                 }
400
401                 if (!li->li_dbshutdown) {
402                         Debug( LDAP_DEBUG_TRACE, "syncing %s\n", li->li_directory, 0, 0 );
403                         ldbm_cache_sync( be );
404                 }
405         }
406
407         Debug( LDAP_DEBUG_ANY, "synchronizer stopping\n", 0, 0, 0 );
408   
409         return NULL;
410 }