]> git.sur5r.net Git - openldap/blob - servers/slapd/back-ldbm/dbcache.c
Import alias deref finding bug fix from devel
[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 <sys/stat.h>
17
18 #include "slap.h"
19 #include "back-ldbm.h"
20
21 DBCache *
22 ldbm_cache_open(
23     Backend     *be,
24     const char  *name,
25     const char  *suffix,
26     int         flags
27 )
28 {
29         struct ldbminfo *li = (struct ldbminfo *) be->be_private;
30         int             i, lru, empty;
31         time_t          oldtime, curtime;
32         char            buf[MAXPATHLEN];
33 #ifdef HAVE_ST_BLKSIZE
34         struct stat     st;
35 #endif
36
37         sprintf( buf, "%s" LDAP_DIRSEP "%s%s",
38                 li->li_directory, name, suffix );
39
40         if( li->li_dblocking ) {
41                 flags |= LDBM_LOCKING;
42         } else {
43                 flags |= LDBM_NOLOCKING;
44         }
45         
46         if( li->li_dbwritesync ) {
47                 flags |= LDBM_SYNC;
48         } else {
49                 flags |= LDBM_NOSYNC;
50         }
51         
52         Debug( LDAP_DEBUG_TRACE, "=> ldbm_cache_open( \"%s\", %d, %o )\n", buf,
53             flags, li->li_mode );
54
55         curtime = slap_get_time();
56         empty = MAXDBCACHE;
57
58         ldap_pvt_thread_mutex_lock( &li->li_dbcache_mutex );
59         do {
60                 lru = 0;
61                 oldtime = curtime;
62                 for ( i = 0; i < MAXDBCACHE; i++ ) {
63                         /* see if this slot is free */
64                         if ( li->li_dbcache[i].dbc_name == NULL) {
65                                 if (empty == MAXDBCACHE)
66                                         empty = i;
67                                 continue;
68                         }
69
70                         if ( strcmp( li->li_dbcache[i].dbc_name, buf ) == 0 ) {
71                                 /* already open - return it */
72                                 if (li->li_dbcache[i].dbc_flags != flags
73                                         && li->li_dbcache[i].dbc_refcnt == 0)
74                                 {
75                                         /* we don't want to use an open cache with different
76                                          * permissions (esp. if we need write but the open
77                                          * cache is read-only).  So close this one if
78                                          * possible, and re-open below.
79                                          *
80                                          * FIXME:  what about the case where the refcount
81                                          * is > 0?  right now, we're using it anyway and
82                                          * just praying.  Can there be more than one open
83                                          * cache to the same db?
84                                          *
85                                          * Also, it's really only necessary to compare the
86                                          * read-only flag, instead of all of the flags,
87                                          * but for now I'm checking all of them.
88                                          */
89                                         lru = i;
90                                         empty = MAXDBCACHE;
91                                         break;
92                                 }
93                                 li->li_dbcache[i].dbc_refcnt++;
94                                 Debug( LDAP_DEBUG_TRACE,
95                                     "<= ldbm_cache_open (cache %d)\n", i, 0, 0 );
96                                 ldap_pvt_thread_mutex_unlock( &li->li_dbcache_mutex );
97                                 return( &li->li_dbcache[i] );
98                         }
99
100                         /* keep track of lru db */
101                         if ( li->li_dbcache[i].dbc_lastref < oldtime
102                                 && li->li_dbcache[i].dbc_refcnt == 0 )
103                         {
104                                 lru = i;
105                                 oldtime = li->li_dbcache[i].dbc_lastref;
106                         }
107                 }
108
109                 i = empty;
110                 if ( i == MAXDBCACHE ) {
111                         /* no empty slots, not already open - close lru and use that slot */
112                         if ( li->li_dbcache[lru].dbc_refcnt == 0 ) {
113                                 i = lru;
114                                 ldbm_close( li->li_dbcache[i].dbc_db );
115                                 free( li->li_dbcache[i].dbc_name );
116                                 li->li_dbcache[i].dbc_name = NULL;
117                         } else {
118                                 Debug( LDAP_DEBUG_ANY,
119                                     "ldbm_cache_open no unused db to close - waiting\n",
120                                     0, 0, 0 );
121                                 ldap_pvt_thread_cond_wait( &li->li_dbcache_cv,
122                                             &li->li_dbcache_mutex );
123                                 /* after waiting for a free slot, go back to square
124                                  * one: look for an open cache for this db, or an
125                                  * empty slot, or an unref'ed cache, or wait again.
126                                  */
127                         }
128                 }
129         } while (i == MAXDBCACHE);
130
131         if ( (li->li_dbcache[i].dbc_db = ldbm_open( buf, flags, li->li_mode,
132             li->li_dbcachesize )) == NULL )
133         {
134                 int err = errno;
135                 Debug( LDAP_DEBUG_TRACE,
136                     "<= ldbm_cache_open NULL \"%s\" errno=%d reason=\"%s\")\n",
137                     buf, err, err > -1 && err < sys_nerr ?
138                     sys_errlist[err] : "unknown" );
139                 ldap_pvt_thread_mutex_unlock( &li->li_dbcache_mutex );
140                 return( NULL );
141         }
142         li->li_dbcache[i].dbc_name = ch_strdup( buf );
143         li->li_dbcache[i].dbc_refcnt = 1;
144         li->li_dbcache[i].dbc_lastref = curtime;
145         li->li_dbcache[i].dbc_flags = flags;
146         li->li_dbcache[i].dbc_dirty = 0;
147 #ifdef HAVE_ST_BLKSIZE
148         if ( stat( buf, &st ) == 0 ) {
149                 li->li_dbcache[i].dbc_blksize = st.st_blksize;
150         } else
151 #endif
152         {
153                 li->li_dbcache[i].dbc_blksize = DEFAULT_BLOCKSIZE;
154         }
155         li->li_dbcache[i].dbc_maxids = (li->li_dbcache[i].dbc_blksize /
156             sizeof(ID)) - ID_BLOCK_IDS_OFFSET;
157         li->li_dbcache[i].dbc_maxindirect = ( SLAPD_LDBM_MIN_MAXIDS /
158             li->li_dbcache[i].dbc_maxids ) + 1;
159
160         assert( li->li_dbcache[i].dbc_maxindirect < 256 );
161
162         Debug( LDAP_DEBUG_ARGS,
163             "ldbm_cache_open (blksize %ld) (maxids %d) (maxindirect %d)\n",
164             li->li_dbcache[i].dbc_blksize, li->li_dbcache[i].dbc_maxids,
165             li->li_dbcache[i].dbc_maxindirect );
166         Debug( LDAP_DEBUG_TRACE, "<= ldbm_cache_open (opened %d)\n", i, 0, 0 );
167         ldap_pvt_thread_mutex_unlock( &li->li_dbcache_mutex );
168         return( &li->li_dbcache[i] );
169 }
170
171 void
172 ldbm_cache_close( Backend *be, DBCache *db )
173 {
174         struct ldbminfo *li = (struct ldbminfo *) be->be_private;
175
176         if( li->li_dbwritesync && db->dbc_dirty ) {
177                 ldbm_sync( db->dbc_db );
178                 db->dbc_dirty = 0;
179         }
180
181         ldap_pvt_thread_mutex_lock( &li->li_dbcache_mutex );
182         if ( --db->dbc_refcnt <= 0 ) {
183                 db->dbc_refcnt = 0;
184                 ldap_pvt_thread_cond_signal( &li->li_dbcache_cv );
185         }
186         ldap_pvt_thread_mutex_unlock( &li->li_dbcache_mutex );
187 }
188
189 void
190 ldbm_cache_really_close( Backend *be, DBCache *db )
191 {
192         struct ldbminfo *li = (struct ldbminfo *) be->be_private;
193
194         ldap_pvt_thread_mutex_lock( &li->li_dbcache_mutex );
195         if ( --db->dbc_refcnt <= 0 ) {
196                 db->dbc_refcnt = 0;
197                 ldap_pvt_thread_cond_signal( &li->li_dbcache_cv );
198                 ldbm_close( db->dbc_db );
199                 free( db->dbc_name );
200                 db->dbc_name = NULL;
201         }
202         ldap_pvt_thread_mutex_unlock( &li->li_dbcache_mutex );
203 }
204
205 void
206 ldbm_cache_flush_all( Backend *be )
207 {
208         struct ldbminfo *li = (struct ldbminfo *) be->be_private;
209         int             i;
210
211         ldap_pvt_thread_mutex_lock( &li->li_dbcache_mutex );
212         for ( i = 0; i < MAXDBCACHE; i++ ) {
213                 if ( li->li_dbcache[i].dbc_name != NULL ) {
214                         Debug( LDAP_DEBUG_TRACE, "ldbm flushing db (%s)\n",
215                             li->li_dbcache[i].dbc_name, 0, 0 );
216                         ldbm_sync( li->li_dbcache[i].dbc_db );
217                         li->li_dbcache[i].dbc_dirty = 0;
218                         if ( li->li_dbcache[i].dbc_refcnt != 0 ) {
219                                 Debug( LDAP_DEBUG_TRACE,
220                                        "refcnt = %d, couldn't close db (%s)\n",
221                                        li->li_dbcache[i].dbc_refcnt,
222                                        li->li_dbcache[i].dbc_name, 0 );
223                         } else {
224                                 Debug( LDAP_DEBUG_TRACE,
225                                        "ldbm closing db (%s)\n",
226                                        li->li_dbcache[i].dbc_name, 0, 0 );
227                                 ldap_pvt_thread_cond_signal( &li->li_dbcache_cv );
228                                 ldbm_close( li->li_dbcache[i].dbc_db );
229                                 free( li->li_dbcache[i].dbc_name );
230                                 li->li_dbcache[i].dbc_name = NULL;
231                         }
232                 }
233         }
234         ldap_pvt_thread_mutex_unlock( &li->li_dbcache_mutex );
235 }
236
237 Datum
238 ldbm_cache_fetch(
239     DBCache     *db,
240     Datum               key
241 )
242 {
243         Datum   data;
244
245         return ldbm_fetch( db->dbc_db, key );
246
247         return( data );
248 }
249
250 int
251 ldbm_cache_store(
252     DBCache     *db,
253     Datum               key,
254     Datum               data,
255     int                 flags
256 )
257 {
258         int     rc;
259
260 #ifdef LDBM_DEBUG
261         Statslog( LDAP_DEBUG_STATS,
262                 "=> ldbm_cache_store(): key.dptr=%s, key.dsize=%d\n",
263                 key.dptr, key.dsize, 0, 0, 0 );
264
265         Statslog( LDAP_DEBUG_STATS,
266                 "=> ldbm_cache_store(): key.dptr=0x%08x, data.dptr=0x%0 8x\n",
267                 key.dptr, data.dptr, 0, 0, 0 );
268
269         Statslog( LDAP_DEBUG_STATS,
270                 "=> ldbm_cache_store(): data.dptr=%s, data.dsize=%d\n",
271                 data.dptr, data.dsize, 0, 0, 0 );
272
273         Statslog( LDAP_DEBUG_STATS,
274                 "=> ldbm_cache_store(): flags=0x%08x\n",
275                 flags, 0, 0, 0, 0 );
276 #endif /* LDBM_DEBUG */
277
278         db->dbc_dirty = 1;
279         rc = ldbm_store( db->dbc_db, key, data, flags );
280
281         return( rc );
282 }
283
284 int
285 ldbm_cache_delete(
286     DBCache     *db,
287     Datum               key
288 )
289 {
290         int     rc;
291
292         db->dbc_dirty = 1;
293         rc = ldbm_delete( db->dbc_db, key );
294
295         return( rc );
296 }