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