1 /* back-bdb.h - bdb back-end header file */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 2000-2012 The OpenLDAP Foundation.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted only as authorized by the OpenLDAP
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>.
27 #define DB_VERSION_FULL ((DB_VERSION_MAJOR << 24) | (DB_VERSION_MINOR << 16) | DB_VERSION_PATCH)
29 #define DN_BASE_PREFIX SLAP_INDEX_EQUALITY_PREFIX
30 #define DN_ONE_PREFIX '%'
31 #define DN_SUBTREE_PREFIX '@'
33 #define DBTzero(t) (memset((t), 0, sizeof(DBT)))
34 #define DBT2bv(t,bv) ((bv)->bv_val = (t)->data, \
35 (bv)->bv_len = (t)->size)
36 #define bv2DBT(bv,t) ((t)->data = (bv)->bv_val, \
37 (t)->size = (bv)->bv_len )
39 #define BDB_TXN_RETRIES 16
41 #define BDB_MAX_ADD_LOOP 30
43 #define BDB_SUFFIX ".bdb"
44 #define BDB_ID2ENTRY 0
48 /* The bdb on-disk entry format is pretty space-inefficient. Average
49 * sized user entries are 3-4K each. You need at least two entries to
50 * fit into a single database page, more is better. 64K is BDB's
51 * upper bound. Smaller pages are better for concurrency.
53 #ifndef BDB_ID2ENTRY_PAGESIZE
54 #define BDB_ID2ENTRY_PAGESIZE 16384
57 #define DEFAULT_CACHE_SIZE 1000
59 /* The default search IDL stack cache depth */
60 #define DEFAULT_SEARCH_STACK_DEPTH 16
62 /* The minimum we can function with */
63 #define MINIMUM_SEARCH_STACK_DEPTH 8
65 typedef struct bdb_idl_cache_entry_s {
70 struct bdb_idl_cache_entry_s* idl_lru_prev;
71 struct bdb_idl_cache_entry_s* idl_lru_next;
72 } bdb_idl_cache_entry_t;
74 /* BDB backend specific entry info */
75 typedef struct bdb_entry_info {
76 struct bdb_entry_info *bei_parent;
79 /* we use the bei_id as a lockobj, but we need to make the size != 4
80 * to avoid conflicting with BDB's internal locks. So add a byte here
81 * that is always zero.
86 #define CACHE_ENTRY_DELETED 1
87 #define CACHE_ENTRY_NO_KIDS 2
88 #define CACHE_ENTRY_NOT_LINKED 4
89 #define CACHE_ENTRY_NO_GRANDKIDS 8
90 #define CACHE_ENTRY_LOADING 0x10
91 #define CACHE_ENTRY_WALKING 0x20
92 #define CACHE_ENTRY_ONELEVEL 0x40
93 #define CACHE_ENTRY_REFERENCED 0x80
94 #define CACHE_ENTRY_NOT_CACHED 0x100
98 * remaining fields require backend cache lock to access
100 struct berval bei_nrdn;
102 struct berval bei_rdn;
103 int bei_modrdns; /* track renames */
104 int bei_ckids; /* number of kids cached */
105 int bei_dkids; /* number of kids on-disk, plus 1 */
109 #ifdef SLAP_ZONE_ALLOC
110 struct bdb_info *bei_bdb;
113 ldap_pvt_thread_mutex_t bei_kids_mutex;
115 struct bdb_entry_info *bei_lrunext; /* for cache lru list */
116 struct bdb_entry_info *bei_lruprev;
119 #define BEI(e) ((EntryInfo *) ((e)->e_private))
121 /* for the in-core cache of entries */
122 typedef struct bdb_cache {
123 EntryInfo *c_eifree; /* free list */
125 EntryInfo *c_lruhead; /* lru - add accessed entries here */
126 EntryInfo *c_lrutail; /* lru - rem lru entries from here */
132 ID c_eiused; /* EntryInfo's in use */
133 ID c_leaves; /* EntryInfo leaf nodes */
135 DB_TXN *c_txn; /* used by lru cleaner */
136 ldap_pvt_thread_rdwr_t c_rwlock;
137 ldap_pvt_thread_mutex_t c_lru_mutex;
138 ldap_pvt_thread_mutex_t c_count_mutex;
139 ldap_pvt_thread_mutex_t c_eifree_mutex;
140 #ifdef SLAP_ZONE_ALLOC
145 #define CACHE_READ_LOCK 0
146 #define CACHE_WRITE_LOCK 1
148 #define BDB_INDICES 128
151 struct berval bdi_name;
155 struct bdb_db_pgsize {
156 struct bdb_db_pgsize *bdp_next;
157 struct berval bdp_name;
162 #define BDB_MONITOR_IDX
163 #endif /* LDAP_DEVEL */
165 typedef struct bdb_monitor_t {
167 struct berval bdm_ndn;
176 /* DB_ENV parameters */
177 /* The DB_ENV can be tuned via DB_CONFIG */
179 u_int32_t bi_dbenv_xflags; /* extra flags */
183 int bi_db_opflags; /* db-specific flags */
184 struct bdb_db_info **bi_databases;
185 ldap_pvt_thread_mutex_t bi_database_mutex;
186 struct bdb_db_pgsize *bi_pagesizes;
188 slap_mask_t bi_defaultmask;
190 struct bdb_attrinfo **bi_attrs;
192 void *bi_search_stack;
193 int bi_search_stack_depth;
197 u_int32_t bi_txn_cp_min;
198 u_int32_t bi_txn_cp_kbyte;
199 struct re_s *bi_txn_cp_task;
200 struct re_s *bi_index_task;
202 u_int32_t bi_lock_detect;
206 ldap_pvt_thread_mutex_t bi_lastid_mutex;
207 ID bi_idl_cache_max_size;
208 ID bi_idl_cache_size;
209 Avlnode *bi_idl_tree;
210 bdb_idl_cache_entry_t *bi_idl_lru_head;
211 bdb_idl_cache_entry_t *bi_idl_lru_tail;
212 ldap_pvt_thread_rdwr_t bi_idl_tree_rwlock;
213 ldap_pvt_thread_mutex_t bi_idl_tree_lrulock;
214 alock_info_t bi_alock_info;
215 char *bi_db_config_path;
216 BerVarray bi_db_config;
217 char *bi_db_crypt_file;
218 struct berval bi_db_crypt_key;
219 bdb_monitor_t bi_monitor;
221 #ifdef BDB_MONITOR_IDX
222 ldap_pvt_thread_mutex_t bi_idx_mutex;
224 #endif /* BDB_MONITOR_IDX */
227 #define BDB_IS_OPEN 0x01
228 #define BDB_HAS_CONFIG 0x02
229 #define BDB_UPD_CONFIG 0x04
230 #define BDB_DEL_INDEX 0x08
231 #define BDB_RE_OPEN 0x10
232 #define BDB_CHKSUM 0x20
234 int bi_modrdns; /* number of modrdns completed */
235 ldap_pvt_thread_mutex_t bi_modrdns_mutex;
239 #define bi_id2entry bi_databases[BDB_ID2ENTRY]
240 #define bi_dn2id bi_databases[BDB_DN2ID]
243 struct bdb_lock_info {
244 struct bdb_lock_info *bli_next;
249 #define BLI_DONTFREE 1
254 struct bdb_lock_info *boi_locks; /* used when no txn */
259 #define BOI_DONTFREE 1
261 #define DB_OPEN(db, file, name, type, flags, mode) \
262 ((db)->open)(db, file, name, type, flags, mode)
264 #if DB_VERSION_MAJOR < 4
265 #define LOCK_DETECT(env,f,t,a) lock_detect(env, f, t, a)
266 #define LOCK_GET(env,i,f,o,m,l) lock_get(env, i, f, o, m, l)
267 #define LOCK_PUT(env,l) lock_put(env, l)
268 #define TXN_CHECKPOINT(env,k,m,f) txn_checkpoint(env, k, m, f)
269 #define TXN_BEGIN(env,p,t,f) txn_begin((env), p, t, f)
270 #define TXN_PREPARE(txn,gid) txn_prepare((txn), (gid))
271 #define TXN_COMMIT(txn,f) txn_commit((txn), (f))
272 #define TXN_ABORT(txn) txn_abort((txn))
273 #define TXN_ID(txn) txn_id(txn)
274 #define XLOCK_ID(env, locker) lock_id(env, locker)
275 #define XLOCK_ID_FREE(env, locker) lock_id_free(env, locker)
277 #define LOCK_DETECT(env,f,t,a) (env)->lock_detect(env, f, t, a)
278 #define LOCK_GET(env,i,f,o,m,l) (env)->lock_get(env, i, f, o, m, l)
279 #define LOCK_PUT(env,l) (env)->lock_put(env, l)
280 #define TXN_CHECKPOINT(env,k,m,f) (env)->txn_checkpoint(env, k, m, f)
281 #define TXN_BEGIN(env,p,t,f) (env)->txn_begin((env), p, t, f)
282 #define TXN_PREPARE(txn,g) (txn)->prepare((txn), (g))
283 #define TXN_COMMIT(txn,f) (txn)->commit((txn), (f))
284 #define TXN_ABORT(txn) (txn)->abort((txn))
285 #define TXN_ID(txn) (txn)->id(txn)
286 #define XLOCK_ID(env, locker) (env)->lock_id(env, locker)
287 #define XLOCK_ID_FREE(env, locker) (env)->lock_id_free(env, locker)
289 /* BDB 4.1.17 adds txn arg to db->open */
290 #if DB_VERSION_FULL >= 0x04010011
292 #define DB_OPEN(db, file, name, type, flags, mode) \
293 ((db)->open)(db, NULL, file, name, type, flags, mode)
296 /* #undef BDB_LOG_DEBUG */
300 /* env->log_printf appeared in 4.4 */
301 #if DB_VERSION_FULL >= 0x04040000
302 #define BDB_LOG_PRINTF(env,txn,fmt,...) (env)->log_printf((env),(txn),(fmt),__VA_ARGS__)
304 extern int __db_logmsg(const DB_ENV *env, DB_TXN *txn, const char *op, u_int32_t flags,
305 const char *fmt,...);
306 #define BDB_LOG_PRINTF(env,txn,fmt,...) __db_logmsg((env),(txn),"DIAGNOSTIC",0,(fmt),__VA_ARGS__)
310 #elif (defined(__STDC_VERSION__) && __STDC_VERSION__ >= 199901L) || \
311 (defined(__GNUC__) && __GNUC__ >= 3 && !defined(__STRICT_ANSI__))
312 #define BDB_LOG_PRINTF(a,b,c,...)
314 #define BDB_LOG_PRINTF (void) /* will evaluate and discard the arguments */
316 #endif /* BDB_LOG_DEBUG */
320 #ifndef DB_BUFFER_SMALL
321 #define DB_BUFFER_SMALL ENOMEM
324 #define BDB_CSN_COMMIT 0
325 #define BDB_CSN_ABORT 1
326 #define BDB_CSN_RETRY 2
328 /* Copy an ID "src" to pointer "dst" in big-endian byte order */
329 #define BDB_ID2DISK( src, dst ) \
330 do { int i0; ID tmp; unsigned char *_p; \
331 tmp = (src); _p = (unsigned char *)(dst); \
332 for ( i0=sizeof(ID)-1; i0>=0; i0-- ) { \
333 _p[i0] = tmp & 0xff; tmp >>= 8; \
337 /* Copy a pointer "src" to a pointer "dst" from big-endian to native order */
338 #define BDB_DISK2ID( src, dst ) \
339 do { unsigned i0; ID tmp = 0; unsigned char *_p; \
340 _p = (unsigned char *)(src); \
341 for ( i0=0; i0<sizeof(ID); i0++ ) { \
342 tmp <<= 8; tmp |= *_p++; \
348 /* for the cache of attribute information (which are indexed, etc.) */
349 typedef struct bdb_attrinfo {
350 AttributeDescription *ai_desc; /* attribute description cn;lang-en */
351 slap_mask_t ai_indexmask; /* how the attr is indexed */
352 slap_mask_t ai_newmask; /* new settings to replace old mask */
353 #ifdef LDAP_COMP_MATCH
354 ComponentReference* ai_cr; /*component indexing*/
358 /* These flags must not clash with SLAP_INDEX flags or ops in slap.h! */
359 #define BDB_INDEX_DELETING 0x8000U /* index is being modified */
360 #define BDB_INDEX_UPDATE_OP 0x03 /* performing an index update */
362 /* For slapindex to record which attrs in an entry belong to which
365 typedef struct AttrList {
366 struct AttrList *next;
370 typedef struct IndexRec {
375 #include "proto-bdb.h"
377 #endif /* _BACK_BDB_H_ */