]> git.sur5r.net Git - openldap/blob - servers/slapd/back-bdb/back-bdb.h
e7e0fb12541629515d8a956057363435a2dc31e0
[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-2015 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 #define DEFAULT_CACHE_SIZE     1000
58
59 /* The default search IDL stack cache depth */
60 #define DEFAULT_SEARCH_STACK_DEPTH      16
61
62 /* The minimum we can function with */
63 #define MINIMUM_SEARCH_STACK_DEPTH      8
64
65 typedef struct bdb_idl_cache_entry_s {
66         struct berval kstr;
67         ID      *idl;
68         DB      *db;
69         int             idl_flags;
70         struct bdb_idl_cache_entry_s* idl_lru_prev;
71         struct bdb_idl_cache_entry_s* idl_lru_next;
72 } bdb_idl_cache_entry_t;
73
74 /* BDB backend specific entry info */
75 typedef struct bdb_entry_info {
76         struct bdb_entry_info *bei_parent;
77         ID bei_id;
78
79         /* we use the bei_id as a lockobj, but we need to make the size != 4
80          * to avoid conflicting with BDB's internal locks. So add a byte here
81          * that is always zero.
82          */
83         short bei_lockpad;
84
85         short bei_state;
86 #define CACHE_ENTRY_DELETED     1
87 #define CACHE_ENTRY_NO_KIDS     2
88 #define CACHE_ENTRY_NOT_LINKED  4
89 #define CACHE_ENTRY_NO_GRANDKIDS        8
90 #define CACHE_ENTRY_LOADING     0x10
91 #define CACHE_ENTRY_WALKING     0x20
92 #define CACHE_ENTRY_ONELEVEL    0x40
93 #define CACHE_ENTRY_REFERENCED  0x80
94 #define CACHE_ENTRY_NOT_CACHED  0x100
95         int bei_finders;
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         EntryInfo       *c_eifree;      /* free list */
124         Avlnode         *c_idtree;
125         EntryInfo       *c_lruhead;     /* lru - add accessed entries here */
126         EntryInfo       *c_lrutail;     /* lru - rem lru entries from here */
127         EntryInfo       c_dntree;
128         ID              c_maxsize;
129         ID              c_cursize;
130         ID              c_minfree;
131         ID              c_eimax;
132         ID              c_eiused;       /* EntryInfo's in use */
133         ID              c_leaves;       /* EntryInfo leaf nodes */
134         int             c_purging;
135         DB_TXN  *c_txn; /* used by lru cleaner */
136         ldap_pvt_thread_rdwr_t c_rwlock;
137         ldap_pvt_thread_mutex_t c_lru_mutex;
138         ldap_pvt_thread_mutex_t c_count_mutex;
139         ldap_pvt_thread_mutex_t c_eifree_mutex;
140 #ifdef SLAP_ZONE_ALLOC
141         void *c_zctx;
142 #endif
143 } Cache;
144  
145 #define CACHE_READ_LOCK                0
146 #define CACHE_WRITE_LOCK       1
147  
148 #define BDB_INDICES             128
149
150 struct bdb_db_info {
151         struct berval   bdi_name;
152         DB                      *bdi_db;
153 };
154
155 struct bdb_db_pgsize {
156         struct bdb_db_pgsize *bdp_next;
157         struct berval   bdp_name;
158         int     bdp_size;
159 };
160
161 #define BDB_MONITOR_IDX
162
163 typedef struct bdb_monitor_t {
164         void            *bdm_cb;
165         struct berval   bdm_ndn;
166 } bdb_monitor_t;
167
168 /* From ldap_rq.h */
169 struct re_s;
170
171 struct bdb_info {
172         DB_ENV          *bi_dbenv;
173
174         /* DB_ENV parameters */
175         /* The DB_ENV can be tuned via DB_CONFIG */
176         char            *bi_dbenv_home;
177         u_int32_t       bi_dbenv_xflags; /* extra flags */
178         int                     bi_dbenv_mode;
179
180         int                     bi_ndatabases;
181         int             bi_db_opflags;  /* db-specific flags */
182         struct bdb_db_info **bi_databases;
183         ldap_pvt_thread_mutex_t bi_database_mutex;
184         struct bdb_db_pgsize *bi_pagesizes;
185
186         slap_mask_t     bi_defaultmask;
187         Cache           bi_cache;
188         struct bdb_attrinfo             **bi_attrs;
189         int                     bi_nattrs;
190         void            *bi_search_stack;
191         int             bi_search_stack_depth;
192         int             bi_linear_index;
193
194         int                     bi_txn_cp;
195         u_int32_t       bi_txn_cp_min;
196         u_int32_t       bi_txn_cp_kbyte;
197         struct re_s             *bi_txn_cp_task;
198         struct re_s             *bi_index_task;
199
200         u_int32_t               bi_lock_detect;
201         long            bi_shm_key;
202
203         ID                      bi_lastid;
204         ldap_pvt_thread_mutex_t bi_lastid_mutex;
205         ID      bi_idl_cache_max_size;
206         ID              bi_idl_cache_size;
207         Avlnode         *bi_idl_tree;
208         bdb_idl_cache_entry_t   *bi_idl_lru_head;
209         bdb_idl_cache_entry_t   *bi_idl_lru_tail;
210         ldap_pvt_thread_rdwr_t bi_idl_tree_rwlock;
211         ldap_pvt_thread_mutex_t bi_idl_tree_lrulock;
212         alock_info_t    bi_alock_info;
213         char            *bi_db_config_path;
214         BerVarray       bi_db_config;
215         char            *bi_db_crypt_file;
216         struct berval   bi_db_crypt_key;
217         bdb_monitor_t   bi_monitor;
218
219 #ifdef BDB_MONITOR_IDX
220         ldap_pvt_thread_mutex_t bi_idx_mutex;
221         Avlnode         *bi_idx;
222 #endif /* BDB_MONITOR_IDX */
223
224         int             bi_flags;
225 #define BDB_IS_OPEN             0x01
226 #define BDB_HAS_CONFIG  0x02
227 #define BDB_UPD_CONFIG  0x04
228 #define BDB_DEL_INDEX   0x08
229 #define BDB_RE_OPEN             0x10
230 #define BDB_CHKSUM              0x20
231 #ifdef BDB_HIER
232         int             bi_modrdns;             /* number of modrdns completed */
233         ldap_pvt_thread_mutex_t bi_modrdns_mutex;
234 #endif
235 };
236
237 #define bi_id2entry     bi_databases[BDB_ID2ENTRY]
238 #define bi_dn2id        bi_databases[BDB_DN2ID]
239
240
241 struct bdb_lock_info {
242         struct bdb_lock_info *bli_next;
243         DB_LOCK bli_lock;
244         ID              bli_id;
245         int             bli_flag;
246 };
247 #define BLI_DONTFREE    1
248
249 struct bdb_op_info {
250         OpExtra boi_oe;
251         DB_TXN*         boi_txn;
252         struct bdb_lock_info *boi_locks;        /* used when no txn */
253         u_int32_t       boi_err;
254         char            boi_acl_cache;
255         char            boi_flag;
256 };
257 #define BOI_DONTFREE    1
258
259 #define DB_OPEN(db, file, name, type, flags, mode) \
260         ((db)->open)(db, file, name, type, flags, mode)
261
262 #if DB_VERSION_MAJOR < 4
263 #define LOCK_DETECT(env,f,t,a)          lock_detect(env, f, t, a)
264 #define LOCK_GET(env,i,f,o,m,l)         lock_get(env, i, f, o, m, l)
265 #define LOCK_PUT(env,l)                 lock_put(env, l)
266 #define TXN_CHECKPOINT(env,k,m,f)       txn_checkpoint(env, k, m, f)
267 #define TXN_BEGIN(env,p,t,f)            txn_begin((env), p, t, f)
268 #define TXN_PREPARE(txn,gid)            txn_prepare((txn), (gid))
269 #define TXN_COMMIT(txn,f)                       txn_commit((txn), (f))
270 #define TXN_ABORT(txn)                          txn_abort((txn))
271 #define TXN_ID(txn)                                     txn_id(txn)
272 #define XLOCK_ID(env, locker)           lock_id(env, locker)
273 #define XLOCK_ID_FREE(env, locker)      lock_id_free(env, locker)
274 #else
275 #define LOCK_DETECT(env,f,t,a)          (env)->lock_detect(env, f, t, a)
276 #define LOCK_GET(env,i,f,o,m,l)         (env)->lock_get(env, i, f, o, m, l)
277 #define LOCK_PUT(env,l)                 (env)->lock_put(env, l)
278 #define TXN_CHECKPOINT(env,k,m,f)       (env)->txn_checkpoint(env, k, m, f)
279 #define TXN_BEGIN(env,p,t,f)            (env)->txn_begin((env), p, t, f)
280 #define TXN_PREPARE(txn,g)                      (txn)->prepare((txn), (g))
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)           (env)->lock_id(env, locker)
285 #define XLOCK_ID_FREE(env, locker)      (env)->lock_id_free(env, locker)
286
287 /* BDB 4.1.17 adds txn arg to db->open */
288 #if DB_VERSION_FULL >= 0x04010011
289 #undef DB_OPEN
290 #define DB_OPEN(db, file, name, type, flags, mode) \
291         ((db)->open)(db, NULL, file, name, type, flags, mode)
292 #endif
293
294 /* #undef BDB_LOG_DEBUG */
295
296 #ifdef BDB_LOG_DEBUG
297
298 /* env->log_printf appeared in 4.4 */
299 #if DB_VERSION_FULL >= 0x04040000
300 #define BDB_LOG_PRINTF(env,txn,fmt,...) (env)->log_printf((env),(txn),(fmt),__VA_ARGS__)
301 #else
302 extern int __db_logmsg(const DB_ENV *env, DB_TXN *txn, const char *op, u_int32_t flags,
303         const char *fmt,...);
304 #define BDB_LOG_PRINTF(env,txn,fmt,...) __db_logmsg((env),(txn),"DIAGNOSTIC",0,(fmt),__VA_ARGS__)
305 #endif
306
307 /* !BDB_LOG_DEBUG */
308 #elif (defined(__STDC_VERSION__) && __STDC_VERSION__ >= 199901L) || \
309         (defined(__GNUC__) && __GNUC__ >= 3 && !defined(__STRICT_ANSI__))
310 #define BDB_LOG_PRINTF(a,b,c,...)
311 #else
312 #define BDB_LOG_PRINTF (void)   /* will evaluate and discard the arguments */
313
314 #endif /* BDB_LOG_DEBUG */
315
316 #endif
317
318 #ifndef DB_BUFFER_SMALL
319 #define DB_BUFFER_SMALL                 ENOMEM
320 #endif
321
322 #define BDB_CSN_COMMIT  0
323 #define BDB_CSN_ABORT   1
324 #define BDB_CSN_RETRY   2
325
326 /* Copy an ID "src" to pointer "dst" in big-endian byte order */
327 #define BDB_ID2DISK( src, dst ) \
328         do { int i0; ID tmp; unsigned char *_p; \
329                 tmp = (src); _p = (unsigned char *)(dst);       \
330                 for ( i0=sizeof(ID)-1; i0>=0; i0-- ) {  \
331                         _p[i0] = tmp & 0xff; tmp >>= 8; \
332                 } \
333         } while(0)
334
335 /* Copy a pointer "src" to a pointer "dst" from big-endian to native order */
336 #define BDB_DISK2ID( src, dst ) \
337         do { unsigned i0; ID tmp = 0; unsigned char *_p;        \
338                 _p = (unsigned char *)(src);    \
339                 for ( i0=0; i0<sizeof(ID); i0++ ) {     \
340                         tmp <<= 8; tmp |= *_p++;        \
341                 } *(dst) = tmp; \
342         } while (0)
343
344 LDAP_END_DECL
345
346 /* for the cache of attribute information (which are indexed, etc.) */
347 typedef struct bdb_attrinfo {
348         AttributeDescription *ai_desc; /* attribute description cn;lang-en */
349         slap_mask_t ai_indexmask;       /* how the attr is indexed      */
350         slap_mask_t ai_newmask; /* new settings to replace old mask */
351 #ifdef LDAP_COMP_MATCH
352         ComponentReference* ai_cr; /*component indexing*/
353 #endif
354 } AttrInfo;
355
356 /* These flags must not clash with SLAP_INDEX flags or ops in slap.h! */
357 #define BDB_INDEX_DELETING      0x8000U /* index is being modified */
358 #define BDB_INDEX_UPDATE_OP     0x03    /* performing an index update */
359
360 /* For slapindex to record which attrs in an entry belong to which
361  * index database 
362  */
363 typedef struct AttrList {
364         struct AttrList *next;
365         Attribute *attr;
366 } AttrList;
367
368 typedef struct IndexRec {
369         AttrInfo *ai;
370         AttrList *attrs;
371 } IndexRec;
372
373 #include "proto-bdb.h"
374
375 #endif /* _BACK_BDB_H_ */