| 1 |
/* OpenLDAP WiredTiger backend */ |
| 2 |
/* $OpenLDAP$ */ |
| 3 |
/* This work is part of OpenLDAP Software <http://www.openldap.org/>. |
| 4 |
* |
| 5 |
* Copyright 2002-2024 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 |
/* ACKNOWLEDGEMENTS: |
| 17 |
* This work was developed by HAMANO Tsukasa <[email protected]> |
| 18 |
* based on back-bdb for inclusion in OpenLDAP Software. |
| 19 |
* WiredTiger is a product of MongoDB Inc. |
| 20 |
*/ |
| 21 |
|
| 22 |
#include "portable.h" |
| 23 |
|
| 24 |
#include <stdio.h> |
| 25 |
#include <ac/string.h> |
| 26 |
#include "back-wt.h" |
| 27 |
#include "slap-config.h" |
| 28 |
|
| 29 |
static int |
| 30 |
wt_db_init( BackendDB *be, ConfigReply *cr ) |
| 31 |
{ |
| 32 |
struct wt_info *wi; |
| 33 |
|
| 34 |
Debug( LDAP_DEBUG_TRACE, "wt_db_init: Initializing wt backend\n" ); |
| 35 |
|
| 36 |
/* allocate backend-database-specific stuff */ |
| 37 |
wi = ch_calloc( 1, sizeof(struct wt_info) ); |
| 38 |
wi->wi_home = ch_strdup( SLAPD_DEFAULT_DB_DIR ); |
| 39 |
wi->wi_config = ch_calloc( 1, WT_CONFIG_MAX + 1); |
| 40 |
if ( slapMode & SLAP_TOOL_READONLY ) { |
| 41 |
strcpy(wi->wi_config, "readonly"); |
| 42 |
} else { |
| 43 |
strcpy(wi->wi_config, "create"); |
| 44 |
} |
| 45 |
wi->wi_lastid = 0; |
| 46 |
wi->wi_search_stack_depth = DEFAULT_SEARCH_STACK_DEPTH; |
| 47 |
wi->wi_search_stack = NULL; |
| 48 |
wi->wi_flags = WT_USE_IDLCACHE; |
| 49 |
|
| 50 |
be->be_private = wi; |
| 51 |
be->be_cf_ocs = be->bd_info->bi_cf_ocs; |
| 52 |
|
| 53 |
return LDAP_SUCCESS; |
| 54 |
} |
| 55 |
|
| 56 |
static int |
| 57 |
wt_db_open( BackendDB *be, ConfigReply *cr ) |
| 58 |
{ |
| 59 |
struct wt_info *wi = (struct wt_info *) be->be_private; |
| 60 |
int rc; |
| 61 |
struct stat st; |
| 62 |
WT_SESSION *session = NULL; |
| 63 |
WT_SESSION *cache_session = NULL; |
| 64 |
|
| 65 |
if ( be->be_suffix == NULL ) { |
| 66 |
Debug( LDAP_DEBUG_ANY, "wt_db_open: need suffix.\n" ); |
| 67 |
return -1; |
| 68 |
} |
| 69 |
|
| 70 |
Debug( LDAP_DEBUG_ARGS, |
| 71 |
"wt_db_open: \"%s\", home=%s, config=%s\n", |
| 72 |
be->be_suffix[0].bv_val, wi->wi_home, wi->wi_config ); |
| 73 |
|
| 74 |
/* Check existence of home. Any error means trouble */ |
| 75 |
rc = stat( wi->wi_home, &st ); |
| 76 |
if( rc ) { |
| 77 |
int saved_errno = errno; |
| 78 |
Debug( LDAP_DEBUG_ANY, |
| 79 |
"wt_db_open: database \"%s\": " |
| 80 |
"cannot access database directory \"%s\" (%d).\n", |
| 81 |
be->be_suffix[0].bv_val, wi->wi_home, saved_errno ); |
| 82 |
return -1; |
| 83 |
} |
| 84 |
|
| 85 |
/* back-wt is always clean */ |
| 86 |
be->be_flags |= SLAP_DBFLAG_CLEAN; |
| 87 |
|
| 88 |
/* Open and create database */ |
| 89 |
rc = wiredtiger_open(wi->wi_home, NULL, |
| 90 |
wi->wi_config, &wi->wi_conn); |
| 91 |
if( rc ) { |
| 92 |
int saved_errno = errno; |
| 93 |
Debug( LDAP_DEBUG_ANY, |
| 94 |
"wt_db_open: database \"%s\": " |
| 95 |
"cannot open database \"%s\" (%d).\n", |
| 96 |
be->be_suffix[0].bv_val, wi->wi_home, saved_errno ); |
| 97 |
return -1; |
| 98 |
} |
| 99 |
|
| 100 |
rc = wi->wi_conn->open_session(wi->wi_conn, NULL, NULL, &session); |
| 101 |
if( rc ) { |
| 102 |
Debug( LDAP_DEBUG_ANY, |
| 103 |
"wt_db_open: database \"%s\": " |
| 104 |
"cannot open session: \"%s\"\n", |
| 105 |
be->be_suffix[0].bv_val, wiredtiger_strerror(rc) ); |
| 106 |
return -1; |
| 107 |
} |
| 108 |
|
| 109 |
if ( slapMode & SLAP_TOOL_READONLY ) { |
| 110 |
goto readonly; |
| 111 |
} |
| 112 |
|
| 113 |
/* checking for obsolete table */ |
| 114 |
rc = session->verify(session, WT_INDEX_REVDN, NULL); |
| 115 |
if ( !rc ) { |
| 116 |
Debug( LDAP_DEBUG_ANY, |
| 117 |
"wt_db_open: database \"%s\": " |
| 118 |
"incompatible wiredtiger table, please restore from LDIF.\n", |
| 119 |
be->be_suffix[0].bv_val ); |
| 120 |
return -1; |
| 121 |
} |
| 122 |
|
| 123 |
/* create tables and indexes */ |
| 124 |
rc = session->create(session, |
| 125 |
WT_TABLE_ID2ENTRY, |
| 126 |
"key_format=Q," |
| 127 |
"value_format=Su," |
| 128 |
"columns=(id,dn,entry)"); |
| 129 |
if( rc ) { |
| 130 |
Debug( LDAP_DEBUG_ANY, |
| 131 |
"wt_db_open: database \"%s\": " |
| 132 |
"cannot create entry table: \"%s\"\n", |
| 133 |
be->be_suffix[0].bv_val, wiredtiger_strerror(rc) ); |
| 134 |
return -1; |
| 135 |
} |
| 136 |
|
| 137 |
rc = session->create(session, |
| 138 |
WT_TABLE_DN2ID, |
| 139 |
"key_format=S," |
| 140 |
"value_format=SQQ," |
| 141 |
"columns=(revdn,ndn,id,pid)"); |
| 142 |
if( rc ) { |
| 143 |
Debug( LDAP_DEBUG_ANY, |
| 144 |
"wt_db_open: database \"%s\": " |
| 145 |
"cannot create entry table: \"%s\"\n", |
| 146 |
be->be_suffix[0].bv_val, wiredtiger_strerror(rc) ); |
| 147 |
return -1; |
| 148 |
} |
| 149 |
|
| 150 |
/* not using dn2id index for id2entry table */ |
| 151 |
rc = session->create(session, WT_INDEX_DN, "columns=(dn)"); |
| 152 |
if( rc ) { |
| 153 |
Debug( LDAP_DEBUG_ANY, |
| 154 |
"wt_db_open: database \"%s\": " |
| 155 |
"cannot create dn index: \"%s\"\n", |
| 156 |
be->be_suffix[0].bv_val, wiredtiger_strerror(rc) ); |
| 157 |
return -1; |
| 158 |
} |
| 159 |
|
| 160 |
rc = session->create(session, WT_INDEX_PID, "columns=(pid)"); |
| 161 |
if( rc ) { |
| 162 |
Debug( LDAP_DEBUG_ANY, |
| 163 |
"wt_db_open: database \"%s\": " |
| 164 |
"cannot create pid index: \"%s\"\n", |
| 165 |
be->be_suffix[0].bv_val, wiredtiger_strerror(rc) ); |
| 166 |
return -1; |
| 167 |
} |
| 168 |
|
| 169 |
rc = session->create(session, WT_INDEX_NDN, "columns=(ndn)"); |
| 170 |
if( rc ) { |
| 171 |
Debug( LDAP_DEBUG_ANY, |
| 172 |
"wt_db_open: database \"%s\": " |
| 173 |
"cannot create ndn index: \"%s\"\n", |
| 174 |
be->be_suffix[0].bv_val, wiredtiger_strerror(rc) ); |
| 175 |
return -1; |
| 176 |
} |
| 177 |
|
| 178 |
/* open in-memory database for idlcache */ |
| 179 |
rc = wiredtiger_open(be->be_suffix[0].bv_val, NULL, |
| 180 |
"in_memory=true", &wi->wi_cache); |
| 181 |
if( rc ) { |
| 182 |
Debug( LDAP_DEBUG_ANY, |
| 183 |
"wt_db_open: database \"%s\": " |
| 184 |
"cannot open database for cache (%s).\n", |
| 185 |
be->be_suffix[0].bv_val, wiredtiger_strerror(rc) ); |
| 186 |
return -1; |
| 187 |
} |
| 188 |
|
| 189 |
rc = wi->wi_cache->open_session(wi->wi_cache, NULL, NULL, &cache_session); |
| 190 |
if( rc ) { |
| 191 |
Debug( LDAP_DEBUG_ANY, |
| 192 |
"wt_db_open: database \"%s\": " |
| 193 |
"cannot open session for cache: \"%s\"\n", |
| 194 |
be->be_suffix[0].bv_val, wiredtiger_strerror(rc) ); |
| 195 |
return -1; |
| 196 |
} |
| 197 |
|
| 198 |
rc = cache_session->create(cache_session, |
| 199 |
WT_TABLE_IDLCACHE, |
| 200 |
"key_format=Sb," |
| 201 |
"value_format=u," |
| 202 |
"columns=(ndn,scope,idl)"); |
| 203 |
if( rc ) { |
| 204 |
Debug( LDAP_DEBUG_ANY, |
| 205 |
"wt_db_open: database \"%s\": " |
| 206 |
"cannot create idlcache table: \"%s\"\n", |
| 207 |
be->be_suffix[0].bv_val, wiredtiger_strerror(rc) ); |
| 208 |
return -1; |
| 209 |
} |
| 210 |
|
| 211 |
readonly: |
| 212 |
rc = wt_last_id( be, session, &wi->wi_lastid); |
| 213 |
if (rc) { |
| 214 |
snprintf( cr->msg, sizeof(cr->msg), "database \"%s\": " |
| 215 |
"last_id() failed: %s(%d).", |
| 216 |
be->be_suffix[0].bv_val, wiredtiger_strerror(rc), rc ); |
| 217 |
Debug( LDAP_DEBUG_ANY, "wt_db_open: %s\n", cr->msg ); |
| 218 |
return rc; |
| 219 |
} |
| 220 |
|
| 221 |
if (session) { |
| 222 |
session->close(session, NULL); |
| 223 |
} |
| 224 |
if (cache_session) { |
| 225 |
cache_session->close(cache_session, NULL); |
| 226 |
} |
| 227 |
|
| 228 |
wi->wi_flags |= WT_IS_OPEN; |
| 229 |
return LDAP_SUCCESS; |
| 230 |
} |
| 231 |
|
| 232 |
static int |
| 233 |
wt_db_close( BackendDB *be, ConfigReply *cr ) |
| 234 |
{ |
| 235 |
struct wt_info *wi = (struct wt_info *) be->be_private; |
| 236 |
int rc; |
| 237 |
|
| 238 |
if ( wi->wi_cache ) { |
| 239 |
rc = wi->wi_cache->close(wi->wi_cache, NULL); |
| 240 |
if( rc ) { |
| 241 |
Debug( LDAP_DEBUG_ANY, |
| 242 |
"wt_db_close: cannot close cache database (%d).\n", rc ); |
| 243 |
return -1; |
| 244 |
} |
| 245 |
} |
| 246 |
|
| 247 |
if ( wi->wi_conn ) { |
| 248 |
rc = wi->wi_conn->close(wi->wi_conn, NULL); |
| 249 |
if( rc ) { |
| 250 |
Debug( LDAP_DEBUG_ANY, |
| 251 |
"wt_db_close: cannot close database (%d).\n", rc ); |
| 252 |
return -1; |
| 253 |
} |
| 254 |
wi->wi_flags &= ~WT_IS_OPEN; |
| 255 |
} |
| 256 |
|
| 257 |
return LDAP_SUCCESS; |
| 258 |
} |
| 259 |
|
| 260 |
static int |
| 261 |
wt_db_destroy( Backend *be, ConfigReply *cr ) |
| 262 |
{ |
| 263 |
struct wt_info *wi = (struct wt_info *) be->be_private; |
| 264 |
|
| 265 |
if( wi->wi_home ) { |
| 266 |
ch_free( wi->wi_home ); |
| 267 |
wi->wi_home = NULL; |
| 268 |
} |
| 269 |
|
| 270 |
if( wi->wi_config ) { |
| 271 |
ch_free( wi->wi_config ); |
| 272 |
wi->wi_config = NULL; |
| 273 |
} |
| 274 |
|
| 275 |
wt_attr_index_destroy( wi ); |
| 276 |
ch_free( wi ); |
| 277 |
be->be_private = NULL; |
| 278 |
|
| 279 |
return LDAP_SUCCESS; |
| 280 |
} |
| 281 |
|
| 282 |
int |
| 283 |
wt_back_initialize( BackendInfo *bi ) |
| 284 |
{ |
| 285 |
static const char *controls[] = { |
| 286 |
LDAP_CONTROL_ASSERT, |
| 287 |
LDAP_CONTROL_MANAGEDSAIT, |
| 288 |
LDAP_CONTROL_NOOP, |
| 289 |
LDAP_CONTROL_PAGEDRESULTS, |
| 290 |
LDAP_CONTROL_PRE_READ, |
| 291 |
LDAP_CONTROL_POST_READ, |
| 292 |
LDAP_CONTROL_SUBENTRIES, |
| 293 |
LDAP_CONTROL_X_PERMISSIVE_MODIFY, |
| 294 |
#ifdef LDAP_X_TXN |
| 295 |
LDAP_CONTROL_X_TXN_SPEC, |
| 296 |
#endif |
| 297 |
NULL |
| 298 |
}; |
| 299 |
|
| 300 |
/* initialize the database system */ |
| 301 |
Debug( LDAP_DEBUG_TRACE, |
| 302 |
"wt_back_initialize: initialize WiredTiger backend\n" ); |
| 303 |
|
| 304 |
bi->bi_flags |= |
| 305 |
SLAP_BFLAG_INCREMENT | |
| 306 |
SLAP_BFLAG_SUBENTRIES | |
| 307 |
SLAP_BFLAG_ALIASES | |
| 308 |
SLAP_BFLAG_REFERRALS; |
| 309 |
|
| 310 |
bi->bi_controls = (char **)controls; |
| 311 |
|
| 312 |
/* version check */ |
| 313 |
Debug( LDAP_DEBUG_TRACE, |
| 314 |
"wt_back_initialize: %s\n", |
| 315 |
wiredtiger_version(NULL, NULL, NULL) ); |
| 316 |
|
| 317 |
bi->bi_open = 0; |
| 318 |
bi->bi_close = 0; |
| 319 |
bi->bi_config = 0; |
| 320 |
bi->bi_destroy = 0; |
| 321 |
|
| 322 |
bi->bi_db_init = wt_db_init; |
| 323 |
bi->bi_db_config = config_generic_wrapper; |
| 324 |
bi->bi_db_open = wt_db_open; |
| 325 |
bi->bi_db_close = wt_db_close; |
| 326 |
bi->bi_db_destroy = wt_db_destroy; |
| 327 |
|
| 328 |
bi->bi_op_add = wt_add; |
| 329 |
bi->bi_op_bind = wt_bind; |
| 330 |
bi->bi_op_unbind = 0; |
| 331 |
bi->bi_op_search = wt_search; |
| 332 |
bi->bi_op_compare = wt_compare; |
| 333 |
bi->bi_op_modify = wt_modify; |
| 334 |
bi->bi_op_modrdn = wt_modrdn; |
| 335 |
bi->bi_op_delete = wt_delete; |
| 336 |
bi->bi_op_abandon = 0; |
| 337 |
|
| 338 |
bi->bi_extended = wt_extended; |
| 339 |
#ifdef LDAP_X_TXN |
| 340 |
bi->bi_op_txn = 0; |
| 341 |
#endif |
| 342 |
|
| 343 |
bi->bi_chk_referrals = 0; |
| 344 |
bi->bi_operational = wt_operational; |
| 345 |
|
| 346 |
bi->bi_has_subordinates = wt_hasSubordinates; |
| 347 |
bi->bi_entry_release_rw = wt_entry_release; |
| 348 |
bi->bi_entry_get_rw = wt_entry_get; |
| 349 |
|
| 350 |
bi->bi_tool_entry_open = wt_tool_entry_open; |
| 351 |
bi->bi_tool_entry_close = wt_tool_entry_close; |
| 352 |
bi->bi_tool_entry_first = backend_tool_entry_first; |
| 353 |
bi->bi_tool_entry_first_x = wt_tool_entry_first_x; |
| 354 |
bi->bi_tool_entry_next = wt_tool_entry_next; |
| 355 |
bi->bi_tool_entry_get = wt_tool_entry_get; |
| 356 |
bi->bi_tool_entry_put = wt_tool_entry_put; |
| 357 |
bi->bi_tool_entry_reindex = wt_tool_entry_reindex; |
| 358 |
bi->bi_tool_sync = 0; |
| 359 |
bi->bi_tool_dn2id_get = wt_tool_dn2id_get; |
| 360 |
bi->bi_tool_entry_modify = wt_tool_entry_modify; |
| 361 |
|
| 362 |
#if LDAP_VENDOR_VERSION_MINOR == X || LDAP_VENDOR_VERSION_MINOR >= 5 |
| 363 |
bi->bi_tool_entry_delete = wt_tool_entry_delete; |
| 364 |
#endif |
| 365 |
|
| 366 |
bi->bi_connection_init = 0; |
| 367 |
bi->bi_connection_destroy = 0; |
| 368 |
|
| 369 |
return wt_back_init_cf( bi ); |
| 370 |
} |
| 371 |
|
| 372 |
#if SLAPD_WT == SLAPD_MOD_DYNAMIC |
| 373 |
|
| 374 |
/* conditionally define the init_module() function */ |
| 375 |
SLAP_BACKEND_INIT_MODULE( wt ) |
| 376 |
|
| 377 |
#endif /* SLAPD_WT == SLAPD_MOD_DYNAMIC */ |
| 378 |
|
| 379 |
/* |
| 380 |
* Local variables: |
| 381 |
* indent-tabs-mode: t |
| 382 |
* tab-width: 4 |
| 383 |
* c-basic-offset: 4 |
| 384 |
* End: |
| 385 |
*/ |