]> git.sur5r.net Git - openldap/blob - servers/slapd/back-bdb/back-bdb.h
Sync with HEAD
[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-2005 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 BDB_SUBENTRIES 1
30
31 #define DN_BASE_PREFIX          SLAP_INDEX_EQUALITY_PREFIX
32 #define DN_ONE_PREFIX           '%'
33 #define DN_SUBTREE_PREFIX       '@'
34
35 #define DBTzero(t)                      (memset((t), 0, sizeof(DBT)))
36 #define DBT2bv(t,bv)            ((bv)->bv_val = (t)->data, \
37                                                                 (bv)->bv_len = (t)->size)
38 #define bv2DBT(bv,t)            ((t)->data = (bv)->bv_val, \
39                                                                 (t)->size = (bv)->bv_len )
40
41 #define BDB_TXN_RETRIES         16
42
43 #define BDB_MAX_ADD_LOOP        30
44
45 #define BDB_SUFFIX              ".bdb"
46 #define BDB_ID2ENTRY    0
47 #define BDB_DN2ID               1
48 #define BDB_NDB                 2
49
50 /* The bdb on-disk entry format is pretty space-inefficient. Average
51  * sized user entries are 3-4K each. You need at least two entries to
52  * fit into a single database page, more is better. 64K is BDB's
53  * upper bound. Smaller pages are better for concurrency.
54  */
55 #ifndef BDB_ID2ENTRY_PAGESIZE
56 #define BDB_ID2ENTRY_PAGESIZE   16384
57 #endif
58
59 #ifndef BDB_PAGESIZE
60 #define BDB_PAGESIZE    4096    /* BDB's original default */
61 #endif
62
63 #define DEFAULT_CACHE_SIZE     1000
64
65 /* The default search IDL stack cache depth */
66 #define DEFAULT_SEARCH_STACK_DEPTH      16
67
68 /* The minimum we can function with */
69 #define MINIMUM_SEARCH_STACK_DEPTH      8
70
71 typedef struct bdb_idl_cache_entry_s {
72         struct berval kstr;
73         ldap_pvt_thread_rdwr_t idl_entry_rwlock;
74         ID      *idl;
75         DB      *db;
76         struct bdb_idl_cache_entry_s* idl_lru_prev;
77         struct bdb_idl_cache_entry_s* idl_lru_next;
78 } bdb_idl_cache_entry_t;
79
80 /* BDB backend specific entry info */
81 typedef struct bdb_entry_info {
82         struct bdb_entry_info *bei_parent;
83         ID bei_id;
84
85         /* we use the bei_id as a lockobj, but we need to make the size != 4
86          * to avoid conflicting with BDB's internal locks. So add a byte here
87          * that is always zero.
88          */
89         char bei_lockpad;
90
91         short bei_state;
92 #define CACHE_ENTRY_DELETED     1
93 #define CACHE_ENTRY_NO_KIDS     2
94 #define CACHE_ENTRY_NOT_LINKED  4
95 #define CACHE_ENTRY_NO_GRANDKIDS        8
96 #define CACHE_ENTRY_LOADING     0x10
97 #define CACHE_ENTRY_WALKING     0x20
98 #define CACHE_ENTRY_ONELEVEL    0x40
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         int             c_maxsize;
127         int             c_cursize;
128         int             c_eiused;       /* EntryInfo's in use */
129         int             c_leaves;       /* EntryInfo leaf nodes */
130         EntryInfo       c_dntree;
131         EntryInfo       *c_eifree;      /* free list */
132         Avlnode         *c_idtree;
133         EntryInfo       *c_lruhead;     /* lru - add accessed entries here */
134         EntryInfo       *c_lrutail;     /* lru - rem lru entries from here */
135         ldap_pvt_thread_rdwr_t c_rwlock;
136         ldap_pvt_thread_mutex_t lru_mutex;
137         u_int32_t       c_locker;       /* used by lru cleaner */
138 #ifdef SLAP_ZONE_ALLOC
139         void *c_zctx;
140 #endif
141 } Cache;
142  
143 #define CACHE_READ_LOCK                0
144 #define CACHE_WRITE_LOCK       1
145  
146 #define BDB_INDICES             128
147
148 struct bdb_db_info {
149         char            *bdi_name;
150         DB                      *bdi_db;
151 };
152
153 struct bdb_info {
154         DB_ENV          *bi_dbenv;
155
156         /* DB_ENV parameters */
157         /* The DB_ENV can be tuned via DB_CONFIG */
158         char            *bi_dbenv_home;
159         u_int32_t       bi_dbenv_xflags; /* extra flags */
160         int                     bi_dbenv_mode;
161
162         int                     bi_ndatabases;
163         struct bdb_db_info **bi_databases;
164         ldap_pvt_thread_mutex_t bi_database_mutex;
165         int             bi_db_opflags;  /* db-specific flags */
166
167         slap_mask_t     bi_defaultmask;
168         Cache           bi_cache;
169         Avlnode         *bi_attrs;
170         void            *bi_search_stack;
171         int             bi_search_stack_depth;
172         int             bi_linear_index;
173
174         int                     bi_txn_cp;
175         u_int32_t       bi_txn_cp_min;
176         u_int32_t       bi_txn_cp_kbyte;
177         void            *bi_txn_cp_task;
178         void            *bi_index_task;
179
180         int                     bi_lock_detect;
181         long            bi_shm_key;
182
183         ID                      bi_lastid;
184         ldap_pvt_thread_mutex_t bi_lastid_mutex;
185         int             bi_idl_cache_max_size;
186         int             bi_idl_cache_size;
187         Avlnode         *bi_idl_tree;
188         bdb_idl_cache_entry_t   *bi_idl_lru_head;
189         bdb_idl_cache_entry_t   *bi_idl_lru_tail;
190         ldap_pvt_thread_rdwr_t bi_idl_tree_rwlock;
191         ldap_pvt_thread_mutex_t bi_idl_tree_lrulock;
192         alock_info_t    bi_alock_info;
193         char            *bi_db_config_path;
194         BerVarray       bi_db_config;
195         int             bi_flags;
196 #define BDB_IS_OPEN             0x01
197 #define BDB_HAS_CONFIG  0x02
198 #define BDB_UPD_CONFIG  0x04
199 #define BDB_DEL_INDEX   0x08
200 #define BDB_RE_OPEN             0x10
201 #ifdef BDB_HIER
202         int             bi_modrdns;             /* number of modrdns completed */
203         ldap_pvt_thread_mutex_t bi_modrdns_mutex;
204 #endif
205 };
206
207 #define bi_id2entry     bi_databases[BDB_ID2ENTRY]
208 #define bi_dn2id        bi_databases[BDB_DN2ID]
209
210 struct bdb_lock_info {
211         struct bdb_lock_info *bli_next;
212         ID              bli_id;
213         DB_LOCK bli_lock;
214 };
215
216 struct bdb_op_info {
217         BackendDB*      boi_bdb;
218         DB_TXN*         boi_txn;
219         u_int32_t       boi_err;
220         u_int32_t       boi_locker;
221         int             boi_acl_cache;
222         struct bdb_lock_info *boi_locks;        /* used when no txn */
223 };
224
225 #define DB_OPEN(db, file, name, type, flags, mode) \
226         (db)->open(db, file, name, type, flags, mode)
227
228 #if DB_VERSION_MAJOR < 4
229 #define LOCK_DETECT(env,f,t,a)          lock_detect(env, f, t, a)
230 #define LOCK_GET(env,i,f,o,m,l)         lock_get(env, i, f, o, m, l)
231 #define LOCK_PUT(env,l)                 lock_put(env, l)
232 #define TXN_CHECKPOINT(env,k,m,f)       txn_checkpoint(env, k, m, f)
233 #define TXN_BEGIN(env,p,t,f)            txn_begin((env), p, t, f)
234 #define TXN_PREPARE(txn,gid)            txn_prepare((txn), (gid))
235 #define TXN_COMMIT(txn,f)                       txn_commit((txn), (f))
236 #define TXN_ABORT(txn)                          txn_abort((txn))
237 #define TXN_ID(txn)                                     txn_id(txn)
238 #define XLOCK_ID(env, locker)           lock_id(env, locker)
239 #define XLOCK_ID_FREE(env, locker)      lock_id_free(env, locker)
240 #else
241 #define LOCK_DETECT(env,f,t,a)          (env)->lock_detect(env, f, t, a)
242 #define LOCK_GET(env,i,f,o,m,l)         (env)->lock_get(env, i, f, o, m, l)
243 #define LOCK_PUT(env,l)                 (env)->lock_put(env, l)
244 #define TXN_CHECKPOINT(env,k,m,f)       (env)->txn_checkpoint(env, k, m, f)
245 #define TXN_BEGIN(env,p,t,f)            (env)->txn_begin((env), p, t, f)
246 #define TXN_PREPARE(txn,g)                      (txn)->prepare((txn), (g))
247 #define TXN_COMMIT(txn,f)                       (txn)->commit((txn), (f))
248 #define TXN_ABORT(txn)                          (txn)->abort((txn))
249 #define TXN_ID(txn)                                     (txn)->id(txn)
250 #define XLOCK_ID(env, locker)           (env)->lock_id(env, locker)
251 #define XLOCK_ID_FREE(env, locker)      (env)->lock_id_free(env, locker)
252
253 /* BDB 4.1.17 adds txn arg to db->open */
254 #if DB_VERSION_FULL >= 0x04010011
255 #undef DB_OPEN
256 #define DB_OPEN(db, file, name, type, flags, mode) \
257         (db)->open(db, NULL, file, name, type, flags, mode)
258 #endif
259
260 #endif
261
262 #define BDB_REUSE_LOCKERS
263
264 #define BDB_CSN_COMMIT  0
265 #define BDB_CSN_ABORT   1
266 #define BDB_CSN_RETRY   2
267
268 /* Copy an ID "src" to pointer "dst" in big-endian byte order */
269 #define BDB_ID2DISK( src, dst ) \
270         do { int i0; ID tmp; unsigned char *_p; \
271                 tmp = (src); _p = (unsigned char *)(dst);       \
272                 for ( i0=sizeof(ID)-1; i0>=0; i0-- ) {  \
273                         _p[i0] = tmp & 0xff; tmp >>= 8; \
274                 } \
275         } while(0)
276
277 /* Copy a pointer "src" to a pointer "dst" from big-endian to native order */
278 #define BDB_DISK2ID( src, dst ) \
279         do { int i0; ID tmp = 0; unsigned char *_p;     \
280                 _p = (unsigned char *)(src);    \
281                 for ( i0=0; i0<sizeof(ID); i0++ ) {     \
282                         tmp <<= 8; tmp |= *_p++;        \
283                 } *(dst) = tmp; \
284         } while (0)
285
286 LDAP_END_DECL
287
288 /* for the cache of attribute information (which are indexed, etc.) */
289 typedef struct bdb_attrinfo {
290         AttributeDescription *ai_desc; /* attribute description cn;lang-en */
291         slap_mask_t ai_indexmask;       /* how the attr is indexed      */
292         slap_mask_t ai_newmask; /* new settings to replace old mask */
293 #ifdef LDAP_COMP_MATCH
294         ComponentReference* ai_cr; /*component indexing*/
295 #endif
296 } AttrInfo;
297
298 /* These flags must not clash with SLAP_INDEX flags or ops in slap.h! */
299 #define BDB_INDEX_DELETING      0x8000U /* index is being modified */
300 #define BDB_INDEX_UPDATE_OP     0x03    /* performing an index update */
301
302 #include "proto-bdb.h"
303
304 #endif /* _BACK_BDB_H_ */