]> git.sur5r.net Git - openldap/blob - servers/slapd/back-bdb/back-bdb.h
ITS#5183
[openldap] / servers / slapd / back-bdb / back-bdb.h
1 /* back-bdb.h - bdb back-end header file */
2 /* $OpenLDAP$ */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4  *
5  * Copyright 2000-2007 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 #ifndef _BACK_BDB_H_
18 #define _BACK_BDB_H_
19
20 #include <portable.h>
21 #include "slap.h"
22 #include <db.h>
23 #include "alock.h"
24
25 LDAP_BEGIN_DECL
26
27 #define DB_VERSION_FULL ((DB_VERSION_MAJOR << 24) | (DB_VERSION_MINOR << 16) | DB_VERSION_PATCH)
28
29 #define DN_BASE_PREFIX          SLAP_INDEX_EQUALITY_PREFIX
30 #define DN_ONE_PREFIX           '%'
31 #define DN_SUBTREE_PREFIX       '@'
32
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 )
38
39 #define BDB_TXN_RETRIES         16
40
41 #define BDB_MAX_ADD_LOOP        30
42
43 #define BDB_SUFFIX              ".bdb"
44 #define BDB_ID2ENTRY    0
45 #define BDB_DN2ID               1
46 #define BDB_NDB                 2
47
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.
52  */
53 #ifndef BDB_ID2ENTRY_PAGESIZE
54 #define BDB_ID2ENTRY_PAGESIZE   16384
55 #endif
56
57 #ifndef BDB_PAGESIZE
58 #define BDB_PAGESIZE    4096    /* BDB's original default */
59 #endif
60
61 /* 4.6.18 redefines cursor->locker */
62 #if DB_VERSION_FULL >= 0x04060012
63
64 struct __db_locker {
65         u_int32_t       id;
66 };
67
68 typedef struct __db_locker * BDB_LOCKER;
69
70 extern int __lock_getlocker(DB_LOCKTAB *lt, u_int32_t locker, int create, DB_LOCKER **ret);
71
72 #define CURSOR_SETLOCKER(cursor, id)    cursor->locker = id
73 #define CURSOR_GETLOCKER(cursor)        cursor->locker
74 #else
75
76 typedef u_int32_t BDB_LOCKER;
77
78 #define CURSOR_SETLOCKER(cursor, id)    cursor->locker = id
79 #define CURSOR_GETLOCKER(cursor)        cursor->locker
80
81 #endif
82
83 #define DEFAULT_CACHE_SIZE     1000
84
85 /* The default search IDL stack cache depth */
86 #define DEFAULT_SEARCH_STACK_DEPTH      16
87
88 /* The minimum we can function with */
89 #define MINIMUM_SEARCH_STACK_DEPTH      8
90
91 typedef struct bdb_idl_cache_entry_s {
92         struct berval kstr;
93         ID      *idl;
94         DB      *db;
95         int             idl_flags;
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;
99
100 /* BDB backend specific entry info */
101 typedef struct bdb_entry_info {
102         struct bdb_entry_info *bei_parent;
103         ID bei_id;
104
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.
108          */
109         char bei_lockpad;
110
111         short bei_state;
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
120 #define CACHE_ENTRY_NOT_CACHED  0x100
121         int bei_finders;
122
123         /*
124          * remaining fields require backend cache lock to access
125          */
126         struct berval bei_nrdn;
127 #ifdef BDB_HIER
128         struct berval bei_rdn;
129         int     bei_modrdns;    /* track renames */
130         int     bei_ckids;      /* number of kids cached */
131         int     bei_dkids;      /* number of kids on-disk, plus 1 */
132 #endif
133         Entry   *bei_e;
134         Avlnode *bei_kids;
135 #ifdef SLAP_ZONE_ALLOC
136         struct bdb_info *bei_bdb;
137         int bei_zseq;   
138 #endif
139         ldap_pvt_thread_mutex_t bei_kids_mutex;
140         
141         struct bdb_entry_info   *bei_lrunext;   /* for cache lru list */
142         struct bdb_entry_info   *bei_lruprev;
143 } EntryInfo;
144 #undef BEI
145 #define BEI(e)  ((EntryInfo *) ((e)->e_private))
146
147 /* for the in-core cache of entries */
148 typedef struct bdb_cache {
149         EntryInfo       *c_eifree;      /* free list */
150         Avlnode         *c_idtree;
151         EntryInfo       *c_lruhead;     /* lru - add accessed entries here */
152         EntryInfo       *c_lrutail;     /* lru - rem lru entries from here */
153         EntryInfo       c_dntree;
154         unsigned        c_maxsize;
155         int             c_cursize;
156         unsigned        c_minfree;
157         unsigned        c_eimax;
158         int             c_eiused;       /* EntryInfo's in use */
159         int             c_leaves;       /* EntryInfo leaf nodes */
160         int             c_purging;
161         BDB_LOCKER      c_locker;       /* used by lru cleaner */
162         ldap_pvt_thread_rdwr_t c_rwlock;
163         ldap_pvt_thread_mutex_t c_lru_mutex;
164         ldap_pvt_thread_mutex_t c_count_mutex;
165         ldap_pvt_thread_mutex_t c_eifree_mutex;
166 #ifdef SLAP_ZONE_ALLOC
167         void *c_zctx;
168 #endif
169 } Cache;
170  
171 #define CACHE_READ_LOCK                0
172 #define CACHE_WRITE_LOCK       1
173  
174 #define BDB_INDICES             128
175
176 struct bdb_db_info {
177         struct berval   bdi_name;
178         DB                      *bdi_db;
179 };
180
181 typedef struct bdb_monitor_t {
182         void            *bdm_cb;
183         struct berval   bdm_ndn;
184 } bdb_monitor_t;
185
186 /* From ldap_rq.h */
187 struct re_s;
188
189 struct bdb_info {
190         DB_ENV          *bi_dbenv;
191
192         /* DB_ENV parameters */
193         /* The DB_ENV can be tuned via DB_CONFIG */
194         char            *bi_dbenv_home;
195         u_int32_t       bi_dbenv_xflags; /* extra flags */
196         int                     bi_dbenv_mode;
197
198         int                     bi_ndatabases;
199         struct bdb_db_info **bi_databases;
200         ldap_pvt_thread_mutex_t bi_database_mutex;
201         int             bi_db_opflags;  /* db-specific flags */
202
203         slap_mask_t     bi_defaultmask;
204         Cache           bi_cache;
205         struct bdb_attrinfo             **bi_attrs;
206         int                     bi_nattrs;
207         void            *bi_search_stack;
208         int             bi_search_stack_depth;
209         int             bi_linear_index;
210
211         int                     bi_txn_cp;
212         u_int32_t       bi_txn_cp_min;
213         u_int32_t       bi_txn_cp_kbyte;
214         struct re_s             *bi_txn_cp_task;
215         struct re_s             *bi_index_task;
216
217         int                     bi_lock_detect;
218         long            bi_shm_key;
219
220         ID                      bi_lastid;
221         ldap_pvt_thread_mutex_t bi_lastid_mutex;
222         unsigned        bi_idl_cache_max_size;
223         int             bi_idl_cache_size;
224         Avlnode         *bi_idl_tree;
225         bdb_idl_cache_entry_t   *bi_idl_lru_head;
226         bdb_idl_cache_entry_t   *bi_idl_lru_tail;
227         ldap_pvt_thread_rdwr_t bi_idl_tree_rwlock;
228         ldap_pvt_thread_mutex_t bi_idl_tree_lrulock;
229         alock_info_t    bi_alock_info;
230         char            *bi_db_config_path;
231         BerVarray       bi_db_config;
232         bdb_monitor_t   bi_monitor;
233
234 #ifdef BDB_MONITOR_IDX
235         ldap_pvt_thread_mutex_t bi_idx_mutex;
236         Avlnode         *bi_idx;
237 #endif /* BDB_MONITOR_IDX */
238
239         int             bi_flags;
240 #define BDB_IS_OPEN             0x01
241 #define BDB_HAS_CONFIG  0x02
242 #define BDB_UPD_CONFIG  0x04
243 #define BDB_DEL_INDEX   0x08
244 #define BDB_RE_OPEN             0x10
245 #ifdef BDB_HIER
246         int             bi_modrdns;             /* number of modrdns completed */
247         ldap_pvt_thread_mutex_t bi_modrdns_mutex;
248 #endif
249 };
250
251 #define bi_id2entry     bi_databases[BDB_ID2ENTRY]
252 #define bi_dn2id        bi_databases[BDB_DN2ID]
253
254
255 struct bdb_lock_info {
256         struct bdb_lock_info *bli_next;
257         ID              bli_id;
258         DB_LOCK bli_lock;
259 };
260
261 struct bdb_op_info {
262         BackendDB*      boi_bdb;
263         DB_TXN*         boi_txn;
264         BDB_LOCKER      boi_locker;
265         u_int32_t       boi_err;
266         int             boi_acl_cache;
267         struct bdb_lock_info *boi_locks;        /* used when no txn */
268 };
269
270 #define DB_OPEN(db, file, name, type, flags, mode) \
271         ((db)->open)(db, file, name, type, flags, mode)
272
273 #if DB_VERSION_MAJOR < 4
274 #define LOCK_DETECT(env,f,t,a)          lock_detect(env, f, t, a)
275 #define LOCK_GET(env,i,f,o,m,l)         lock_get(env, i, f, o, m, l)
276 #define LOCK_PUT(env,l)                 lock_put(env, l)
277 #define TXN_CHECKPOINT(env,k,m,f)       txn_checkpoint(env, k, m, f)
278 #define TXN_BEGIN(env,p,t,f)            txn_begin((env), p, t, f)
279 #define TXN_PREPARE(txn,gid)            txn_prepare((txn), (gid))
280 #define TXN_COMMIT(txn,f)                       txn_commit((txn), (f))
281 #define TXN_ABORT(txn)                          txn_abort((txn))
282 #define TXN_ID(txn)                                     txn_id(txn)
283 #define XLOCK_ID(env, locker)           lock_id(env, locker)
284 #define XLOCK_ID_FREE(env, locker)      lock_id_free(env, locker)
285 #else
286 #define LOCK_DETECT(env,f,t,a)          (env)->lock_detect(env, f, t, a)
287 #define LOCK_GET(env,i,f,o,m,l)         (env)->lock_get(env, i, f, o, m, l)
288 #define LOCK_PUT(env,l)                 (env)->lock_put(env, l)
289 #define TXN_CHECKPOINT(env,k,m,f)       (env)->txn_checkpoint(env, k, m, f)
290 #define TXN_BEGIN(env,p,t,f)            (env)->txn_begin((env), p, t, f)
291 #define TXN_PREPARE(txn,g)                      (txn)->prepare((txn), (g))
292 #define TXN_COMMIT(txn,f)                       (txn)->commit((txn), (f))
293 #define TXN_ABORT(txn)                          (txn)->abort((txn))
294 #define TXN_ID(txn)                                     (txn)->id(txn)
295 #define XLOCK_ID(env, locker)           (env)->lock_id(env, locker)
296 #define XLOCK_ID_FREE(env, locker)      (env)->lock_id_free(env, locker)
297
298 /* BDB 4.1.17 adds txn arg to db->open */
299 #if DB_VERSION_FULL >= 0x04010011
300 #undef DB_OPEN
301 #define DB_OPEN(db, file, name, type, flags, mode) \
302         ((db)->open)(db, NULL, file, name, type, flags, mode)
303 #endif
304
305 /* BDB 4.6.18 makes locker a struct instead of an int */
306 #if DB_VERSION_FULL >= 0x04060012
307 #undef TXN_ID
308 #define TXN_ID(txn)     (txn)->locker
309 #endif
310
311 #endif
312
313 #ifndef DB_BUFFER_SMALL
314 #define DB_BUFFER_SMALL                 ENOMEM
315 #endif
316
317 #define BDB_REUSE_LOCKERS
318
319 #define BDB_CSN_COMMIT  0
320 #define BDB_CSN_ABORT   1
321 #define BDB_CSN_RETRY   2
322
323 /* Copy an ID "src" to pointer "dst" in big-endian byte order */
324 #define BDB_ID2DISK( src, dst ) \
325         do { int i0; ID tmp; unsigned char *_p; \
326                 tmp = (src); _p = (unsigned char *)(dst);       \
327                 for ( i0=sizeof(ID)-1; i0>=0; i0-- ) {  \
328                         _p[i0] = tmp & 0xff; tmp >>= 8; \
329                 } \
330         } while(0)
331
332 /* Copy a pointer "src" to a pointer "dst" from big-endian to native order */
333 #define BDB_DISK2ID( src, dst ) \
334         do { int i0; ID tmp = 0; unsigned char *_p;     \
335                 _p = (unsigned char *)(src);    \
336                 for ( i0=0; i0<sizeof(ID); i0++ ) {     \
337                         tmp <<= 8; tmp |= *_p++;        \
338                 } *(dst) = tmp; \
339         } while (0)
340
341 LDAP_END_DECL
342
343 /* for the cache of attribute information (which are indexed, etc.) */
344 typedef struct bdb_attrinfo {
345         AttributeDescription *ai_desc; /* attribute description cn;lang-en */
346         slap_mask_t ai_indexmask;       /* how the attr is indexed      */
347         slap_mask_t ai_newmask; /* new settings to replace old mask */
348 #ifdef LDAP_COMP_MATCH
349         ComponentReference* ai_cr; /*component indexing*/
350 #endif
351 } AttrInfo;
352
353 /* These flags must not clash with SLAP_INDEX flags or ops in slap.h! */
354 #define BDB_INDEX_DELETING      0x8000U /* index is being modified */
355 #define BDB_INDEX_UPDATE_OP     0x03    /* performing an index update */
356
357 /* For slapindex to record which attrs in an entry belong to which
358  * index database 
359  */
360 typedef struct AttrList {
361         struct AttrList *next;
362         Attribute *attr;
363 } AttrList;
364
365 typedef struct IndexRec {
366         AttrInfo *ai;
367         AttrList *attrs;
368 } IndexRec;
369
370 #include "proto-bdb.h"
371
372 #endif /* _BACK_BDB_H_ */