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