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