1 /* back-bdb.h - bdb back-end header file */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 2000-2005 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 BDB_SUBENTRIES 1
31 #define DN_BASE_PREFIX SLAP_INDEX_EQUALITY_PREFIX
32 #define DN_ONE_PREFIX '%'
33 #define DN_SUBTREE_PREFIX '@'
35 #define DBTzero(t) (memset((t), 0, sizeof(DBT)))
36 #define DBT2bv(t,bv) ((bv)->bv_val = (t)->data, \
37 (bv)->bv_len = (t)->size)
38 #define bv2DBT(bv,t) ((t)->data = (bv)->bv_val, \
39 (t)->size = (bv)->bv_len )
41 #define BDB_TXN_RETRIES 16
43 #define BDB_MAX_ADD_LOOP 30
46 #define BDB_TMP_SUBDIR "tmp"
47 #define BDB_LG_SUBDIR "log"
48 #define BDB_DATA_SUBDIR "data"
51 #define BDB_SUFFIX ".bdb"
52 #define BDB_ID2ENTRY 0
56 /* The bdb on-disk entry format is pretty space-inefficient. Average
57 * sized user entries are 3-4K each. You need at least two entries to
58 * fit into a single database page, more is better. 64K is BDB's
59 * upper bound. Smaller pages are better for concurrency.
61 #ifndef BDB_ID2ENTRY_PAGESIZE
62 #define BDB_ID2ENTRY_PAGESIZE 16384
66 #define BDB_PAGESIZE 4096 /* BDB's original default */
69 #define DEFAULT_CACHE_SIZE 1000
71 /* The default search IDL stack cache depth */
72 #define DEFAULT_SEARCH_STACK_DEPTH 16
74 /* The minimum we can function with */
75 #define MINIMUM_SEARCH_STACK_DEPTH 8
77 typedef struct bdb_idl_cache_entry_s {
79 ldap_pvt_thread_rdwr_t idl_entry_rwlock;
82 struct bdb_idl_cache_entry_s* idl_lru_prev;
83 struct bdb_idl_cache_entry_s* idl_lru_next;
84 } bdb_idl_cache_entry_t;
86 /* BDB backend specific entry info */
87 typedef struct bdb_entry_info {
88 struct bdb_entry_info *bei_parent;
91 /* we use the bei_id as a lockobj, but we need to make the size != 4
92 * to avoid conflicting with BDB's internal locks. So add a byte here
93 * that is always zero.
98 #define CACHE_ENTRY_DELETED 1
99 #define CACHE_ENTRY_NO_KIDS 2
100 #define CACHE_ENTRY_NOT_LINKED 4
101 #define CACHE_ENTRY_NO_GRANDKIDS 8
102 #define CACHE_ENTRY_LOADING 0x10
105 * remaining fields require backend cache lock to access
107 struct berval bei_nrdn;
109 struct berval bei_rdn;
110 int bei_modrdns; /* track renames */
111 int bei_ckids; /* number of kids cached */
112 int bei_dkids; /* number of kids on-disk, plus 1 */
116 #ifdef SLAP_ZONE_ALLOC
117 struct bdb_info *bei_bdb;
120 ldap_pvt_thread_mutex_t bei_kids_mutex;
122 struct bdb_entry_info *bei_lrunext; /* for cache lru list */
123 struct bdb_entry_info *bei_lruprev;
126 #define BEI(e) ((EntryInfo *) ((e)->e_private))
128 /* for the in-core cache of entries */
129 typedef struct bdb_cache {
132 int c_eiused; /* EntryInfo's in use */
134 EntryInfo *c_eifree; /* free list */
136 EntryInfo *c_lruhead; /* lru - add accessed entries here */
137 EntryInfo *c_lrutail; /* lru - rem lru entries from here */
138 ldap_pvt_thread_rdwr_t c_rwlock;
139 ldap_pvt_thread_mutex_t lru_mutex;
140 u_int32_t c_locker; /* used by lru cleaner */
141 #ifdef SLAP_ZONE_ALLOC
146 #define CACHE_READ_LOCK 0
147 #define CACHE_WRITE_LOCK 1
149 #define BDB_INDICES 128
159 /* DB_ENV parameters */
160 /* The DB_ENV can be tuned via DB_CONFIG */
162 u_int32_t bi_dbenv_xflags; /* extra flags */
166 struct bdb_db_info **bi_databases;
167 ldap_pvt_thread_mutex_t bi_database_mutex;
168 int bi_db_opflags; /* db-specific flags */
170 slap_mask_t bi_defaultmask;
173 void *bi_search_stack;
174 int bi_search_stack_depth;
178 u_int32_t bi_txn_cp_min;
179 u_int32_t bi_txn_cp_kbyte;
185 ldap_pvt_thread_mutex_t bi_lastid_mutex;
186 int bi_idl_cache_max_size;
187 int bi_idl_cache_size;
188 Avlnode *bi_idl_tree;
189 bdb_idl_cache_entry_t *bi_idl_lru_head;
190 bdb_idl_cache_entry_t *bi_idl_lru_tail;
191 ldap_pvt_thread_rdwr_t bi_idl_tree_rwlock;
192 ldap_pvt_thread_mutex_t bi_idl_tree_lrulock;
193 alock_info_t bi_alock_info;
194 char *bi_db_config_path;
195 BerVarray bi_db_config;
197 int bi_db_has_config;
200 #define bi_id2entry bi_databases[BDB_ID2ENTRY]
201 #define bi_dn2id bi_databases[BDB_DN2ID]
203 struct bdb_lock_info {
204 struct bdb_lock_info *bli_next;
213 u_int32_t boi_locker;
215 struct bdb_lock_info *boi_locks; /* used when no txn */
218 #define DB_OPEN(db, file, name, type, flags, mode) \
219 (db)->open(db, file, name, type, flags, mode)
221 #if DB_VERSION_MAJOR < 4
222 #define LOCK_DETECT(env,f,t,a) lock_detect(env, f, t, a)
223 #define LOCK_GET(env,i,f,o,m,l) lock_get(env, i, f, o, m, l)
224 #define LOCK_PUT(env,l) lock_put(env, l)
225 #define TXN_CHECKPOINT(env,k,m,f) txn_checkpoint(env, k, m, f)
226 #define TXN_BEGIN(env,p,t,f) txn_begin((env), p, t, f)
227 #define TXN_PREPARE(txn,gid) txn_prepare((txn), (gid))
228 #define TXN_COMMIT(txn,f) txn_commit((txn), (f))
229 #define TXN_ABORT(txn) txn_abort((txn))
230 #define TXN_ID(txn) txn_id(txn)
231 #define XLOCK_ID(env, locker) lock_id(env, locker)
232 #define XLOCK_ID_FREE(env, locker) lock_id_free(env, locker)
234 #define LOCK_DETECT(env,f,t,a) (env)->lock_detect(env, f, t, a)
235 #define LOCK_GET(env,i,f,o,m,l) (env)->lock_get(env, i, f, o, m, l)
236 #define LOCK_PUT(env,l) (env)->lock_put(env, l)
237 #define TXN_CHECKPOINT(env,k,m,f) (env)->txn_checkpoint(env, k, m, f)
238 #define TXN_BEGIN(env,p,t,f) (env)->txn_begin((env), p, t, f)
239 #define TXN_PREPARE(txn,g) (txn)->prepare((txn), (g))
240 #define TXN_COMMIT(txn,f) (txn)->commit((txn), (f))
241 #define TXN_ABORT(txn) (txn)->abort((txn))
242 #define TXN_ID(txn) (txn)->id(txn)
243 #define XLOCK_ID(env, locker) (env)->lock_id(env, locker)
244 #define XLOCK_ID_FREE(env, locker) (env)->lock_id_free(env, locker)
246 /* BDB 4.1.17 adds txn arg to db->open */
247 #if DB_VERSION_FULL >= 0x04010011
249 #define DB_OPEN(db, file, name, type, flags, mode) \
250 (db)->open(db, NULL, file, name, type, flags, mode)
255 #define BDB_REUSE_LOCKERS
257 #define BDB_CSN_COMMIT 0
258 #define BDB_CSN_ABORT 1
259 #define BDB_CSN_RETRY 2
261 /* Copy an ID "src" to pointer "dst" in big-endian byte order */
262 #define BDB_ID2DISK( src, dst ) \
263 do { int i0; ID tmp; unsigned char *_p; \
264 tmp = (src); _p = (char *)(dst); \
265 for ( i0=sizeof(ID)-1; i0>=0; i0-- ) { \
266 _p[i0] = tmp & 0xff; tmp >>= 8; \
270 /* Copy a pointer "src" to a pointer "dst" from big-endian to native order */
271 #define BDB_DISK2ID( src, dst ) \
272 do { int i0; ID tmp = 0; unsigned char *_p; \
273 _p = (unsigned char *)(src); \
274 for ( i0=0; i0<sizeof(ID); i0++ ) { \
275 tmp <<= 8; tmp |= *_p++; \
281 #include "proto-bdb.h"
283 #endif /* _BACK_BDB_H_ */