1*549b59edSchristos /* $NetBSD: init.c,v 1.2 2021/08/14 16:15:02 christos Exp $ */
2e670fd5cSchristos
3e670fd5cSchristos /* OpenLDAP WiredTiger backend */
4e670fd5cSchristos /* $OpenLDAP$ */
5e670fd5cSchristos /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
6e670fd5cSchristos *
7e670fd5cSchristos * Copyright 2002-2021 The OpenLDAP Foundation.
8e670fd5cSchristos * All rights reserved.
9e670fd5cSchristos *
10e670fd5cSchristos * Redistribution and use in source and binary forms, with or without
11e670fd5cSchristos * modification, are permitted only as authorized by the OpenLDAP
12e670fd5cSchristos * Public License.
13e670fd5cSchristos *
14e670fd5cSchristos * A copy of this license is available in the file LICENSE in the
15e670fd5cSchristos * top-level directory of the distribution or, alternatively, at
16e670fd5cSchristos * <http://www.OpenLDAP.org/license.html>.
17e670fd5cSchristos */
18e670fd5cSchristos /* ACKNOWLEDGEMENTS:
19e670fd5cSchristos * This work was developed by HAMANO Tsukasa <hamano@osstech.co.jp>
20e670fd5cSchristos * based on back-bdb for inclusion in OpenLDAP Software.
21e670fd5cSchristos * WiredTiger is a product of MongoDB Inc.
22e670fd5cSchristos */
23e670fd5cSchristos
24e670fd5cSchristos #include <sys/cdefs.h>
25*549b59edSchristos __RCSID("$NetBSD: init.c,v 1.2 2021/08/14 16:15:02 christos Exp $");
26e670fd5cSchristos
27e670fd5cSchristos #include "portable.h"
28e670fd5cSchristos
29e670fd5cSchristos #include <stdio.h>
30e670fd5cSchristos #include <ac/string.h>
31e670fd5cSchristos #include "back-wt.h"
32e670fd5cSchristos #include "slap-config.h"
33e670fd5cSchristos
34e670fd5cSchristos static int
wt_db_init(BackendDB * be,ConfigReply * cr)35e670fd5cSchristos wt_db_init( BackendDB *be, ConfigReply *cr )
36e670fd5cSchristos {
37e670fd5cSchristos struct wt_info *wi;
38e670fd5cSchristos
39e670fd5cSchristos Debug( LDAP_DEBUG_TRACE,
40e670fd5cSchristos LDAP_XSTRING(wt_db_init) ": Initializing wt backend\n" );
41e670fd5cSchristos
42e670fd5cSchristos /* allocate backend-database-specific stuff */
43e670fd5cSchristos wi = ch_calloc( 1, sizeof(struct wt_info) );
44e670fd5cSchristos
45e670fd5cSchristos wi->wi_dbenv_home = ch_strdup( SLAPD_DEFAULT_DB_DIR );
46e670fd5cSchristos wi->wi_dbenv_config = ch_strdup("create");
47e670fd5cSchristos wi->wi_lastid = 0;
48e670fd5cSchristos wi->wi_search_stack_depth = DEFAULT_SEARCH_STACK_DEPTH;
49e670fd5cSchristos wi->wi_search_stack = NULL;
50e670fd5cSchristos
51e670fd5cSchristos be->be_private = wi;
52e670fd5cSchristos be->be_cf_ocs = be->bd_info->bi_cf_ocs;
53e670fd5cSchristos
54e670fd5cSchristos return LDAP_SUCCESS;
55e670fd5cSchristos }
56e670fd5cSchristos
57e670fd5cSchristos static int
wt_db_open(BackendDB * be,ConfigReply * cr)58e670fd5cSchristos wt_db_open( BackendDB *be, ConfigReply *cr )
59e670fd5cSchristos {
60e670fd5cSchristos struct wt_info *wi = (struct wt_info *) be->be_private;
61e670fd5cSchristos int rc;
62e670fd5cSchristos struct stat st;
63e670fd5cSchristos WT_CONNECTION *conn;
64e670fd5cSchristos WT_SESSION *session;
65e670fd5cSchristos
66e670fd5cSchristos if ( be->be_suffix == NULL ) {
67e670fd5cSchristos Debug( LDAP_DEBUG_ANY,
68e670fd5cSchristos LDAP_XSTRING(wt_db_open) ": need suffix.\n" );
69e670fd5cSchristos return -1;
70e670fd5cSchristos }
71e670fd5cSchristos
72e670fd5cSchristos Debug( LDAP_DEBUG_ARGS,
73e670fd5cSchristos LDAP_XSTRING(wt_db_open) ": \"%s\"\n",
74e670fd5cSchristos be->be_suffix[0].bv_val );
75e670fd5cSchristos
76e670fd5cSchristos /* Check existence of home. Any error means trouble */
77e670fd5cSchristos rc = stat( wi->wi_dbenv_home, &st );
78e670fd5cSchristos if( rc ) {
79e670fd5cSchristos int saved_errno = errno;
80e670fd5cSchristos Debug( LDAP_DEBUG_ANY,
81e670fd5cSchristos LDAP_XSTRING(wt_db_open) ": database \"%s\": "
82e670fd5cSchristos "cannot access database directory \"%s\" (%d).\n",
83e670fd5cSchristos be->be_suffix[0].bv_val, wi->wi_dbenv_home, saved_errno );
84e670fd5cSchristos return -1;
85e670fd5cSchristos }
86e670fd5cSchristos
87e670fd5cSchristos /* Open and create database */
88e670fd5cSchristos rc = wiredtiger_open(wi->wi_dbenv_home, NULL,
89e670fd5cSchristos wi->wi_dbenv_config, &conn);
90e670fd5cSchristos if( rc ) {
91e670fd5cSchristos int saved_errno = errno;
92e670fd5cSchristos Debug( LDAP_DEBUG_ANY,
93e670fd5cSchristos LDAP_XSTRING(wt_db_open) ": database \"%s\": "
94e670fd5cSchristos "cannot open database \"%s\" (%d).\n",
95e670fd5cSchristos be->be_suffix[0].bv_val, wi->wi_dbenv_home, saved_errno );
96e670fd5cSchristos return -1;
97e670fd5cSchristos }
98e670fd5cSchristos
99e670fd5cSchristos rc = conn->open_session(conn, NULL, NULL, &session);
100e670fd5cSchristos if( rc ) {
101e670fd5cSchristos Debug( LDAP_DEBUG_ANY,
102e670fd5cSchristos LDAP_XSTRING(wt_db_open) ": database \"%s\": "
103e670fd5cSchristos "cannot open session: \"%s\"\n",
104e670fd5cSchristos be->be_suffix[0].bv_val, wiredtiger_strerror(rc) );
105e670fd5cSchristos return -1;
106e670fd5cSchristos }
107e670fd5cSchristos
108e670fd5cSchristos rc = session->create(session,
109e670fd5cSchristos WT_TABLE_ID2ENTRY,
110e670fd5cSchristos "key_format=Q,"
111e670fd5cSchristos "value_format=Su,"
112e670fd5cSchristos "columns=(id,dn,entry)");
113e670fd5cSchristos if( rc ) {
114e670fd5cSchristos Debug( LDAP_DEBUG_ANY,
115e670fd5cSchristos LDAP_XSTRING(wt_db_open) ": database \"%s\": "
116e670fd5cSchristos "cannot create entry table: \"%s\"\n",
117e670fd5cSchristos be->be_suffix[0].bv_val, wiredtiger_strerror(rc) );
118e670fd5cSchristos return -1;
119e670fd5cSchristos }
120e670fd5cSchristos
121e670fd5cSchristos rc = session->create(session,
122e670fd5cSchristos WT_TABLE_DN2ID,
123e670fd5cSchristos "key_format=S,"
124e670fd5cSchristos "value_format=QQS,"
125e670fd5cSchristos "columns=(ndn,id,pid,revdn)");
126e670fd5cSchristos if( rc ) {
127e670fd5cSchristos Debug( LDAP_DEBUG_ANY,
128e670fd5cSchristos LDAP_XSTRING(wt_db_open) ": database \"%s\": "
129e670fd5cSchristos "cannot create entry table: \"%s\"\n",
130e670fd5cSchristos be->be_suffix[0].bv_val, wiredtiger_strerror(rc) );
131e670fd5cSchristos return -1;
132e670fd5cSchristos }
133e670fd5cSchristos
134e670fd5cSchristos /* not using dn2id index for id2entry table */
135e670fd5cSchristos rc = session->create(session, WT_INDEX_DN, "columns=(dn)");
136e670fd5cSchristos if( rc ) {
137e670fd5cSchristos Debug( LDAP_DEBUG_ANY,
138e670fd5cSchristos LDAP_XSTRING(wt_db_open) ": database \"%s\": "
139e670fd5cSchristos "cannot create dn index: \"%s\"\n",
140e670fd5cSchristos be->be_suffix[0].bv_val, wiredtiger_strerror(rc) );
141e670fd5cSchristos return -1;
142e670fd5cSchristos }
143e670fd5cSchristos
144e670fd5cSchristos rc = session->create(session, WT_INDEX_PID, "columns=(pid)");
145e670fd5cSchristos if( rc ) {
146e670fd5cSchristos Debug( LDAP_DEBUG_ANY,
147e670fd5cSchristos LDAP_XSTRING(wt_db_open) ": database \"%s\": "
148e670fd5cSchristos "cannot create pid index: \"%s\"\n",
149e670fd5cSchristos be->be_suffix[0].bv_val, wiredtiger_strerror(rc) );
150e670fd5cSchristos return -1;
151e670fd5cSchristos }
152e670fd5cSchristos
153e670fd5cSchristos rc = session->create(session, WT_INDEX_REVDN, "columns=(revdn)");
154e670fd5cSchristos if( rc ) {
155e670fd5cSchristos Debug( LDAP_DEBUG_ANY,
156e670fd5cSchristos LDAP_XSTRING(wt_db_open) ": database \"%s\": "
157e670fd5cSchristos "cannot create revdn index: \"%s\"\n",
158e670fd5cSchristos be->be_suffix[0].bv_val, wiredtiger_strerror(rc) );
159e670fd5cSchristos return -1;
160e670fd5cSchristos }
161e670fd5cSchristos
162e670fd5cSchristos rc = wt_last_id( be, session, &wi->wi_lastid);
163e670fd5cSchristos if (rc) {
164e670fd5cSchristos snprintf( cr->msg, sizeof(cr->msg), "database \"%s\": "
165e670fd5cSchristos "last_id() failed: %s(%d).",
166e670fd5cSchristos be->be_suffix[0].bv_val, wiredtiger_strerror(rc), rc );
167e670fd5cSchristos Debug( LDAP_DEBUG_ANY,
168e670fd5cSchristos LDAP_XSTRING(wt_db_open) ": %s\n",
169e670fd5cSchristos cr->msg );
170e670fd5cSchristos return rc;
171e670fd5cSchristos }
172e670fd5cSchristos
173e670fd5cSchristos session->close(session, NULL);
174e670fd5cSchristos wi->wi_conn = conn;
175e670fd5cSchristos wi->wi_flags |= WT_IS_OPEN;
176e670fd5cSchristos
177e670fd5cSchristos return LDAP_SUCCESS;
178e670fd5cSchristos }
179e670fd5cSchristos
180e670fd5cSchristos static int
wt_db_close(BackendDB * be,ConfigReply * cr)181e670fd5cSchristos wt_db_close( BackendDB *be, ConfigReply *cr )
182e670fd5cSchristos {
183e670fd5cSchristos struct wt_info *wi = (struct wt_info *) be->be_private;
184e670fd5cSchristos int rc;
185e670fd5cSchristos
186e670fd5cSchristos rc = wi->wi_conn->close(wi->wi_conn, NULL);
187e670fd5cSchristos if( rc ) {
188e670fd5cSchristos int saved_errno = errno;
189e670fd5cSchristos Debug( LDAP_DEBUG_ANY,
190e670fd5cSchristos LDAP_XSTRING(wt_db_close)
191e670fd5cSchristos ": cannot close database (%d).\n",
192e670fd5cSchristos saved_errno );
193e670fd5cSchristos return -1;
194e670fd5cSchristos }
195e670fd5cSchristos
196e670fd5cSchristos wi->wi_flags &= ~WT_IS_OPEN;
197e670fd5cSchristos
198e670fd5cSchristos return LDAP_SUCCESS;
199e670fd5cSchristos }
200e670fd5cSchristos
201e670fd5cSchristos static int
wt_db_destroy(Backend * be,ConfigReply * cr)202e670fd5cSchristos wt_db_destroy( Backend *be, ConfigReply *cr )
203e670fd5cSchristos {
204e670fd5cSchristos struct wt_info *wi = (struct wt_info *) be->be_private;
205e670fd5cSchristos
206e670fd5cSchristos if( wi->wi_dbenv_home ) {
207e670fd5cSchristos ch_free( wi->wi_dbenv_home );
208e670fd5cSchristos wi->wi_dbenv_home = NULL;
209e670fd5cSchristos }
210e670fd5cSchristos if( wi->wi_dbenv_config ) {
211e670fd5cSchristos ch_free( wi->wi_dbenv_config );
212e670fd5cSchristos wi->wi_dbenv_config = NULL;
213e670fd5cSchristos }
214e670fd5cSchristos
215e670fd5cSchristos wt_attr_index_destroy( wi );
216e670fd5cSchristos ch_free( wi );
217e670fd5cSchristos be->be_private = NULL;
218e670fd5cSchristos
219e670fd5cSchristos return LDAP_SUCCESS;
220e670fd5cSchristos }
221e670fd5cSchristos
222e670fd5cSchristos int
wt_back_initialize(BackendInfo * bi)223e670fd5cSchristos wt_back_initialize( BackendInfo *bi )
224e670fd5cSchristos {
225e670fd5cSchristos static char *controls[] = {
226e670fd5cSchristos LDAP_CONTROL_ASSERT,
227e670fd5cSchristos LDAP_CONTROL_MANAGEDSAIT,
228e670fd5cSchristos LDAP_CONTROL_NOOP,
229e670fd5cSchristos LDAP_CONTROL_PAGEDRESULTS,
230e670fd5cSchristos LDAP_CONTROL_PRE_READ,
231e670fd5cSchristos LDAP_CONTROL_POST_READ,
232e670fd5cSchristos LDAP_CONTROL_SUBENTRIES,
233e670fd5cSchristos LDAP_CONTROL_X_PERMISSIVE_MODIFY,
234e670fd5cSchristos NULL
235e670fd5cSchristos };
236e670fd5cSchristos
237e670fd5cSchristos /* initialize the database system */
238e670fd5cSchristos Debug( LDAP_DEBUG_TRACE,
239e670fd5cSchristos LDAP_XSTRING(wt_back_initialize)
240e670fd5cSchristos ": initialize WiredTiger backend\n" );
241e670fd5cSchristos
242e670fd5cSchristos bi->bi_flags |=
243e670fd5cSchristos SLAP_BFLAG_INCREMENT |
244e670fd5cSchristos SLAP_BFLAG_SUBENTRIES |
245e670fd5cSchristos SLAP_BFLAG_ALIASES |
246e670fd5cSchristos SLAP_BFLAG_REFERRALS;
247e670fd5cSchristos
248e670fd5cSchristos bi->bi_controls = controls;
249e670fd5cSchristos /* version check */
250e670fd5cSchristos Debug( LDAP_DEBUG_TRACE,
251e670fd5cSchristos LDAP_XSTRING(wt_back_initialize) ": %s\n",
252e670fd5cSchristos wiredtiger_version(NULL, NULL, NULL) );
253e670fd5cSchristos
254e670fd5cSchristos bi->bi_open = 0;
255e670fd5cSchristos bi->bi_close = 0;
256e670fd5cSchristos bi->bi_config = 0;
257e670fd5cSchristos bi->bi_destroy = 0;
258e670fd5cSchristos
259e670fd5cSchristos bi->bi_db_init = wt_db_init;
260e670fd5cSchristos bi->bi_db_config = config_generic_wrapper;
261e670fd5cSchristos bi->bi_db_open = wt_db_open;
262e670fd5cSchristos bi->bi_db_close = wt_db_close;
263e670fd5cSchristos bi->bi_db_destroy = wt_db_destroy;
264e670fd5cSchristos
265e670fd5cSchristos bi->bi_op_add = wt_add;
266e670fd5cSchristos bi->bi_op_bind = wt_bind;
267e670fd5cSchristos bi->bi_op_unbind = 0;
268e670fd5cSchristos bi->bi_op_search = wt_search;
269e670fd5cSchristos bi->bi_op_compare = wt_compare;
270e670fd5cSchristos bi->bi_op_modify = wt_modify;
271e670fd5cSchristos bi->bi_op_modrdn = 0;
272e670fd5cSchristos bi->bi_op_delete = wt_delete;
273e670fd5cSchristos bi->bi_op_abandon = 0;
274e670fd5cSchristos
275e670fd5cSchristos bi->bi_extended = 0;
276e670fd5cSchristos
277e670fd5cSchristos bi->bi_chk_referrals = 0;
278e670fd5cSchristos bi->bi_operational = wt_operational;
279e670fd5cSchristos
280e670fd5cSchristos bi->bi_entry_release_rw = wt_entry_release;
281e670fd5cSchristos bi->bi_entry_get_rw = wt_entry_get;
282e670fd5cSchristos
283e670fd5cSchristos bi->bi_tool_entry_open = wt_tool_entry_open;
284e670fd5cSchristos bi->bi_tool_entry_close = wt_tool_entry_close;
285e670fd5cSchristos bi->bi_tool_entry_first = backend_tool_entry_first;
286e670fd5cSchristos bi->bi_tool_entry_first_x = wt_tool_entry_first_x;
287e670fd5cSchristos bi->bi_tool_entry_next = wt_tool_entry_next;
288e670fd5cSchristos bi->bi_tool_entry_get = wt_tool_entry_get;
289e670fd5cSchristos bi->bi_tool_entry_put = wt_tool_entry_put;
290e670fd5cSchristos bi->bi_tool_entry_reindex = wt_tool_entry_reindex;
291e670fd5cSchristos
292e670fd5cSchristos bi->bi_connection_init = 0;
293e670fd5cSchristos bi->bi_connection_destroy = 0;
294e670fd5cSchristos
295e670fd5cSchristos return wt_back_init_cf( bi );
296e670fd5cSchristos }
297e670fd5cSchristos
298e670fd5cSchristos #if SLAPD_WT == SLAPD_MOD_DYNAMIC
299e670fd5cSchristos
300e670fd5cSchristos /* conditionally define the init_module() function */
301e670fd5cSchristos SLAP_BACKEND_INIT_MODULE( wt )
302e670fd5cSchristos
303e670fd5cSchristos #endif /* SLAPD_WT == SLAPD_MOD_DYNAMIC */
304e670fd5cSchristos
305e670fd5cSchristos /*
306e670fd5cSchristos * Local variables:
307e670fd5cSchristos * indent-tabs-mode: t
308e670fd5cSchristos * tab-width: 4
309e670fd5cSchristos * c-basic-offset: 4
310e670fd5cSchristos * End:
311e670fd5cSchristos */
312