]> git.sur5r.net Git - openldap/blob - servers/slapd/back-bdb/dbcache.c
Happy New Year
[openldap] / servers / slapd / back-bdb / dbcache.c
1 /* dbcache.c - manage cache of open databases */
2 /* $OpenLDAP$ */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4  *
5  * Copyright 2000-2018 The OpenLDAP Foundation.
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted only as authorized by the OpenLDAP
10  * Public License.
11  *
12  * A copy of this license is available in the file LICENSE in the
13  * top-level directory of the distribution or, alternatively, at
14  * <http://www.OpenLDAP.org/license.html>.
15  */
16
17 #include "portable.h"
18
19 #include <stdio.h>
20
21 #include <ac/errno.h>
22 #include <ac/socket.h>
23 #include <ac/string.h>
24 #include <ac/time.h>
25 #include <sys/stat.h>
26
27 #include "slap.h"
28 #include "back-bdb.h"
29 #include "lutil_hash.h"
30
31 #ifdef BDB_INDEX_USE_HASH
32 /* Pass-thru hash function. Since the indexer is already giving us hash
33  * values as keys, we don't need BDB to re-hash them.
34  */
35 static u_int32_t
36 bdb_db_hash(
37         DB *db,
38         const void *bytes,
39         u_int32_t length
40 )
41 {
42         u_int32_t ret = 0;
43         unsigned char *dst = (unsigned char *)&ret;
44         const unsigned char *src = (const unsigned char *)bytes;
45
46         if ( length > sizeof(u_int32_t) )
47                 length = sizeof(u_int32_t);
48
49         while ( length ) {
50                 *dst++ = *src++;
51                 length--;
52         }
53         return ret;
54 }
55 #define BDB_INDEXTYPE   DB_HASH
56 #else
57 #define BDB_INDEXTYPE   DB_BTREE
58 #endif
59
60 /* If a configured size is found, return it, otherwise return 0 */
61 int
62 bdb_db_findsize(
63         struct bdb_info *bdb,
64         struct berval *name
65 )
66 {
67         struct bdb_db_pgsize *bp;
68         int rc;
69
70         for ( bp = bdb->bi_pagesizes; bp; bp=bp->bdp_next ) {
71                 rc = strncmp( name->bv_val, bp->bdp_name.bv_val, name->bv_len );
72                 if ( !rc ) {
73                         if ( name->bv_len == bp->bdp_name.bv_len )
74                                 return bp->bdp_size;
75                         if ( name->bv_len < bp->bdp_name.bv_len &&
76                                 bp->bdp_name.bv_val[name->bv_len] == '.' )
77                                 return bp->bdp_size;
78                 }
79         }
80         return 0;
81 }
82
83 int
84 bdb_db_cache(
85         Backend *be,
86         struct berval *name,
87         DB **dbout )
88 {
89         int i, flags;
90         int rc;
91         struct bdb_info *bdb = (struct bdb_info *) be->be_private;
92         struct bdb_db_info *db;
93         char *file;
94
95         *dbout = NULL;
96
97         for( i=BDB_NDB; i < bdb->bi_ndatabases; i++ ) {
98                 if( !ber_bvcmp( &bdb->bi_databases[i]->bdi_name, name) ) {
99                         *dbout = bdb->bi_databases[i]->bdi_db;
100                         return 0;
101                 }
102         }
103
104         ldap_pvt_thread_mutex_lock( &bdb->bi_database_mutex );
105
106         /* check again! may have been added by another thread */
107         for( i=BDB_NDB; i < bdb->bi_ndatabases; i++ ) {
108                 if( !ber_bvcmp( &bdb->bi_databases[i]->bdi_name, name) ) {
109                         *dbout = bdb->bi_databases[i]->bdi_db;
110                         ldap_pvt_thread_mutex_unlock( &bdb->bi_database_mutex );
111                         return 0;
112                 }
113         }
114
115         if( i >= BDB_INDICES ) {
116                 ldap_pvt_thread_mutex_unlock( &bdb->bi_database_mutex );
117                 return -1;
118         }
119
120         db = (struct bdb_db_info *) ch_calloc(1, sizeof(struct bdb_db_info));
121
122         ber_dupbv( &db->bdi_name, name );
123
124         rc = db_create( &db->bdi_db, bdb->bi_dbenv, 0 );
125         if( rc != 0 ) {
126                 Debug( LDAP_DEBUG_ANY,
127                         "bdb_db_cache: db_create(%s) failed: %s (%d)\n",
128                         bdb->bi_dbenv_home, db_strerror(rc), rc );
129                 ldap_pvt_thread_mutex_unlock( &bdb->bi_database_mutex );
130                 ch_free( db );
131                 return rc;
132         }
133
134         if( !BER_BVISNULL( &bdb->bi_db_crypt_key )) {
135                 rc = db->bdi_db->set_flags( db->bdi_db, DB_ENCRYPT );
136                 if ( rc ) {
137                         Debug( LDAP_DEBUG_ANY,
138                                 "bdb_db_cache: db set_flags(DB_ENCRYPT)(%s) failed: %s (%d)\n",
139                                 bdb->bi_dbenv_home, db_strerror(rc), rc );
140                         ldap_pvt_thread_mutex_unlock( &bdb->bi_database_mutex );
141                         db->bdi_db->close( db->bdi_db, 0 );
142                         ch_free( db );
143                         return rc;
144                 }
145         }
146
147         if( bdb->bi_flags & BDB_CHKSUM ) {
148                 rc = db->bdi_db->set_flags( db->bdi_db, DB_CHKSUM );
149                 if ( rc ) {
150                         Debug( LDAP_DEBUG_ANY,
151                                 "bdb_db_cache: db set_flags(DB_CHKSUM)(%s) failed: %s (%d)\n",
152                                 bdb->bi_dbenv_home, db_strerror(rc), rc );
153                         ldap_pvt_thread_mutex_unlock( &bdb->bi_database_mutex );
154                         db->bdi_db->close( db->bdi_db, 0 );
155                         ch_free( db );
156                         return rc;
157                 }
158         }
159
160         /* If no explicit size set, use the FS default */
161         flags = bdb_db_findsize( bdb, name );
162         if ( flags )
163                 rc = db->bdi_db->set_pagesize( db->bdi_db, flags );
164
165 #ifdef BDB_INDEX_USE_HASH
166         rc = db->bdi_db->set_h_hash( db->bdi_db, bdb_db_hash );
167 #endif
168         rc = db->bdi_db->set_flags( db->bdi_db, DB_DUP | DB_DUPSORT );
169
170         file = ch_malloc( db->bdi_name.bv_len + sizeof(BDB_SUFFIX) );
171         strcpy( file, db->bdi_name.bv_val );
172         strcpy( file+db->bdi_name.bv_len, BDB_SUFFIX );
173
174 #ifdef HAVE_EBCDIC
175         __atoe( file );
176 #endif
177         flags = DB_CREATE | DB_THREAD;
178 #ifdef DB_AUTO_COMMIT
179         if ( !( slapMode & SLAP_TOOL_QUICK ))
180                 flags |= DB_AUTO_COMMIT;
181 #endif
182         /* Cannot Truncate when Transactions are in use */
183         if ( (slapMode & (SLAP_TOOL_QUICK|SLAP_TRUNCATE_MODE)) ==
184                 (SLAP_TOOL_QUICK|SLAP_TRUNCATE_MODE))
185                         flags |= DB_TRUNCATE;
186
187         rc = DB_OPEN( db->bdi_db,
188                 file, NULL /* name */,
189                 BDB_INDEXTYPE, bdb->bi_db_opflags | flags, bdb->bi_dbenv_mode );
190
191         ch_free( file );
192
193         if( rc != 0 ) {
194                 Debug( LDAP_DEBUG_ANY,
195                         "bdb_db_cache: db_open(%s) failed: %s (%d)\n",
196                         name->bv_val, db_strerror(rc), rc );
197                 ldap_pvt_thread_mutex_unlock( &bdb->bi_database_mutex );
198                 db->bdi_db->close( db->bdi_db, 0 );
199                 ch_free( db );
200                 return rc;
201         }
202
203         bdb->bi_databases[i] = db;
204         bdb->bi_ndatabases = i+1;
205
206         *dbout = db->bdi_db;
207
208         ldap_pvt_thread_mutex_unlock( &bdb->bi_database_mutex );
209         return 0;
210 }