]> git.sur5r.net Git - openldap/blob - servers/slapd/back-ldbm/dbcache.c
5589fc2153fc13d06e0cb6f2aaa20636aa664e2e
[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         ldap_pvt_thread_mutex_lock( &currenttime_mutex );
44         curtime = currenttime;
45         ldap_pvt_thread_mutex_unlock( &currenttime_mutex );
46         oldtime = curtime;
47
48         ldap_pvt_thread_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                         ldap_pvt_thread_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                                 ldap_pvt_thread_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                 int err = errno;
97                 Debug( LDAP_DEBUG_TRACE,
98                     "<= ldbm_cache_open NULL \"%s\" errno %d reason \"%s\")\n",
99                     buf, err, err > -1 && err < sys_nerr ?
100                     sys_errlist[err] : "unknown" );
101                 ldap_pvt_thread_mutex_unlock( &li->li_dbcache_mutex );
102                 return( NULL );
103         }
104         li->li_dbcache[i].dbc_name = ch_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)) - ID_BLOCK_IDS_OFFSET;
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 %ld) (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         ldap_pvt_thread_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         ldap_pvt_thread_mutex_lock( &li->li_dbcache_mutex );
132         if ( --db->dbc_refcnt == 0 ) {
133                 ldap_pvt_thread_cond_signal( &li->li_dbcache_cv );
134         }
135         ldap_pvt_thread_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         ldap_pvt_thread_mutex_lock( &li->li_dbcache_mutex );
144         if ( --db->dbc_refcnt == 0 ) {
145                 ldap_pvt_thread_cond_signal( &li->li_dbcache_cv );
146                 ldbm_close( db->dbc_db );
147                 free( db->dbc_name );
148                 db->dbc_name = NULL;
149         }
150         ldap_pvt_thread_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         ldap_pvt_thread_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                         ldbm_sync( li->li_dbcache[i].dbc_db );
165                 }
166         }
167         ldap_pvt_thread_mutex_unlock( &li->li_dbcache_mutex );
168 }
169
170 Datum
171 ldbm_cache_fetch(
172     struct dbcache      *db,
173     Datum               key
174 )
175 {
176         Datum   data;
177
178         ldbm_datum_init( data );
179
180         data = ldbm_fetch( db->dbc_db, key );
181
182         return( data );
183 }
184
185 int
186 ldbm_cache_store(
187     struct dbcache      *db,
188     Datum               key,
189     Datum               data,
190     int                 flags
191 )
192 {
193         int     rc;
194
195 #ifdef LDBM_DEBUG
196         Statslog( LDAP_DEBUG_STATS,
197                 "=> ldbm_cache_store(): key.dptr=%s, key.dsize=%d\n",
198                 key.dptr, key.dsize, 0, 0, 0 );
199
200         Statslog( LDAP_DEBUG_STATS,
201                 "=> ldbm_cache_store(): key.dptr=0x%08x, data.dptr=0x%0 8x\n",
202                 key.dptr, data.dptr, 0, 0, 0 );
203
204         Statslog( LDAP_DEBUG_STATS,
205                 "=> ldbm_cache_store(): data.dptr=%s, data.dsize=%d\n",
206                 data.dptr, data.dsize, 0, 0, 0 );
207
208         Statslog( LDAP_DEBUG_STATS,
209                 "=> ldbm_cache_store(): flags=0x%08x\n",
210                 flags, 0, 0, 0, 0 );
211 #endif /* LDBM_DEBUG */
212
213         rc = ldbm_store( db->dbc_db, key, data, flags );
214
215         return( rc );
216 }
217
218 int
219 ldbm_cache_delete(
220     struct dbcache      *db,
221     Datum               key
222 )
223 {
224         int     rc;
225
226         rc = ldbm_delete( db->dbc_db, key );
227
228         return( rc );
229 }