]> git.sur5r.net Git - openldap/blob - servers/slapd/back-bdb/back-bdb.h
4a22fa00ddfa2a7df4890c5f56a5960f956e4ee0
[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-2008 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 #define DEFAULT_CACHE_SIZE     1000
62
63 /* The default search IDL stack cache depth */
64 #define DEFAULT_SEARCH_STACK_DEPTH      16
65
66 /* The minimum we can function with */
67 #define MINIMUM_SEARCH_STACK_DEPTH      8
68
69 typedef struct bdb_idl_cache_entry_s {
70         struct berval kstr;
71         ID      *idl;
72         DB      *db;
73         struct bdb_idl_cache_entry_s* idl_lru_prev;
74         struct bdb_idl_cache_entry_s* idl_lru_next;
75 } bdb_idl_cache_entry_t;
76
77 /* BDB backend specific entry info */
78 typedef struct bdb_entry_info {
79         struct bdb_entry_info *bei_parent;
80         ID bei_id;
81
82         /* we use the bei_id as a lockobj, but we need to make the size != 4
83          * to avoid conflicting with BDB's internal locks. So add a byte here
84          * that is always zero.
85          */
86         short bei_lockpad;
87
88         short bei_state;
89 #define CACHE_ENTRY_DELETED     1
90 #define CACHE_ENTRY_NO_KIDS     2
91 #define CACHE_ENTRY_NOT_LINKED  4
92 #define CACHE_ENTRY_NO_GRANDKIDS        8
93 #define CACHE_ENTRY_LOADING     0x10
94 #define CACHE_ENTRY_WALKING     0x20
95 #define CACHE_ENTRY_ONELEVEL    0x40
96
97         /*
98          * remaining fields require backend cache lock to access
99          */
100         struct berval bei_nrdn;
101 #ifdef BDB_HIER
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 */
106 #endif
107         Entry   *bei_e;
108         Avlnode *bei_kids;
109 #ifdef SLAP_ZONE_ALLOC
110         struct bdb_info *bei_bdb;
111         int bei_zseq;   
112 #endif
113         ldap_pvt_thread_mutex_t bei_kids_mutex;
114         
115         struct bdb_entry_info   *bei_lrunext;   /* for cache lru list */
116         struct bdb_entry_info   *bei_lruprev;
117 } EntryInfo;
118 #undef BEI
119 #define BEI(e)  ((EntryInfo *) ((e)->e_private))
120
121 /* for the in-core cache of entries */
122 typedef struct bdb_cache {
123         int             c_maxsize;
124         int             c_cursize;
125         int             c_minfree;
126         int             c_eiused;       /* EntryInfo's in use */
127         int             c_leaves;       /* EntryInfo leaf nodes */
128         EntryInfo       c_dntree;
129         EntryInfo       *c_eifree;      /* free list */
130         Avlnode         *c_idtree;
131         EntryInfo       *c_lruhead;     /* lru - add accessed entries here */
132         EntryInfo       *c_lrutail;     /* lru - rem lru entries from here */
133         ldap_pvt_thread_rdwr_t c_rwlock;
134         ldap_pvt_thread_mutex_t lru_head_mutex;
135         ldap_pvt_thread_mutex_t lru_tail_mutex;
136         u_int32_t       c_locker;       /* used by lru cleaner */
137 #ifdef SLAP_ZONE_ALLOC
138         void *c_zctx;
139 #endif
140 } Cache;
141  
142 #define CACHE_READ_LOCK                0
143 #define CACHE_WRITE_LOCK       1
144  
145 #define BDB_INDICES             128
146
147 struct bdb_db_info {
148         char            *bdi_name;
149         DB                      *bdi_db;
150 };
151
152 /* From ldap_rq.h */
153 struct re_s;
154
155 struct bdb_info {
156         DB_ENV          *bi_dbenv;
157
158         /* DB_ENV parameters */
159         /* The DB_ENV can be tuned via DB_CONFIG */
160         char            *bi_dbenv_home;
161         u_int32_t       bi_dbenv_xflags; /* extra flags */
162         int                     bi_dbenv_mode;
163
164         int                     bi_ndatabases;
165         struct bdb_db_info **bi_databases;
166         ldap_pvt_thread_mutex_t bi_database_mutex;
167         int             bi_db_opflags;  /* db-specific flags */
168
169         slap_mask_t     bi_defaultmask;
170         Cache           bi_cache;
171         struct bdb_attrinfo             **bi_attrs;
172         int                     bi_nattrs;
173         void            *bi_search_stack;
174         int             bi_search_stack_depth;
175         int             bi_linear_index;
176
177         int                     bi_txn_cp;
178         u_int32_t       bi_txn_cp_min;
179         u_int32_t       bi_txn_cp_kbyte;
180         struct re_s             *bi_txn_cp_task;
181         struct re_s             *bi_index_task;
182
183         int                     bi_lock_detect;
184         long            bi_shm_key;
185
186         ID                      bi_lastid;
187         ldap_pvt_thread_mutex_t bi_lastid_mutex;
188         int             bi_idl_cache_max_size;
189         int             bi_idl_cache_size;
190         Avlnode         *bi_idl_tree;
191         bdb_idl_cache_entry_t   *bi_idl_lru_head;
192         bdb_idl_cache_entry_t   *bi_idl_lru_tail;
193         ldap_pvt_thread_rdwr_t bi_idl_tree_rwlock;
194         ldap_pvt_thread_mutex_t bi_idl_tree_lrulock;
195         alock_info_t    bi_alock_info;
196         char            *bi_db_config_path;
197         BerVarray       bi_db_config;
198         int             bi_flags;
199 #define BDB_IS_OPEN             0x01
200 #define BDB_HAS_CONFIG  0x02
201 #define BDB_UPD_CONFIG  0x04
202 #define BDB_DEL_INDEX   0x08
203 #define BDB_RE_OPEN             0x10
204 #ifdef BDB_HIER
205         int             bi_modrdns;             /* number of modrdns completed */
206         ldap_pvt_thread_mutex_t bi_modrdns_mutex;
207 #endif
208 };
209
210 #define bi_id2entry     bi_databases[BDB_ID2ENTRY]
211 #define bi_dn2id        bi_databases[BDB_DN2ID]
212
213 struct bdb_lock_info {
214         struct bdb_lock_info *bli_next;
215         ID              bli_id;
216         DB_LOCK bli_lock;
217 };
218
219 struct bdb_op_info {
220         BackendDB*      boi_bdb;
221         DB_TXN*         boi_txn;
222         u_int32_t       boi_err;
223         u_int32_t       boi_locker;
224         int             boi_acl_cache;
225         struct bdb_lock_info *boi_locks;        /* used when no txn */
226 };
227
228 #define DB_OPEN(db, file, name, type, flags, mode) \
229         ((db)->open)(db, file, name, type, flags, mode)
230
231 #if DB_VERSION_MAJOR < 4
232 #define LOCK_DETECT(env,f,t,a)          lock_detect(env, f, t, a)
233 #define LOCK_GET(env,i,f,o,m,l)         lock_get(env, i, f, o, m, l)
234 #define LOCK_PUT(env,l)                 lock_put(env, l)
235 #define TXN_CHECKPOINT(env,k,m,f)       txn_checkpoint(env, k, m, f)
236 #define TXN_BEGIN(env,p,t,f)            txn_begin((env), p, t, f)
237 #define TXN_PREPARE(txn,gid)            txn_prepare((txn), (gid))
238 #define TXN_COMMIT(txn,f)                       txn_commit((txn), (f))
239 #define TXN_ABORT(txn)                          txn_abort((txn))
240 #define TXN_ID(txn)                                     txn_id(txn)
241 #define XLOCK_ID(env, locker)           lock_id(env, locker)
242 #define XLOCK_ID_FREE(env, locker)      lock_id_free(env, locker)
243 #else
244 #define LOCK_DETECT(env,f,t,a)          (env)->lock_detect(env, f, t, a)
245 #define LOCK_GET(env,i,f,o,m,l)         (env)->lock_get(env, i, f, o, m, l)
246 #define LOCK_PUT(env,l)                 (env)->lock_put(env, l)
247 #define TXN_CHECKPOINT(env,k,m,f)       (env)->txn_checkpoint(env, k, m, f)
248 #define TXN_BEGIN(env,p,t,f)            (env)->txn_begin((env), p, t, f)
249 #define TXN_PREPARE(txn,g)                      (txn)->prepare((txn), (g))
250 #define TXN_COMMIT(txn,f)                       (txn)->commit((txn), (f))
251 #define TXN_ABORT(txn)                          (txn)->abort((txn))
252 #define TXN_ID(txn)                                     (txn)->id(txn)
253 #define XLOCK_ID(env, locker)           (env)->lock_id(env, locker)
254 #define XLOCK_ID_FREE(env, locker)      (env)->lock_id_free(env, locker)
255
256 /* BDB 4.1.17 adds txn arg to db->open */
257 #if DB_VERSION_FULL >= 0x04010011
258 #undef DB_OPEN
259 #define DB_OPEN(db, file, name, type, flags, mode) \
260         ((db)->open)(db, NULL, file, name, type, flags, mode)
261 #endif
262
263 #endif
264
265 #define BDB_REUSE_LOCKERS
266
267 #define BDB_CSN_COMMIT  0
268 #define BDB_CSN_ABORT   1
269 #define BDB_CSN_RETRY   2
270
271 /* Copy an ID "src" to pointer "dst" in big-endian byte order */
272 #define BDB_ID2DISK( src, dst ) \
273         do { int i0; ID tmp; unsigned char *_p; \
274                 tmp = (src); _p = (unsigned char *)(dst);       \
275                 for ( i0=sizeof(ID)-1; i0>=0; i0-- ) {  \
276                         _p[i0] = tmp & 0xff; tmp >>= 8; \
277                 } \
278         } while(0)
279
280 /* Copy a pointer "src" to a pointer "dst" from big-endian to native order */
281 #define BDB_DISK2ID( src, dst ) \
282         do { int i0; ID tmp = 0; unsigned char *_p;     \
283                 _p = (unsigned char *)(src);    \
284                 for ( i0=0; i0<sizeof(ID); i0++ ) {     \
285                         tmp <<= 8; tmp |= *_p++;        \
286                 } *(dst) = tmp; \
287         } while (0)
288
289 LDAP_END_DECL
290
291 /* for the cache of attribute information (which are indexed, etc.) */
292 typedef struct bdb_attrinfo {
293         AttributeDescription *ai_desc; /* attribute description cn;lang-en */
294         slap_mask_t ai_indexmask;       /* how the attr is indexed      */
295         slap_mask_t ai_newmask; /* new settings to replace old mask */
296 #ifdef LDAP_COMP_MATCH
297         ComponentReference* ai_cr; /*component indexing*/
298 #endif
299 } AttrInfo;
300
301 /* These flags must not clash with SLAP_INDEX flags or ops in slap.h! */
302 #define BDB_INDEX_DELETING      0x8000U /* index is being modified */
303 #define BDB_INDEX_UPDATE_OP     0x03    /* performing an index update */
304
305 /* For slapindex to record which attrs in an entry belong to which
306  * index database 
307  */
308 typedef struct AttrList {
309         struct AttrList *next;
310         Attribute *attr;
311 } AttrList;
312
313 typedef struct IndexRec {
314         AttrInfo *ai;
315         AttrList *attrs;
316 } IndexRec;
317
318 #include "proto-bdb.h"
319
320 #endif /* _BACK_BDB_H_ */