]> git.sur5r.net Git - openldap/blob - servers/slapd/back-bdb/back-bdb.h
ITS#3470 don't propagate error if hasSubordinates fails, it's not that
[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
24 LDAP_BEGIN_DECL
25
26 #undef BDB_PSEARCH
27
28 #define DB_VERSION_FULL ((DB_VERSION_MAJOR << 24) | (DB_VERSION_MINOR << 16) | DB_VERSION_PATCH)
29
30 #define BDB_SUBENTRIES 1
31
32 #define DN_BASE_PREFIX          SLAP_INDEX_EQUALITY_PREFIX
33 #define DN_ONE_PREFIX           '%'
34 #define DN_SUBTREE_PREFIX       '@'
35
36 #define DBTzero(t)                      (memset((t), 0, sizeof(DBT)))
37 #define DBT2bv(t,bv)            ((bv)->bv_val = (t)->data, \
38                                                                 (bv)->bv_len = (t)->size)
39 #define bv2DBT(bv,t)            ((t)->data = (bv)->bv_val, \
40                                                                 (t)->size = (bv)->bv_len )
41
42 #define BDB_TXN_RETRIES         16
43
44 #define BDB_MAX_ADD_LOOP        30
45
46 #ifdef BDB_SUBDIRS
47 #define BDB_TMP_SUBDIR  "tmp"
48 #define BDB_LG_SUBDIR   "log"
49 #define BDB_DATA_SUBDIR "data"
50 #endif
51
52 #define BDB_SUFFIX              ".bdb"
53 #define BDB_ID2ENTRY    0
54 #define BDB_DN2ID               1
55 #define BDB_NDB                 2
56
57 /* The bdb on-disk entry format is pretty space-inefficient. Average
58  * sized user entries are 3-4K each. You need at least two entries to
59  * fit into a single database page, more is better. 64K is BDB's
60  * upper bound. Smaller pages are better for concurrency.
61  */
62 #ifndef BDB_ID2ENTRY_PAGESIZE
63 #define BDB_ID2ENTRY_PAGESIZE   16384
64 #endif
65
66 #ifndef BDB_PAGESIZE
67 #define BDB_PAGESIZE    4096    /* BDB's original default */
68 #endif
69
70 #define DEFAULT_CACHE_SIZE     1000
71
72 /* The default search IDL stack cache depth */
73 #define DEFAULT_SEARCH_STACK_DEPTH      16
74
75 /* The minimum we can function with */
76 #define MINIMUM_SEARCH_STACK_DEPTH      8
77
78 typedef struct bdb_idl_cache_entry_s {
79         struct berval kstr;
80         ldap_pvt_thread_rdwr_t idl_entry_rwlock;
81         ID      *idl;
82         DB      *db;
83         struct bdb_idl_cache_entry_s* idl_lru_prev;
84         struct bdb_idl_cache_entry_s* idl_lru_next;
85 } bdb_idl_cache_entry_t;
86
87 /* BDB backend specific entry info */
88 typedef struct bdb_entry_info {
89         struct bdb_entry_info *bei_parent;
90         ID bei_id;
91
92         /* we use the bei_id as a lockobj, but we need to make the size != 4
93          * to avoid conflicting with BDB's internal locks. So add a byte here
94          * that is always zero.
95          */
96         char bei_lockpad;
97                                                 
98         short bei_state;
99 #define CACHE_ENTRY_DELETED     1
100 #define CACHE_ENTRY_NO_KIDS     2
101 #define CACHE_ENTRY_NOT_LINKED  4
102 #define CACHE_ENTRY_NO_GRANDKIDS        8
103 #define CACHE_ENTRY_LOADING     0x10
104
105         /*
106          * remaining fields require backend cache lock to access
107          */
108         struct berval bei_nrdn;
109 #ifdef BDB_HIER
110         struct berval bei_rdn;
111         int     bei_modrdns;    /* track renames */
112         int     bei_ckids;      /* number of kids cached */
113         int     bei_dkids;      /* number of kids on-disk, plus 1 */
114 #endif
115         Entry   *bei_e;
116         Avlnode *bei_kids;
117 #ifdef SLAP_ZONE_ALLOC
118         struct bdb_info *bei_bdb;
119         int bei_zseq;   
120 #endif
121         ldap_pvt_thread_mutex_t bei_kids_mutex;
122         
123         struct bdb_entry_info   *bei_lrunext;   /* for cache lru list */
124         struct bdb_entry_info   *bei_lruprev;
125 } EntryInfo;
126 #undef BEI
127 #define BEI(e)  ((EntryInfo *) ((e)->e_private))
128
129 /* for the in-core cache of entries */
130 typedef struct bdb_cache {
131         int             c_maxsize;
132         int             c_cursize;
133         int             c_eiused;       /* EntryInfo's in use */
134         EntryInfo       c_dntree;
135         EntryInfo       *c_eifree;      /* free list */
136         Avlnode         *c_idtree;
137         EntryInfo       *c_lruhead;     /* lru - add accessed entries here */
138         EntryInfo       *c_lrutail;     /* lru - rem lru entries from here */
139         ldap_pvt_thread_rdwr_t c_rwlock;
140         ldap_pvt_thread_mutex_t lru_mutex;
141         u_int32_t       c_locker;       /* used by lru cleaner */
142 #ifdef SLAP_ZONE_ALLOC
143         void *c_zctx;
144 #endif
145 } Cache;
146  
147 #define CACHE_READ_LOCK                0
148 #define CACHE_WRITE_LOCK       1
149  
150 #define BDB_INDICES             128
151
152 struct bdb_db_info {
153         char            *bdi_name;
154         DB                      *bdi_db;
155 };
156
157 struct bdb_info {
158         DB_ENV          *bi_dbenv;
159
160         /* DB_ENV parameters */
161         /* The DB_ENV can be tuned via DB_CONFIG */
162         char            *bi_dbenv_home;
163         u_int32_t       bi_dbenv_xflags; /* extra flags */
164         int                     bi_dbenv_mode;
165
166         int                     bi_ndatabases;
167         struct bdb_db_info **bi_databases;
168         ldap_pvt_thread_mutex_t bi_database_mutex;
169         int             bi_db_opflags;  /* db-specific flags */
170
171         slap_mask_t     bi_defaultmask;
172         Cache           bi_cache;
173         Avlnode         *bi_attrs;
174         void            *bi_search_stack;
175         int             bi_search_stack_depth;
176         int             bi_linear_index;
177
178         int                     bi_txn_cp;
179         u_int32_t       bi_txn_cp_min;
180         u_int32_t       bi_txn_cp_kbyte;
181
182         int                     bi_lock_detect;
183         long            bi_shm_key;
184
185         ID                      bi_lastid;
186         ldap_pvt_thread_mutex_t bi_lastid_mutex;
187 #ifdef BDB_PSEARCH
188         LDAP_LIST_HEAD(pl, slap_op) bi_psearch_list;
189         ldap_pvt_thread_rdwr_t bi_pslist_rwlock;
190         LDAP_LIST_HEAD(se, slap_session_entry) bi_session_list;
191 #endif
192         int             bi_idl_cache_max_size;
193         int             bi_idl_cache_size;
194         Avlnode         *bi_idl_tree;
195         bdb_idl_cache_entry_t   *bi_idl_lru_head;
196         bdb_idl_cache_entry_t   *bi_idl_lru_tail;
197         ldap_pvt_thread_rdwr_t bi_idl_tree_rwlock;
198         ldap_pvt_thread_mutex_t bi_idl_tree_lrulock;
199 };
200
201 #define bi_id2entry     bi_databases[BDB_ID2ENTRY]
202 #define bi_dn2id        bi_databases[BDB_DN2ID]
203
204 struct bdb_lock_info {
205         struct bdb_lock_info *bli_next;
206         ID              bli_id;
207         DB_LOCK bli_lock;
208 };
209
210 struct bdb_op_info {
211         BackendDB*      boi_bdb;
212         DB_TXN*         boi_txn;
213         u_int32_t       boi_err;
214         u_int32_t       boi_locker;
215         int             boi_acl_cache;
216         struct bdb_lock_info *boi_locks;        /* used when no txn */
217 };
218
219 #define DB_OPEN(db, file, name, type, flags, mode) \
220         (db)->open(db, file, name, type, flags, mode)
221
222 #if DB_VERSION_MAJOR < 4
223 #define LOCK_DETECT(env,f,t,a)          lock_detect(env, f, t, a)
224 #define LOCK_GET(env,i,f,o,m,l)         lock_get(env, i, f, o, m, l)
225 #define LOCK_PUT(env,l)                 lock_put(env, l)
226 #define TXN_CHECKPOINT(env,k,m,f)       txn_checkpoint(env, k, m, f)
227 #define TXN_BEGIN(env,p,t,f)            txn_begin((env), p, t, f)
228 #define TXN_PREPARE(txn,gid)            txn_prepare((txn), (gid))
229 #define TXN_COMMIT(txn,f)                       txn_commit((txn), (f))
230 #define TXN_ABORT(txn)                          txn_abort((txn))
231 #define TXN_ID(txn)                                     txn_id(txn)
232 #define XLOCK_ID(env, locker)           lock_id(env, locker)
233 #define XLOCK_ID_FREE(env, locker)      lock_id_free(env, locker)
234 #else
235 #define LOCK_DETECT(env,f,t,a)          (env)->lock_detect(env, f, t, a)
236 #define LOCK_GET(env,i,f,o,m,l)         (env)->lock_get(env, i, f, o, m, l)
237 #define LOCK_PUT(env,l)                 (env)->lock_put(env, l)
238 #define TXN_CHECKPOINT(env,k,m,f)       (env)->txn_checkpoint(env, k, m, f)
239 #define TXN_BEGIN(env,p,t,f)            (env)->txn_begin((env), p, t, f)
240 #define TXN_PREPARE(txn,g)                      (txn)->prepare((txn), (g))
241 #define TXN_COMMIT(txn,f)                       (txn)->commit((txn), (f))
242 #define TXN_ABORT(txn)                          (txn)->abort((txn))
243 #define TXN_ID(txn)                                     (txn)->id(txn)
244 #define XLOCK_ID(env, locker)           (env)->lock_id(env, locker)
245 #define XLOCK_ID_FREE(env, locker)      (env)->lock_id_free(env, locker)
246
247 /* BDB 4.1.17 adds txn arg to db->open */
248 #if DB_VERSION_FULL >= 0x04010011
249 #undef DB_OPEN
250 #define DB_OPEN(db, file, name, type, flags, mode) \
251         (db)->open(db, NULL, file, name, type, (flags)|DB_AUTO_COMMIT, mode)
252 #endif
253
254 #endif
255
256 #define BDB_REUSE_LOCKERS
257
258 #define BDB_CSN_COMMIT  0
259 #define BDB_CSN_ABORT   1
260 #define BDB_CSN_RETRY   2
261
262 /* Copy an ID "src" to pointer "dst" in big-endian byte order */
263 #define BDB_ID2DISK( src, dst ) \
264         do { int i0; ID tmp; char *ptr; \
265                 tmp = (src); ptr = (char *)(dst);       \
266                 for ( i0=sizeof(ID)-1; i0>=0; i0-- ) {  \
267                         ptr[i0] = tmp & 0xff; tmp >>= 8;        \
268                 } \
269         } while(0);
270
271 /* Copy a pointer "src" to a pointer "dst" from big-endian to native order */
272 #define BDB_DISK2ID( src, dst ) \
273         do { int i0; ID tmp = 0; unsigned char *ptr;    \
274                 ptr = (unsigned char *)(src);   \
275                 for ( i0=0; i0<sizeof(ID); i0++ ) {     \
276                         tmp <<= 8; tmp |= *ptr++;       \
277                 } *(dst) = tmp; \
278         } while (0);
279
280 LDAP_END_DECL
281
282 #include "proto-bdb.h"
283
284 #endif /* _BACK_BDB_H_ */