1 /* back-bdb.h - bdb back-end header file */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 2000-2007 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
58 #define BDB_PAGESIZE 4096 /* BDB's original default */
61 /* 4.6.18 redefines cursor->locker */
62 #if DB_VERSION_FULL >= 0x04060012
68 typedef struct __db_locker * BDB_LOCKER;
70 extern int __lock_getlocker(DB_LOCKTAB *lt, u_int32_t locker, int create, DB_LOCKER **ret);
72 #define CURSOR_SETLOCKER(cursor, id) cursor->locker = id
73 #define CURSOR_GETLOCKER(cursor) cursor->locker
76 typedef u_int32_t BDB_LOCKER;
78 #define CURSOR_SETLOCKER(cursor, id) cursor->locker = id
79 #define CURSOR_GETLOCKER(cursor) cursor->locker
83 #define DEFAULT_CACHE_SIZE 1000
85 /* The default search IDL stack cache depth */
86 #define DEFAULT_SEARCH_STACK_DEPTH 16
88 /* The minimum we can function with */
89 #define MINIMUM_SEARCH_STACK_DEPTH 8
91 typedef struct bdb_idl_cache_entry_s {
96 struct bdb_idl_cache_entry_s* idl_lru_prev;
97 struct bdb_idl_cache_entry_s* idl_lru_next;
98 } bdb_idl_cache_entry_t;
100 /* BDB backend specific entry info */
101 typedef struct bdb_entry_info {
102 struct bdb_entry_info *bei_parent;
105 /* we use the bei_id as a lockobj, but we need to make the size != 4
106 * to avoid conflicting with BDB's internal locks. So add a byte here
107 * that is always zero.
112 #define CACHE_ENTRY_DELETED 1
113 #define CACHE_ENTRY_NO_KIDS 2
114 #define CACHE_ENTRY_NOT_LINKED 4
115 #define CACHE_ENTRY_NO_GRANDKIDS 8
116 #define CACHE_ENTRY_LOADING 0x10
117 #define CACHE_ENTRY_WALKING 0x20
118 #define CACHE_ENTRY_ONELEVEL 0x40
119 #define CACHE_ENTRY_REFERENCED 0x80
123 * remaining fields require backend cache lock to access
125 struct berval bei_nrdn;
127 struct berval bei_rdn;
128 int bei_modrdns; /* track renames */
129 int bei_ckids; /* number of kids cached */
130 int bei_dkids; /* number of kids on-disk, plus 1 */
134 #ifdef SLAP_ZONE_ALLOC
135 struct bdb_info *bei_bdb;
138 ldap_pvt_thread_mutex_t bei_kids_mutex;
140 struct bdb_entry_info *bei_lrunext; /* for cache lru list */
141 struct bdb_entry_info *bei_lruprev;
144 #define BEI(e) ((EntryInfo *) ((e)->e_private))
146 /* for the in-core cache of entries */
147 typedef struct bdb_cache {
148 EntryInfo *c_eifree; /* free list */
150 EntryInfo *c_lruhead; /* lru - add accessed entries here */
151 EntryInfo *c_lrutail; /* lru - rem lru entries from here */
157 int c_eiused; /* EntryInfo's in use */
158 int c_leaves; /* EntryInfo leaf nodes */
160 BDB_LOCKER c_locker; /* used by lru cleaner */
161 ldap_pvt_thread_rdwr_t c_rwlock;
162 ldap_pvt_thread_mutex_t c_lru_mutex;
163 ldap_pvt_thread_mutex_t c_count_mutex;
164 ldap_pvt_thread_mutex_t c_eifree_mutex;
165 #ifdef SLAP_ZONE_ALLOC
170 #define CACHE_READ_LOCK 0
171 #define CACHE_WRITE_LOCK 1
173 #define BDB_INDICES 128
180 typedef struct bdb_monitor_t {
182 struct berval bdm_nbase;
184 struct berval bdm_filter;
193 /* DB_ENV parameters */
194 /* The DB_ENV can be tuned via DB_CONFIG */
196 u_int32_t bi_dbenv_xflags; /* extra flags */
200 struct bdb_db_info **bi_databases;
201 ldap_pvt_thread_mutex_t bi_database_mutex;
202 int bi_db_opflags; /* db-specific flags */
204 slap_mask_t bi_defaultmask;
206 struct bdb_attrinfo **bi_attrs;
208 void *bi_search_stack;
209 int bi_search_stack_depth;
213 u_int32_t bi_txn_cp_min;
214 u_int32_t bi_txn_cp_kbyte;
215 struct re_s *bi_txn_cp_task;
216 struct re_s *bi_index_task;
222 ldap_pvt_thread_mutex_t bi_lastid_mutex;
223 int bi_idl_cache_max_size;
224 int bi_idl_cache_size;
225 Avlnode *bi_idl_tree;
226 bdb_idl_cache_entry_t *bi_idl_lru_head;
227 bdb_idl_cache_entry_t *bi_idl_lru_tail;
228 ldap_pvt_thread_rdwr_t bi_idl_tree_rwlock;
229 ldap_pvt_thread_mutex_t bi_idl_tree_lrulock;
230 alock_info_t bi_alock_info;
231 char *bi_db_config_path;
232 BerVarray bi_db_config;
233 bdb_monitor_t bi_monitor;
235 #ifdef BDB_MONITOR_IDX
236 ldap_pvt_thread_mutex_t bi_idx_mutex;
238 #endif /* BDB_MONITOR_IDX */
241 #define BDB_IS_OPEN 0x01
242 #define BDB_HAS_CONFIG 0x02
243 #define BDB_UPD_CONFIG 0x04
244 #define BDB_DEL_INDEX 0x08
245 #define BDB_RE_OPEN 0x10
247 int bi_modrdns; /* number of modrdns completed */
248 ldap_pvt_thread_mutex_t bi_modrdns_mutex;
252 #define bi_id2entry bi_databases[BDB_ID2ENTRY]
253 #define bi_dn2id bi_databases[BDB_DN2ID]
256 struct bdb_lock_info {
257 struct bdb_lock_info *bli_next;
265 BDB_LOCKER boi_locker;
268 struct bdb_lock_info *boi_locks; /* used when no txn */
271 #define DB_OPEN(db, file, name, type, flags, mode) \
272 ((db)->open)(db, file, name, type, flags, mode)
274 #if DB_VERSION_MAJOR < 4
275 #define LOCK_DETECT(env,f,t,a) lock_detect(env, f, t, a)
276 #define LOCK_GET(env,i,f,o,m,l) lock_get(env, i, f, o, m, l)
277 #define LOCK_PUT(env,l) lock_put(env, l)
278 #define TXN_CHECKPOINT(env,k,m,f) txn_checkpoint(env, k, m, f)
279 #define TXN_BEGIN(env,p,t,f) txn_begin((env), p, t, f)
280 #define TXN_PREPARE(txn,gid) txn_prepare((txn), (gid))
281 #define TXN_COMMIT(txn,f) txn_commit((txn), (f))
282 #define TXN_ABORT(txn) txn_abort((txn))
283 #define TXN_ID(txn) txn_id(txn)
284 #define XLOCK_ID(env, locker) lock_id(env, locker)
285 #define XLOCK_ID_FREE(env, locker) lock_id_free(env, locker)
287 #define LOCK_DETECT(env,f,t,a) (env)->lock_detect(env, f, t, a)
288 #define LOCK_GET(env,i,f,o,m,l) (env)->lock_get(env, i, f, o, m, l)
289 #define LOCK_PUT(env,l) (env)->lock_put(env, l)
290 #define TXN_CHECKPOINT(env,k,m,f) (env)->txn_checkpoint(env, k, m, f)
291 #define TXN_BEGIN(env,p,t,f) (env)->txn_begin((env), p, t, f)
292 #define TXN_PREPARE(txn,g) (txn)->prepare((txn), (g))
293 #define TXN_COMMIT(txn,f) (txn)->commit((txn), (f))
294 #define TXN_ABORT(txn) (txn)->abort((txn))
295 #define TXN_ID(txn) (txn)->id(txn)
296 #define XLOCK_ID(env, locker) (env)->lock_id(env, locker)
297 #define XLOCK_ID_FREE(env, locker) (env)->lock_id_free(env, locker)
299 /* BDB 4.1.17 adds txn arg to db->open */
300 #if DB_VERSION_FULL >= 0x04010011
302 #define DB_OPEN(db, file, name, type, flags, mode) \
303 ((db)->open)(db, NULL, file, name, type, flags, mode)
306 /* BDB 4.6.18 makes locker a struct instead of an int */
307 #if DB_VERSION_FULL >= 0x04060012
309 #define TXN_ID(txn) (txn)->locker
314 #ifndef DB_BUFFER_SMALL
315 #define DB_BUFFER_SMALL ENOMEM
318 #define BDB_REUSE_LOCKERS
320 #define BDB_CSN_COMMIT 0
321 #define BDB_CSN_ABORT 1
322 #define BDB_CSN_RETRY 2
324 /* Copy an ID "src" to pointer "dst" in big-endian byte order */
325 #define BDB_ID2DISK( src, dst ) \
326 do { int i0; ID tmp; unsigned char *_p; \
327 tmp = (src); _p = (unsigned char *)(dst); \
328 for ( i0=sizeof(ID)-1; i0>=0; i0-- ) { \
329 _p[i0] = tmp & 0xff; tmp >>= 8; \
333 /* Copy a pointer "src" to a pointer "dst" from big-endian to native order */
334 #define BDB_DISK2ID( src, dst ) \
335 do { int i0; ID tmp = 0; unsigned char *_p; \
336 _p = (unsigned char *)(src); \
337 for ( i0=0; i0<sizeof(ID); i0++ ) { \
338 tmp <<= 8; tmp |= *_p++; \
344 /* for the cache of attribute information (which are indexed, etc.) */
345 typedef struct bdb_attrinfo {
346 AttributeDescription *ai_desc; /* attribute description cn;lang-en */
347 slap_mask_t ai_indexmask; /* how the attr is indexed */
348 slap_mask_t ai_newmask; /* new settings to replace old mask */
349 #ifdef LDAP_COMP_MATCH
350 ComponentReference* ai_cr; /*component indexing*/
354 /* These flags must not clash with SLAP_INDEX flags or ops in slap.h! */
355 #define BDB_INDEX_DELETING 0x8000U /* index is being modified */
356 #define BDB_INDEX_UPDATE_OP 0x03 /* performing an index update */
358 /* For slapindex to record which attrs in an entry belong to which
361 typedef struct AttrList {
362 struct AttrList *next;
366 typedef struct IndexRec {
371 #include "proto-bdb.h"
373 #endif /* _BACK_BDB_H_ */