Patrick Welche <prlw1@cam.ac.uk>
[netbsd-mini2440.git] / external / bsd / openldap / dist / servers / slapd / back-bdb / back-bdb.h
blob276030adac1639ad2705eab06f4205d58c2fae3d
1 /* back-bdb.h - bdb back-end header file */
2 /* $OpenLDAP: pkg/ldap/servers/slapd/back-bdb/back-bdb.h,v 1.141.2.14 2008/05/01 21:39:35 quanah Exp $ */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 2000-2008 The OpenLDAP Foundation.
6 * All rights reserved.
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.
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>.
17 #ifndef _BACK_BDB_H_
18 #define _BACK_BDB_H_
20 #include <portable.h>
21 #include "slap.h"
22 #include <db.h>
23 #include "alock.h"
25 LDAP_BEGIN_DECL
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
45 #define BDB_DN2ID 1
46 #define BDB_NDB 2
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
55 #endif
57 #ifndef BDB_PAGESIZE
58 #define BDB_PAGESIZE 4096 /* BDB's original default */
59 #endif
61 /* 4.6.18 redefines cursor->locker */
62 #if DB_VERSION_FULL >= 0x04060012
64 struct __db_locker {
65 u_int32_t id;
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
74 #define BDB_LOCKID(locker) locker->id
75 #else
77 typedef u_int32_t BDB_LOCKER;
79 #define CURSOR_SETLOCKER(cursor, id) cursor->locker = id
80 #define CURSOR_GETLOCKER(cursor) cursor->locker
81 #define BDB_LOCKID(locker) locker
83 #endif
85 #define DEFAULT_CACHE_SIZE 1000
87 /* The default search IDL stack cache depth */
88 #define DEFAULT_SEARCH_STACK_DEPTH 16
90 /* The minimum we can function with */
91 #define MINIMUM_SEARCH_STACK_DEPTH 8
93 typedef struct bdb_idl_cache_entry_s {
94 struct berval kstr;
95 ID *idl;
96 DB *db;
97 int idl_flags;
98 struct bdb_idl_cache_entry_s* idl_lru_prev;
99 struct bdb_idl_cache_entry_s* idl_lru_next;
100 } bdb_idl_cache_entry_t;
102 /* BDB backend specific entry info */
103 typedef struct bdb_entry_info {
104 struct bdb_entry_info *bei_parent;
105 ID bei_id;
107 /* we use the bei_id as a lockobj, but we need to make the size != 4
108 * to avoid conflicting with BDB's internal locks. So add a byte here
109 * that is always zero.
111 short bei_lockpad;
113 short bei_state;
114 #define CACHE_ENTRY_DELETED 1
115 #define CACHE_ENTRY_NO_KIDS 2
116 #define CACHE_ENTRY_NOT_LINKED 4
117 #define CACHE_ENTRY_NO_GRANDKIDS 8
118 #define CACHE_ENTRY_LOADING 0x10
119 #define CACHE_ENTRY_WALKING 0x20
120 #define CACHE_ENTRY_ONELEVEL 0x40
121 #define CACHE_ENTRY_REFERENCED 0x80
122 #define CACHE_ENTRY_NOT_CACHED 0x100
123 int bei_finders;
126 * remaining fields require backend cache lock to access
128 struct berval bei_nrdn;
129 #ifdef BDB_HIER
130 struct berval bei_rdn;
131 int bei_modrdns; /* track renames */
132 int bei_ckids; /* number of kids cached */
133 int bei_dkids; /* number of kids on-disk, plus 1 */
134 #endif
135 Entry *bei_e;
136 Avlnode *bei_kids;
137 #ifdef SLAP_ZONE_ALLOC
138 struct bdb_info *bei_bdb;
139 int bei_zseq;
140 #endif
141 ldap_pvt_thread_mutex_t bei_kids_mutex;
143 struct bdb_entry_info *bei_lrunext; /* for cache lru list */
144 struct bdb_entry_info *bei_lruprev;
145 } EntryInfo;
146 #undef BEI
147 #define BEI(e) ((EntryInfo *) ((e)->e_private))
149 /* for the in-core cache of entries */
150 typedef struct bdb_cache {
151 EntryInfo *c_eifree; /* free list */
152 Avlnode *c_idtree;
153 EntryInfo *c_lruhead; /* lru - add accessed entries here */
154 EntryInfo *c_lrutail; /* lru - rem lru entries from here */
155 EntryInfo c_dntree;
156 unsigned c_maxsize;
157 int c_cursize;
158 unsigned c_minfree;
159 unsigned c_eimax;
160 int c_eiused; /* EntryInfo's in use */
161 int c_leaves; /* EntryInfo leaf nodes */
162 int c_purging;
163 BDB_LOCKER c_locker; /* used by lru cleaner */
164 ldap_pvt_thread_rdwr_t c_rwlock;
165 ldap_pvt_thread_mutex_t c_lru_mutex;
166 ldap_pvt_thread_mutex_t c_count_mutex;
167 ldap_pvt_thread_mutex_t c_eifree_mutex;
168 #ifdef SLAP_ZONE_ALLOC
169 void *c_zctx;
170 #endif
171 } Cache;
173 #define CACHE_READ_LOCK 0
174 #define CACHE_WRITE_LOCK 1
176 #define BDB_INDICES 128
178 struct bdb_db_info {
179 struct berval bdi_name;
180 DB *bdi_db;
183 #ifdef LDAP_DEVEL
184 #define BDB_MONITOR_IDX
185 #endif /* LDAP_DEVEL */
187 typedef struct bdb_monitor_t {
188 void *bdm_cb;
189 struct berval bdm_ndn;
190 } bdb_monitor_t;
192 /* From ldap_rq.h */
193 struct re_s;
195 struct bdb_info {
196 DB_ENV *bi_dbenv;
198 /* DB_ENV parameters */
199 /* The DB_ENV can be tuned via DB_CONFIG */
200 char *bi_dbenv_home;
201 u_int32_t bi_dbenv_xflags; /* extra flags */
202 int bi_dbenv_mode;
204 int bi_ndatabases;
205 struct bdb_db_info **bi_databases;
206 ldap_pvt_thread_mutex_t bi_database_mutex;
207 int bi_db_opflags; /* db-specific flags */
209 slap_mask_t bi_defaultmask;
210 Cache bi_cache;
211 struct bdb_attrinfo **bi_attrs;
212 int bi_nattrs;
213 void *bi_search_stack;
214 int bi_search_stack_depth;
215 int bi_linear_index;
217 int bi_txn_cp;
218 u_int32_t bi_txn_cp_min;
219 u_int32_t bi_txn_cp_kbyte;
220 struct re_s *bi_txn_cp_task;
221 struct re_s *bi_index_task;
223 int bi_lock_detect;
224 long bi_shm_key;
226 ID bi_lastid;
227 ldap_pvt_thread_mutex_t bi_lastid_mutex;
228 unsigned bi_idl_cache_max_size;
229 int bi_idl_cache_size;
230 Avlnode *bi_idl_tree;
231 bdb_idl_cache_entry_t *bi_idl_lru_head;
232 bdb_idl_cache_entry_t *bi_idl_lru_tail;
233 ldap_pvt_thread_rdwr_t bi_idl_tree_rwlock;
234 ldap_pvt_thread_mutex_t bi_idl_tree_lrulock;
235 alock_info_t bi_alock_info;
236 char *bi_db_config_path;
237 BerVarray bi_db_config;
238 char *bi_db_crypt_file;
239 struct berval bi_db_crypt_key;
240 bdb_monitor_t bi_monitor;
242 #ifdef BDB_MONITOR_IDX
243 ldap_pvt_thread_mutex_t bi_idx_mutex;
244 Avlnode *bi_idx;
245 #endif /* BDB_MONITOR_IDX */
247 int bi_flags;
248 #define BDB_IS_OPEN 0x01
249 #define BDB_HAS_CONFIG 0x02
250 #define BDB_UPD_CONFIG 0x04
251 #define BDB_DEL_INDEX 0x08
252 #define BDB_RE_OPEN 0x10
253 #ifdef BDB_HIER
254 int bi_modrdns; /* number of modrdns completed */
255 ldap_pvt_thread_mutex_t bi_modrdns_mutex;
256 #endif
259 #define bi_id2entry bi_databases[BDB_ID2ENTRY]
260 #define bi_dn2id bi_databases[BDB_DN2ID]
263 struct bdb_lock_info {
264 struct bdb_lock_info *bli_next;
265 ID bli_id;
266 DB_LOCK bli_lock;
269 struct bdb_op_info {
270 OpExtra boi_oe;
271 DB_TXN* boi_txn;
272 u_int32_t boi_err;
273 int boi_acl_cache;
274 struct bdb_lock_info *boi_locks; /* used when no txn */
277 #define DB_OPEN(db, file, name, type, flags, mode) \
278 ((db)->open)(db, file, name, type, flags, mode)
280 #if DB_VERSION_MAJOR < 4
281 #define LOCK_DETECT(env,f,t,a) lock_detect(env, f, t, a)
282 #define LOCK_GET(env,i,f,o,m,l) lock_get(env, i, f, o, m, l)
283 #define LOCK_PUT(env,l) lock_put(env, l)
284 #define TXN_CHECKPOINT(env,k,m,f) txn_checkpoint(env, k, m, f)
285 #define TXN_BEGIN(env,p,t,f) txn_begin((env), p, t, f)
286 #define TXN_PREPARE(txn,gid) txn_prepare((txn), (gid))
287 #define TXN_COMMIT(txn,f) txn_commit((txn), (f))
288 #define TXN_ABORT(txn) txn_abort((txn))
289 #define TXN_ID(txn) txn_id(txn)
290 #define XLOCK_ID(env, locker) lock_id(env, locker)
291 #define XLOCK_ID_FREE(env, locker) lock_id_free(env, locker)
292 #else
293 #define LOCK_DETECT(env,f,t,a) (env)->lock_detect(env, f, t, a)
294 #define LOCK_GET(env,i,f,o,m,l) (env)->lock_get(env, i, f, o, m, l)
295 #define LOCK_PUT(env,l) (env)->lock_put(env, l)
296 #define TXN_CHECKPOINT(env,k,m,f) (env)->txn_checkpoint(env, k, m, f)
297 #define TXN_BEGIN(env,p,t,f) (env)->txn_begin((env), p, t, f)
298 #define TXN_PREPARE(txn,g) (txn)->prepare((txn), (g))
299 #define TXN_COMMIT(txn,f) (txn)->commit((txn), (f))
300 #define TXN_ABORT(txn) (txn)->abort((txn))
301 #define TXN_ID(txn) (txn)->id(txn)
302 #define XLOCK_ID(env, locker) (env)->lock_id(env, locker)
303 #define XLOCK_ID_FREE(env, locker) (env)->lock_id_free(env, locker)
305 /* BDB 4.1.17 adds txn arg to db->open */
306 #if DB_VERSION_FULL >= 0x04010011
307 #undef DB_OPEN
308 #define DB_OPEN(db, file, name, type, flags, mode) \
309 ((db)->open)(db, NULL, file, name, type, flags, mode)
310 #endif
312 /* BDB 4.6.18 makes locker a struct instead of an int */
313 #if DB_VERSION_FULL >= 0x04060012
314 #undef TXN_ID
315 #define TXN_ID(txn) (txn)->locker
316 #endif
318 /* #undef BDB_LOG_DEBUG */
320 #ifdef BDB_LOG_DEBUG
322 /* env->log_printf appeared in 4.4 */
323 #if DB_VERSION_FULL >= 0x04040000
324 #define BDB_LOG_PRINTF(env,txn,fmt,...) (env)->log_printf((env),(txn),(fmt),__VA_ARGS__)
325 #else
326 extern int __db_logmsg(const DB_ENV *env, DB_TXN *txn, const char *op, u_int32_t flags,
327 const char *fmt,...);
328 #define BDB_LOG_PRINTF(env,txn,fmt,...) __db_logmsg((env),(txn),"DIAGNOSTIC",0,(fmt),__VA_ARGS__)
329 #endif
331 /* !BDB_LOG_DEBUG */
332 #elif (defined(__STDC_VERSION__) && __STDC_VERSION__ >= 199901L) || \
333 (defined(__GNUC__) && __GNUC__ >= 3 && !defined(__STRICT_ANSI__))
334 #define BDB_LOG_PRINTF(a,b,c,...)
335 #else
336 #define BDB_LOG_PRINTF (void) /* will evaluate and discard the arguments */
338 #endif /* BDB_LOG_DEBUG */
340 #endif
342 #ifndef DB_BUFFER_SMALL
343 #define DB_BUFFER_SMALL ENOMEM
344 #endif
346 #define BDB_REUSE_LOCKERS
348 #define BDB_CSN_COMMIT 0
349 #define BDB_CSN_ABORT 1
350 #define BDB_CSN_RETRY 2
352 /* Copy an ID "src" to pointer "dst" in big-endian byte order */
353 #define BDB_ID2DISK( src, dst ) \
354 do { int i0; ID tmp; unsigned char *_p; \
355 tmp = (src); _p = (unsigned char *)(dst); \
356 for ( i0=sizeof(ID)-1; i0>=0; i0-- ) { \
357 _p[i0] = tmp & 0xff; tmp >>= 8; \
359 } while(0)
361 /* Copy a pointer "src" to a pointer "dst" from big-endian to native order */
362 #define BDB_DISK2ID( src, dst ) \
363 do { int i0; ID tmp = 0; unsigned char *_p; \
364 _p = (unsigned char *)(src); \
365 for ( i0=0; i0<sizeof(ID); i0++ ) { \
366 tmp <<= 8; tmp |= *_p++; \
367 } *(dst) = tmp; \
368 } while (0)
370 LDAP_END_DECL
372 /* for the cache of attribute information (which are indexed, etc.) */
373 typedef struct bdb_attrinfo {
374 AttributeDescription *ai_desc; /* attribute description cn;lang-en */
375 slap_mask_t ai_indexmask; /* how the attr is indexed */
376 slap_mask_t ai_newmask; /* new settings to replace old mask */
377 #ifdef LDAP_COMP_MATCH
378 ComponentReference* ai_cr; /*component indexing*/
379 #endif
380 } AttrInfo;
382 /* These flags must not clash with SLAP_INDEX flags or ops in slap.h! */
383 #define BDB_INDEX_DELETING 0x8000U /* index is being modified */
384 #define BDB_INDEX_UPDATE_OP 0x03 /* performing an index update */
386 /* For slapindex to record which attrs in an entry belong to which
387 * index database
389 typedef struct AttrList {
390 struct AttrList *next;
391 Attribute *attr;
392 } AttrList;
394 typedef struct IndexRec {
395 AttrInfo *ai;
396 AttrList *attrs;
397 } IndexRec;
399 #include "proto-bdb.h"
401 #endif /* _BACK_BDB_H_ */