]> git.sur5r.net Git - openldap/blob - servers/slapd/back-ldbm/dbcache.c
c16c8b160dfdfe210188465c5e9f19fe74636ad4
[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
180         pthread_mutex_lock( &db->dbc_mutex );
181 #ifdef reentrant_database
182         /* increment reader count */
183         db->dbc_readers++
184         pthread_mutex_unlock( &db->dbc_mutex );
185 #endif
186
187         data = ldbm_fetch( db->dbc_db, key );
188
189 #ifdef reentrant_database
190         pthread_mutex_lock( &db->dbc_mutex );
191         /* decrement reader count & signal any waiting writers */
192         if ( --db->dbc_readers == 0 ) {
193                 pthread_cond_signal( &db->dbc_cv );
194         }
195 #endif
196         pthread_mutex_unlock( &db->dbc_mutex );
197
198         return( data );
199 }
200
201 int
202 ldbm_cache_store(
203     struct dbcache      *db,
204     Datum               key,
205     Datum               data,
206     int                 flags
207 )
208 {
209         int     rc;
210
211         pthread_mutex_lock( &db->dbc_mutex );
212 #ifdef reentrant_database
213         /* wait for reader count to drop to zero */
214         while ( db->dbc_readers > 0 ) {
215                 pthread_cond_wait( &db->dbc_cv, &db->dbc_mutex );
216         }
217 #endif
218
219         rc = ldbm_store( db->dbc_db, key, data, flags );
220
221         pthread_mutex_unlock( &db->dbc_mutex );
222
223         return( rc );
224 }
225
226 int
227 ldbm_cache_delete(
228     struct dbcache      *db,
229     Datum               key
230 )
231 {
232         int     rc;
233
234         pthread_mutex_lock( &db->dbc_mutex );
235 #ifdef reentrant_database
236         /* wait for reader count to drop to zero - then write */
237         while ( db->dbc_readers > 0 ) {
238                 pthread_cond_wait( &db->dbc_cv, &db->dbc_mutex );
239         }
240 #endif
241
242         rc = ldbm_delete( db->dbc_db, key );
243
244         pthread_mutex_unlock( &db->dbc_mutex );
245
246         return( rc );
247 }