]> git.sur5r.net Git - openldap/blob - servers/slapd/back-ldbm/dbcache.c
f563cb61c196215e5c5cb0b42f7bc219b9584fbf
[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 #ifdef HAVE_ST_BLKSIZE
35         struct stat     st;
36 #endif
37
38         sprintf( buf, "%s%s%s%s", li->li_directory, DEFAULT_DIRSEP, 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         ldap_pvt_thread_mutex_lock( &currenttime_mutex );
45         curtime = currenttime;
46         ldap_pvt_thread_mutex_unlock( &currenttime_mutex );
47         oldtime = curtime;
48
49         ldap_pvt_thread_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                         ldap_pvt_thread_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                                 ldap_pvt_thread_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                 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 #ifdef HAVE_ST_BLKSIZE
108         if ( stat( buf, &st ) == 0 ) {
109                 li->li_dbcache[i].dbc_blksize = st.st_blksize;
110         } else
111 #endif
112         {
113                 li->li_dbcache[i].dbc_blksize = DEFAULT_BLOCKSIZE;
114         }
115         li->li_dbcache[i].dbc_maxids = (li->li_dbcache[i].dbc_blksize /
116             sizeof(ID)) - ID_BLOCK_IDS_OFFSET;
117         li->li_dbcache[i].dbc_maxindirect = (SLAPD_LDBM_MIN_MAXIDS /
118             li->li_dbcache[i].dbc_maxids) + 1;
119
120         Debug( LDAP_DEBUG_ARGS,
121             "ldbm_cache_open (blksize %ld) (maxids %d) (maxindirect %d)\n",
122             li->li_dbcache[i].dbc_blksize, li->li_dbcache[i].dbc_maxids,
123             li->li_dbcache[i].dbc_maxindirect );
124         Debug( LDAP_DEBUG_TRACE, "<= ldbm_cache_open (opened %d)\n", i, 0, 0 );
125         ldap_pvt_thread_mutex_unlock( &li->li_dbcache_mutex );
126         return( &li->li_dbcache[i] );
127 }
128
129 void
130 ldbm_cache_close( Backend *be, struct dbcache *db )
131 {
132         struct ldbminfo *li = (struct ldbminfo *) be->be_private;
133
134         ldap_pvt_thread_mutex_lock( &li->li_dbcache_mutex );
135         if ( --db->dbc_refcnt == 0 ) {
136                 ldap_pvt_thread_cond_signal( &li->li_dbcache_cv );
137         }
138         ldap_pvt_thread_mutex_unlock( &li->li_dbcache_mutex );
139 }
140
141 void
142 ldbm_cache_really_close( Backend *be, struct dbcache *db )
143 {
144         struct ldbminfo *li = (struct ldbminfo *) be->be_private;
145
146         ldap_pvt_thread_mutex_lock( &li->li_dbcache_mutex );
147         if ( --db->dbc_refcnt == 0 ) {
148                 ldap_pvt_thread_cond_signal( &li->li_dbcache_cv );
149                 ldbm_close( db->dbc_db );
150                 free( db->dbc_name );
151                 db->dbc_name = NULL;
152         }
153         ldap_pvt_thread_mutex_unlock( &li->li_dbcache_mutex );
154 }
155
156 void
157 ldbm_cache_flush_all( Backend *be )
158 {
159         struct ldbminfo *li = (struct ldbminfo *) be->be_private;
160         int             i;
161
162         ldap_pvt_thread_mutex_lock( &li->li_dbcache_mutex );
163         for ( i = 0; i < MAXDBCACHE; i++ ) {
164                 if ( li->li_dbcache[i].dbc_name != NULL ) {
165                         Debug( LDAP_DEBUG_TRACE, "ldbm flushing db (%s)\n",
166                             li->li_dbcache[i].dbc_name, 0, 0 );
167                         ldbm_sync( li->li_dbcache[i].dbc_db );
168                 }
169         }
170         ldap_pvt_thread_mutex_unlock( &li->li_dbcache_mutex );
171 }
172
173 Datum
174 ldbm_cache_fetch(
175     struct dbcache      *db,
176     Datum               key
177 )
178 {
179         Datum   data;
180
181         ldbm_datum_init( data );
182
183         data = ldbm_fetch( db->dbc_db, key );
184
185         return( data );
186 }
187
188 int
189 ldbm_cache_store(
190     struct dbcache      *db,
191     Datum               key,
192     Datum               data,
193     int                 flags
194 )
195 {
196         int     rc;
197
198 #ifdef LDBM_DEBUG
199         Statslog( LDAP_DEBUG_STATS,
200                 "=> ldbm_cache_store(): key.dptr=%s, key.dsize=%d\n",
201                 key.dptr, key.dsize, 0, 0, 0 );
202
203         Statslog( LDAP_DEBUG_STATS,
204                 "=> ldbm_cache_store(): key.dptr=0x%08x, data.dptr=0x%0 8x\n",
205                 key.dptr, data.dptr, 0, 0, 0 );
206
207         Statslog( LDAP_DEBUG_STATS,
208                 "=> ldbm_cache_store(): data.dptr=%s, data.dsize=%d\n",
209                 data.dptr, data.dsize, 0, 0, 0 );
210
211         Statslog( LDAP_DEBUG_STATS,
212                 "=> ldbm_cache_store(): flags=0x%08x\n",
213                 flags, 0, 0, 0, 0 );
214 #endif /* LDBM_DEBUG */
215
216         rc = ldbm_store( db->dbc_db, key, data, flags );
217
218         return( rc );
219 }
220
221 int
222 ldbm_cache_delete(
223     struct dbcache      *db,
224     Datum               key
225 )
226 {
227         int     rc;
228
229         rc = ldbm_delete( db->dbc_db, key );
230
231         return( rc );
232 }