1 /* tools.c - tools for slap tools */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 2011-2017 The OpenLDAP Foundation.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted only as authorized by the OpenLDAP
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>.
20 #include <ac/string.h>
27 #ifdef MDB_TOOL_IDL_CACHING
28 static int mdb_tool_idl_flush( BackendDB *be, MDB_txn *txn );
32 typedef struct mdb_tool_idl_cache_entry {
33 struct mdb_tool_idl_cache_entry *next;
35 } mdb_tool_idl_cache_entry;
37 typedef struct mdb_tool_idl_cache {
39 mdb_tool_idl_cache_entry *head, *tail;
45 #define WAS_FOUND 0x01
46 #define WAS_RANGE 0x02
48 #define MDB_TOOL_IDL_FLUSH(be, txn) mdb_tool_idl_flush(be, txn)
50 #define MDB_TOOL_IDL_FLUSH(be, txn)
51 #endif /* MDB_TOOL_IDL_CACHING */
53 MDB_txn *mdb_tool_txn = NULL;
55 static MDB_txn *txi = NULL;
56 static MDB_cursor *cursor = NULL, *idcursor = NULL;
57 static MDB_cursor *mcp = NULL, *mcd = NULL;
58 static MDB_val key, data;
59 static ID previd = NOID;
61 typedef struct dn_id {
66 #define HOLE_SIZE 4096
67 static dn_id hbuf[HOLE_SIZE], *holes = hbuf;
68 static unsigned nhmax = HOLE_SIZE;
69 static unsigned nholes;
71 static struct berval *tool_base;
72 static int tool_scope;
73 static Filter *tool_filter;
74 static Entry *tool_next_entry;
76 static ID mdb_tool_ix_id;
77 static BackendDB *mdb_tool_ix_be;
78 static MDB_txn *mdb_tool_ix_txn;
79 static int mdb_tool_index_tcount, mdb_tool_threads;
80 static IndexRec *mdb_tool_index_rec;
81 static AttrIxInfo **mdb_tool_axinfo;
82 static struct mdb_info *mdb_tool_info;
83 static ldap_pvt_thread_mutex_t mdb_tool_index_mutex;
84 static ldap_pvt_thread_cond_t mdb_tool_index_cond_main;
85 static ldap_pvt_thread_cond_t mdb_tool_index_cond_work;
86 static void * mdb_tool_index_task( void *ctx, void *ptr );
88 static int mdb_writes, mdb_writes_per_commit;
90 /* Number of ops per commit in Quick mode.
91 * Batching speeds writes overall, but too large a
92 * batch will fail with MDB_TXN_FULL.
94 #ifndef MDB_WRITES_PER_COMMIT
95 #define MDB_WRITES_PER_COMMIT 500
99 mdb_tool_entry_get_int( BackendDB *be, ID id, Entry **ep );
101 int mdb_tool_entry_open(
102 BackendDB *be, int mode )
104 /* In Quick mode, commit once per 500 entries */
106 if ( slapMode & SLAP_TOOL_QUICK )
107 mdb_writes_per_commit = MDB_WRITES_PER_COMMIT;
109 mdb_writes_per_commit = 1;
111 #ifdef MDB_TOOL_IDL_CACHING /* threaded indexing has no performance advantage */
112 /* Set up for threaded slapindex */
113 if (( slapMode & (SLAP_TOOL_QUICK|SLAP_TOOL_READONLY)) == SLAP_TOOL_QUICK ) {
114 if ( !mdb_tool_info ) {
115 struct mdb_info *mdb = (struct mdb_info *) be->be_private;
116 ldap_pvt_thread_mutex_init( &mdb_tool_index_mutex );
117 ldap_pvt_thread_cond_init( &mdb_tool_index_cond_main );
118 ldap_pvt_thread_cond_init( &mdb_tool_index_cond_work );
119 if ( mdb->mi_nattrs ) {
121 mdb_tool_threads = slap_tool_thread_max - 1;
122 if ( mdb_tool_threads > 1 ) {
123 mdb_tool_index_rec = ch_calloc( mdb->mi_nattrs, sizeof( IndexRec ));
124 mdb_tool_axinfo = ch_calloc( mdb_tool_threads, sizeof( AttrIxInfo* ) +
125 sizeof( AttrIxInfo ));
126 mdb_tool_axinfo[0] = (AttrIxInfo *)(mdb_tool_axinfo + mdb_tool_threads);
127 for (i=1; i<mdb_tool_threads; i++)
128 mdb_tool_axinfo[i] = mdb_tool_axinfo[i-1]+1;
129 mdb_tool_index_tcount = mdb_tool_threads - 1;
131 for (i=1; i<mdb_tool_threads; i++) {
132 int *ptr = ch_malloc( sizeof( int ));
134 ldap_pvt_thread_pool_submit( &connection_pool,
135 mdb_tool_index_task, ptr );
147 int mdb_tool_entry_close(
150 #ifdef MDB_TOOL_IDL_CACHING
151 if ( mdb_tool_info ) {
154 ldap_pvt_thread_mutex_lock( &mdb_tool_index_mutex );
156 /* There might still be some threads starting */
157 while ( mdb_tool_index_tcount > 0 ) {
158 ldap_pvt_thread_cond_wait( &mdb_tool_index_cond_main,
159 &mdb_tool_index_mutex );
162 mdb_tool_index_tcount = mdb_tool_threads - 1;
163 ldap_pvt_thread_cond_broadcast( &mdb_tool_index_cond_work );
165 /* Make sure all threads are stopped */
166 while ( mdb_tool_index_tcount > 0 ) {
167 ldap_pvt_thread_cond_wait( &mdb_tool_index_cond_main,
168 &mdb_tool_index_mutex );
170 ldap_pvt_thread_mutex_unlock( &mdb_tool_index_mutex );
172 mdb_tool_info = NULL;
174 ch_free( mdb_tool_index_rec );
175 mdb_tool_index_tcount = mdb_tool_threads - 1;
177 MDB_TOOL_IDL_FLUSH( be, mdb_tool_txn );
178 for (i=0; i<mdb_tool_threads; i++) {
179 mdb_tool_idl_cache *ic;
180 mdb_tool_idl_cache_entry *ice;
181 while ((ic = mdb_tool_axinfo[i]->ai_clist)) {
182 mdb_tool_axinfo[i]->ai_clist = ic->head;
185 while ((ice = mdb_tool_axinfo[i]->ai_flist)) {
186 mdb_tool_axinfo[i]->ai_flist = ice->next;
194 mdb_cursor_close( idcursor );
198 mdb_cursor_close( cursor );
202 struct mdb_info *mdb = be->be_private;
205 for (i=0; i<mdb->mi_nattrs; i++)
206 mdb->mi_attrs[i]->ai_cursor = NULL;
211 if (( rc = mdb_txn_commit( mdb_tool_txn ))) {
212 Debug( LDAP_DEBUG_ANY,
213 LDAP_XSTRING(mdb_tool_entry_close) ": database %s: "
214 "txn_commit failed: %s (%d)\n",
215 be->be_suffix[0].bv_val, mdb_strerror(rc), rc );
223 fprintf( stderr, "Error, entries missing!\n");
224 for (i=0; i<nholes; i++) {
225 fprintf(stderr, " entry %ld: %s\n",
226 holes[i].id, holes[i].dn.bv_val);
236 mdb_tool_entry_first_x(
246 return mdb_tool_entry_next( be );
249 ID mdb_tool_entry_next(
254 struct mdb_info *mdb;
256 assert( be != NULL );
257 assert( slapMode & SLAP_TOOL_MODE );
259 mdb = (struct mdb_info *) be->be_private;
260 assert( mdb != NULL );
262 if ( !mdb_tool_txn ) {
263 rc = mdb_txn_begin( mdb->mi_dbenv, NULL, MDB_RDONLY, &mdb_tool_txn );
266 rc = mdb_cursor_open( mdb_tool_txn, mdb->mi_id2entry, &cursor );
268 mdb_txn_abort( mdb_tool_txn );
274 rc = mdb_cursor_get( cursor, &key, &data, MDB_NEXT );
280 previd = *(ID *)key.mv_data;
286 if ( tool_filter || tool_base ) {
287 static Operation op = {0};
288 static Opheader ohdr = {0};
292 op.o_tmpmemctx = NULL;
293 op.o_tmpmfuncs = &ch_mfuncs;
295 if ( tool_next_entry ) {
296 mdb_entry_release( &op, tool_next_entry, 0 );
297 tool_next_entry = NULL;
300 rc = mdb_tool_entry_get_int( be, id, &tool_next_entry );
301 if ( rc == LDAP_NO_SUCH_OBJECT ) {
305 assert( tool_next_entry != NULL );
307 if ( tool_filter && test_filter( NULL, tool_next_entry, tool_filter ) != LDAP_COMPARE_TRUE )
309 mdb_entry_release( &op, tool_next_entry, 0 );
310 tool_next_entry = NULL;
318 ID mdb_tool_dn2id_get(
323 struct mdb_info *mdb;
329 if ( BER_BVISEMPTY(dn) )
332 mdb = (struct mdb_info *) be->be_private;
334 if ( !mdb_tool_txn ) {
335 rc = mdb_txn_begin( mdb->mi_dbenv, NULL, (slapMode & SLAP_TOOL_READONLY) != 0 ?
336 MDB_RDONLY : 0, &mdb_tool_txn );
343 op.o_tmpmemctx = NULL;
344 op.o_tmpmfuncs = &ch_mfuncs;
346 rc = mdb_dn2id( &op, mdb_tool_txn, NULL, dn, &id, NULL, NULL, NULL );
347 if ( rc == MDB_NOTFOUND )
354 mdb_tool_entry_get_int( BackendDB *be, ID id, Entry **ep )
360 struct berval dn = BER_BVNULL, ndn = BER_BVNULL;
363 assert( be != NULL );
364 assert( slapMode & SLAP_TOOL_MODE );
366 if ( ( tool_filter || tool_base ) && id == previd && tool_next_entry != NULL ) {
367 *ep = tool_next_entry;
368 tool_next_entry = NULL;
372 if ( id != previd ) {
373 key.mv_size = sizeof(ID);
375 rc = mdb_cursor_get( cursor, &key, &data, MDB_SET );
381 if ( !data.mv_size ) {
382 rc = LDAP_NO_SUCH_OBJECT;
388 op.o_tmpmemctx = NULL;
389 op.o_tmpmfuncs = &ch_mfuncs;
390 if ( slapMode & SLAP_TOOL_READONLY ) {
391 rc = mdb_id2name( &op, mdb_tool_txn, &idcursor, id, &dn, &ndn );
396 if ( tool_base != NULL ) {
397 if ( !dnIsSuffixScope( &ndn, tool_base, tool_scope ) ) {
398 ch_free( dn.bv_val );
399 ch_free( ndn.bv_val );
400 rc = LDAP_NO_SUCH_OBJECT;
405 rc = mdb_entry_decode( &op, mdb_tool_txn, &data, id, &e );
407 if ( !BER_BVISNULL( &dn )) {
411 e->e_name.bv_val = NULL;
412 e->e_nname.bv_val = NULL;
424 mdb_tool_entry_get( BackendDB *be, ID id )
429 if ( !mdb_tool_txn ) {
430 struct mdb_info *mdb = (struct mdb_info *) be->be_private;
431 rc = mdb_txn_begin( mdb->mi_dbenv, NULL,
432 (slapMode & SLAP_TOOL_READONLY) ? MDB_RDONLY : 0, &mdb_tool_txn );
437 struct mdb_info *mdb = (struct mdb_info *) be->be_private;
438 rc = mdb_cursor_open( mdb_tool_txn, mdb->mi_id2entry, &cursor );
440 mdb_txn_abort( mdb_tool_txn );
445 (void)mdb_tool_entry_get_int( be, id, &e );
449 static int mdb_tool_next_id(
456 struct mdb_info *mdb = (struct mdb_info *) op->o_bd->be_private;
457 struct berval dn = e->e_name;
458 struct berval ndn = e->e_nname;
459 struct berval pdn, npdn, nmatched;
463 if (ndn.bv_len == 0) {
468 rc = mdb_dn2id( op, tid, mcp, &ndn, &id, NULL, NULL, &nmatched );
469 if ( rc == MDB_NOTFOUND ) {
470 if ( !be_issuffix( op->o_bd, &ndn ) ) {
472 dnParent( &ndn, &npdn );
473 if ( nmatched.bv_len != npdn.bv_len ) {
474 dnParent( &dn, &pdn );
477 rc = mdb_tool_next_id( op, tid, e, text, 1 );
483 /* If parent didn't exist, it was created just now
484 * and its ID is now in e->e_id. Make sure the current
485 * entry gets added under the new parent ID.
487 if ( eid != e->e_id ) {
494 rc = mdb_next_id( op->o_bd, idcursor, &e->e_id );
496 snprintf( text->bv_val, text->bv_len,
497 "next_id failed: %s (%d)",
498 mdb_strerror(rc), rc );
499 Debug( LDAP_DEBUG_ANY,
500 "=> mdb_tool_next_id: %s\n", text->bv_val, 0, 0 );
503 rc = mdb_dn2id_add( op, mcp, mcd, pid, 1, 1, e );
505 snprintf( text->bv_val, text->bv_len,
506 "dn2id_add failed: %s (%d)",
507 mdb_strerror(rc), rc );
508 Debug( LDAP_DEBUG_ANY,
509 "=> mdb_tool_next_id: %s\n", text->bv_val, 0, 0 );
512 if ( nholes == nhmax - 1 ) {
513 if ( holes == hbuf ) {
514 holes = ch_malloc( nhmax * sizeof(dn_id) * 2 );
515 AC_MEMCPY( holes, hbuf, sizeof(hbuf) );
517 holes = ch_realloc( holes, nhmax * sizeof(dn_id) * 2 );
521 ber_dupbv( &holes[nholes].dn, &ndn );
522 holes[nholes++].id = e->e_id;
523 key.mv_size = sizeof(ID);
524 key.mv_data = &e->e_id;
527 rc = mdb_cursor_put( idcursor, &key, &data, MDB_NOOVERWRITE );
528 if ( rc == MDB_KEYEXIST )
531 snprintf( text->bv_val, text->bv_len,
532 "dummy id2entry add failed: %s (%d)",
533 mdb_strerror(rc), rc );
534 Debug( LDAP_DEBUG_ANY,
535 "=> mdb_tool_next_id: %s\n", text->bv_val, 0, 0 );
538 } else if ( !hole ) {
543 for ( i=0; i<nholes; i++) {
544 if ( holes[i].id == e->e_id ) {
545 free(holes[i].dn.bv_val);
546 for (j=i;j<nholes;j++) holes[j] = holes[j+1];
550 } else if ( holes[i].id > e->e_id ) {
564 struct mdb_info *mdb = (struct mdb_info *) op->o_bd->be_private;
566 if ( !mdb->mi_nattrs )
569 if ( mdb_tool_threads > 1 ) {
574 ir = mdb_tool_index_rec;
575 for (i=0; i<mdb->mi_nattrs; i++)
576 ir[i].ir_attrs = NULL;
578 for ( a = e->e_attrs; a != NULL; a = a->a_next ) {
579 rc = mdb_index_recset( mdb, a, a->a_desc->ad_type,
580 &a->a_desc->ad_tags, ir );
584 for (i=0; i<mdb->mi_nattrs; i++) {
587 rc = mdb_cursor_open( txn, ir[i].ir_ai->ai_dbi,
588 &ir[i].ir_ai->ai_cursor );
592 mdb_tool_ix_id = e->e_id;
593 mdb_tool_ix_txn = txn;
594 ldap_pvt_thread_mutex_lock( &mdb_tool_index_mutex );
595 /* Wait for all threads to be ready */
596 while ( mdb_tool_index_tcount ) {
597 ldap_pvt_thread_cond_wait( &mdb_tool_index_cond_main,
598 &mdb_tool_index_mutex );
601 for ( i=1; i<mdb_tool_threads; i++ )
602 mdb_tool_index_rec[i].ir_i = LDAP_BUSY;
603 mdb_tool_index_tcount = mdb_tool_threads - 1;
604 ldap_pvt_thread_cond_broadcast( &mdb_tool_index_cond_work );
605 ldap_pvt_thread_mutex_unlock( &mdb_tool_index_mutex );
607 return mdb_index_recrun( op, txn, mdb, ir, e->e_id, 0 );
610 return mdb_index_entry_add( op, txn, e );
615 mdb_tool_index_finish()
618 ldap_pvt_thread_mutex_lock( &mdb_tool_index_mutex );
619 for ( i=1; i<mdb_tool_threads; i++ ) {
620 if ( mdb_tool_index_rec[i].ir_i == LDAP_BUSY ) {
621 ldap_pvt_thread_cond_wait( &mdb_tool_index_cond_main,
622 &mdb_tool_index_mutex );
626 if ( mdb_tool_index_rec[i].ir_i ) {
627 rc = mdb_tool_index_rec[i].ir_i;
631 ldap_pvt_thread_mutex_unlock( &mdb_tool_index_mutex );
635 ID mdb_tool_entry_put(
638 struct berval *text )
641 struct mdb_info *mdb;
645 assert( be != NULL );
646 assert( slapMode & SLAP_TOOL_MODE );
648 assert( text != NULL );
649 assert( text->bv_val != NULL );
650 assert( text->bv_val[0] == '\0' ); /* overconservative? */
652 Debug( LDAP_DEBUG_TRACE, "=> " LDAP_XSTRING(mdb_tool_entry_put)
653 "( %ld, \"%s\" )\n", (long) e->e_id, e->e_dn, 0 );
655 mdb = (struct mdb_info *) be->be_private;
657 if ( !mdb_tool_txn ) {
658 rc = mdb_txn_begin( mdb->mi_dbenv, NULL, 0, &mdb_tool_txn );
660 snprintf( text->bv_val, text->bv_len,
661 "txn_begin failed: %s (%d)",
662 mdb_strerror(rc), rc );
663 Debug( LDAP_DEBUG_ANY,
664 "=> " LDAP_XSTRING(mdb_tool_entry_put) ": %s\n",
665 text->bv_val, 0, 0 );
668 rc = mdb_cursor_open( mdb_tool_txn, mdb->mi_id2entry, &idcursor );
670 snprintf( text->bv_val, text->bv_len,
671 "cursor_open failed: %s (%d)",
672 mdb_strerror(rc), rc );
673 Debug( LDAP_DEBUG_ANY,
674 "=> " LDAP_XSTRING(mdb_tool_entry_put) ": %s\n",
675 text->bv_val, 0, 0 );
678 if ( !mdb->mi_nextid ) {
680 mdb_next_id( be, idcursor, &dummy );
682 rc = mdb_cursor_open( mdb_tool_txn, mdb->mi_dn2id, &mcp );
684 snprintf( text->bv_val, text->bv_len,
685 "cursor_open failed: %s (%d)",
686 mdb_strerror(rc), rc );
687 Debug( LDAP_DEBUG_ANY,
688 "=> " LDAP_XSTRING(mdb_tool_entry_put) ": %s\n",
689 text->bv_val, 0, 0 );
692 rc = mdb_cursor_open( mdb_tool_txn, mdb->mi_dn2id, &mcd );
694 snprintf( text->bv_val, text->bv_len,
695 "cursor_open failed: %s (%d)",
696 mdb_strerror(rc), rc );
697 Debug( LDAP_DEBUG_ANY,
698 "=> " LDAP_XSTRING(mdb_tool_entry_put) ": %s\n",
699 text->bv_val, 0, 0 );
706 op.o_tmpmemctx = NULL;
707 op.o_tmpmfuncs = &ch_mfuncs;
709 /* add dn2id indices */
710 rc = mdb_tool_next_id( &op, mdb_tool_txn, e, text, 0 );
715 if ( mdb_tool_threads > 1 ) {
716 LDAP_SLIST_INSERT_HEAD( &op.o_extra, &mdb_tool_axinfo[0]->ai_oe, oe_next );
718 rc = mdb_tool_index_add( &op, mdb_tool_txn, e );
720 snprintf( text->bv_val, text->bv_len,
721 "index_entry_add failed: err=%d", rc );
722 Debug( LDAP_DEBUG_ANY,
723 "=> " LDAP_XSTRING(mdb_tool_entry_put) ": %s\n",
724 text->bv_val, 0, 0 );
730 rc = mdb_id2entry_add( &op, mdb_tool_txn, idcursor, e );
732 snprintf( text->bv_val, text->bv_len,
733 "id2entry_add failed: err=%d", rc );
734 Debug( LDAP_DEBUG_ANY,
735 "=> " LDAP_XSTRING(mdb_tool_entry_put) ": %s\n",
736 text->bv_val, 0, 0 );
740 if( mdb->mi_nattrs && mdb_tool_threads > 1 )
741 rc = mdb_tool_index_finish();
746 if ( mdb_writes >= mdb_writes_per_commit ) {
748 MDB_TOOL_IDL_FLUSH( be, mdb_tool_txn );
749 rc = mdb_txn_commit( mdb_tool_txn );
750 for ( i=0; i<mdb->mi_nattrs; i++ )
751 mdb->mi_attrs[i]->ai_cursor = NULL;
757 snprintf( text->bv_val, text->bv_len,
758 "txn_commit failed: %s (%d)",
759 mdb_strerror(rc), rc );
760 Debug( LDAP_DEBUG_ANY,
761 "=> " LDAP_XSTRING(mdb_tool_entry_put) ": %s\n",
762 text->bv_val, 0, 0 );
769 mdb_txn_abort( mdb_tool_txn );
772 for ( i=0; i<mdb->mi_nattrs; i++ )
773 mdb->mi_attrs[i]->ai_cursor = NULL;
775 snprintf( text->bv_val, text->bv_len,
776 "txn_aborted! %s (%d)",
777 rc == LDAP_OTHER ? "Internal error" :
778 mdb_strerror(rc), rc );
779 Debug( LDAP_DEBUG_ANY,
780 "=> " LDAP_XSTRING(mdb_tool_entry_put) ": %s\n",
781 text->bv_val, 0, 0 );
788 static int mdb_dn2id_upgrade( BackendDB *be );
790 int mdb_tool_entry_reindex(
793 AttributeDescription **adv )
795 struct mdb_info *mi = (struct mdb_info *) be->be_private;
801 Debug( LDAP_DEBUG_ARGS,
802 "=> " LDAP_XSTRING(mdb_tool_entry_reindex) "( %ld )\n",
804 assert( tool_base == NULL );
805 assert( tool_filter == NULL );
807 /* Special: do a dn2id upgrade */
808 if ( adv && adv[0] == slap_schema.si_ad_entryDN ) {
809 /* short-circuit tool_entry_next() */
810 mdb_cursor_get( cursor, &key, &data, MDB_LAST );
811 return mdb_dn2id_upgrade( be );
814 /* No indexes configured, nothing to do. Could return an
815 * error here to shortcut things.
821 /* Check for explicit list of attrs to index */
825 if ( mi->mi_attrs[0]->ai_desc != adv[0] ) {
827 for ( n = 0; adv[n]; n++ ) ;
830 for ( i = 0; i < n; i++ ) {
831 AttributeDescription *ad = adv[i];
832 for ( j = i-1; j>=0; j--) {
833 if ( SLAP_PTRCMP( adv[j], ad ) <= 0 ) break;
840 for ( i = 0; adv[i]; i++ ) {
841 if ( mi->mi_attrs[i]->ai_desc != adv[i] ) {
842 for ( j = i+1; j < mi->mi_nattrs; j++ ) {
843 if ( mi->mi_attrs[j]->ai_desc == adv[i] ) {
844 AttrInfo *ai = mi->mi_attrs[i];
845 mi->mi_attrs[i] = mi->mi_attrs[j];
846 mi->mi_attrs[j] = ai;
850 if ( j == mi->mi_nattrs ) {
851 Debug( LDAP_DEBUG_ANY,
852 LDAP_XSTRING(mdb_tool_entry_reindex)
853 ": no index configured for %s\n",
854 adv[i]->ad_cname.bv_val, 0, 0 );
862 e = mdb_tool_entry_get( be, id );
865 Debug( LDAP_DEBUG_ANY,
866 LDAP_XSTRING(mdb_tool_entry_reindex)
867 ": could not locate id=%ld\n",
873 rc = mdb_txn_begin( mi->mi_dbenv, NULL, 0, &txi );
875 Debug( LDAP_DEBUG_ANY,
876 "=> " LDAP_XSTRING(mdb_tool_entry_reindex) ": "
877 "txn_begin failed: %s (%d)\n",
878 mdb_strerror(rc), rc, 0 );
883 if ( slapMode & SLAP_TRUNCATE_MODE ) {
885 for ( i=0; i < mi->mi_nattrs; i++ ) {
886 rc = mdb_drop( txi, mi->mi_attrs[i]->ai_dbi, 0 );
888 Debug( LDAP_DEBUG_ANY,
889 LDAP_XSTRING(mdb_tool_entry_reindex)
890 ": (Truncate) mdb_drop(%s) failed: %s (%d)\n",
891 mi->mi_attrs[i]->ai_desc->ad_type->sat_cname.bv_val,
892 mdb_strerror(rc), rc );
896 slapMode ^= SLAP_TRUNCATE_MODE;
900 * just (re)add them for now
901 * Use truncate mode to empty/reset index databases
904 Debug( LDAP_DEBUG_TRACE,
905 "=> " LDAP_XSTRING(mdb_tool_entry_reindex) "( %ld )\n",
910 op.o_tmpmemctx = NULL;
911 op.o_tmpmfuncs = &ch_mfuncs;
913 rc = mdb_tool_index_add( &op, txi, e );
918 if ( mdb_writes >= mdb_writes_per_commit ) {
921 MDB_TOOL_IDL_FLUSH( be, txi );
922 rc = mdb_txn_commit( txi );
924 for ( i=0; i<mi->mi_nattrs; i++ )
925 mi->mi_attrs[i]->ai_cursor = NULL;
927 Debug( LDAP_DEBUG_ANY,
928 "=> " LDAP_XSTRING(mdb_tool_entry_reindex)
929 ": txn_commit failed: %s (%d)\n",
930 mdb_strerror(rc), rc, 0 );
933 mdb_cursor_close( cursor );
935 /* Must close the read txn to allow old pages to be reclaimed. */
936 mdb_txn_abort( mdb_tool_txn );
937 /* and then reopen it so that tool_entry_next still works. */
938 mdb_txn_begin( mi->mi_dbenv, NULL, MDB_RDONLY, &mdb_tool_txn );
939 mdb_cursor_open( mdb_tool_txn, mi->mi_id2entry, &cursor );
941 key.mv_size = sizeof(ID);
942 mdb_cursor_get( cursor, &key, NULL, MDB_SET );
948 mdb_cursor_close( cursor );
950 mdb_txn_abort( txi );
951 for ( i=0; i<mi->mi_nattrs; i++ )
952 mi->mi_attrs[i]->ai_cursor = NULL;
953 Debug( LDAP_DEBUG_ANY,
954 "=> " LDAP_XSTRING(mdb_tool_entry_reindex)
955 ": txn_aborted! err=%d\n",
960 mdb_entry_release( &op, e, 0 );
965 ID mdb_tool_entry_modify(
968 struct berval *text )
971 struct mdb_info *mdb;
975 assert( be != NULL );
976 assert( slapMode & SLAP_TOOL_MODE );
978 assert( text != NULL );
979 assert( text->bv_val != NULL );
980 assert( text->bv_val[0] == '\0' ); /* overconservative? */
982 assert ( e->e_id != NOID );
984 Debug( LDAP_DEBUG_TRACE,
985 "=> " LDAP_XSTRING(mdb_tool_entry_modify) "( %ld, \"%s\" )\n",
986 (long) e->e_id, e->e_dn, 0 );
988 mdb = (struct mdb_info *) be->be_private;
991 mdb_cursor_close( cursor );
994 if ( !mdb_tool_txn ) {
995 rc = mdb_txn_begin( mdb->mi_dbenv, NULL, 0, &mdb_tool_txn );
997 snprintf( text->bv_val, text->bv_len,
998 "txn_begin failed: %s (%d)",
999 mdb_strerror(rc), rc );
1000 Debug( LDAP_DEBUG_ANY,
1001 "=> " LDAP_XSTRING(mdb_tool_entry_modify) ": %s\n",
1002 text->bv_val, 0, 0 );
1009 op.o_tmpmemctx = NULL;
1010 op.o_tmpmfuncs = &ch_mfuncs;
1012 /* id2entry index */
1013 rc = mdb_id2entry_update( &op, mdb_tool_txn, NULL, e );
1015 snprintf( text->bv_val, text->bv_len,
1016 "id2entry_update failed: err=%d", rc );
1017 Debug( LDAP_DEBUG_ANY,
1018 "=> " LDAP_XSTRING(mdb_tool_entry_modify) ": %s\n",
1019 text->bv_val, 0, 0 );
1025 rc = mdb_txn_commit( mdb_tool_txn );
1028 snprintf( text->bv_val, text->bv_len,
1029 "txn_commit failed: %s (%d)",
1030 mdb_strerror(rc), rc );
1031 Debug( LDAP_DEBUG_ANY,
1032 "=> " LDAP_XSTRING(mdb_tool_entry_modify) ": "
1033 "%s\n", text->bv_val, 0, 0 );
1038 mdb_txn_abort( mdb_tool_txn );
1039 snprintf( text->bv_val, text->bv_len,
1040 "txn_aborted! %s (%d)",
1041 mdb_strerror(rc), rc );
1042 Debug( LDAP_DEBUG_ANY,
1043 "=> " LDAP_XSTRING(mdb_tool_entry_modify) ": %s\n",
1044 text->bv_val, 0, 0 );
1047 mdb_tool_txn = NULL;
1052 int mdb_tool_entry_delete(
1055 struct berval *text )
1058 struct mdb_info *mdb;
1060 Opheader ohdr = {0};
1063 assert( be != NULL );
1064 assert( slapMode & SLAP_TOOL_MODE );
1066 assert( text != NULL );
1067 assert( text->bv_val != NULL );
1068 assert( text->bv_val[0] == '\0' ); /* overconservative? */
1070 assert ( ndn != NULL );
1071 assert ( ndn->bv_val != NULL );
1073 Debug( LDAP_DEBUG_TRACE,
1074 "=> " LDAP_XSTRING(mdb_tool_entry_delete) "( %s )\n",
1075 ndn->bv_val, 0, 0 );
1077 mdb = (struct mdb_info *) be->be_private;
1079 assert( cursor == NULL );
1081 mdb_cursor_close( cursor );
1084 if( !mdb_tool_txn ) {
1085 rc = mdb_txn_begin( mdb->mi_dbenv, NULL, 0, &mdb_tool_txn );
1087 snprintf( text->bv_val, text->bv_len,
1088 "txn_begin failed: %s (%d)",
1089 mdb_strerror(rc), rc );
1090 Debug( LDAP_DEBUG_ANY,
1091 "=> " LDAP_XSTRING(mdb_tool_entry_delete) ": %s\n",
1092 text->bv_val, 0, 0 );
1097 rc = mdb_cursor_open( mdb_tool_txn, mdb->mi_dn2id, &cursor );
1099 snprintf( text->bv_val, text->bv_len,
1100 "cursor_open failed: %s (%d)",
1101 mdb_strerror(rc), rc );
1102 Debug( LDAP_DEBUG_ANY,
1103 "=> " LDAP_XSTRING(mdb_tool_entry_delete) ": %s\n",
1104 text->bv_val, 0, 0 );
1110 op.o_tmpmemctx = NULL;
1111 op.o_tmpmfuncs = &ch_mfuncs;
1113 rc = mdb_dn2entry( &op, mdb_tool_txn, cursor, ndn, &e, NULL, 0 );
1115 snprintf( text->bv_val, text->bv_len,
1116 "dn2entry failed: %s (%d)",
1117 mdb_strerror(rc), rc );
1118 Debug( LDAP_DEBUG_ANY,
1119 "=> " LDAP_XSTRING(mdb_tool_entry_delete) ": %s\n",
1120 text->bv_val, 0, 0 );
1124 /* check that we wouldn't orphan any children */
1125 rc = mdb_dn2id_children( &op, mdb_tool_txn, e );
1126 if( rc != MDB_NOTFOUND ) {
1129 snprintf( text->bv_val, text->bv_len,
1131 " subordinate objects must be deleted first");
1134 snprintf( text->bv_val, text->bv_len,
1135 "has_children failed: %s (%d)",
1136 mdb_strerror(rc), rc );
1140 Debug( LDAP_DEBUG_ANY,
1141 "=> " LDAP_XSTRING(mdb_tool_entry_delete) ": %s\n",
1142 text->bv_val, 0, 0 );
1146 /* delete from dn2id */
1147 rc = mdb_dn2id_delete( &op, cursor, e->e_id, 1 );
1149 snprintf( text->bv_val, text->bv_len,
1150 "dn2id_delete failed: err=%d", rc );
1151 Debug( LDAP_DEBUG_ANY,
1152 "=> " LDAP_XSTRING(mdb_tool_entry_delete) ": %s\n",
1153 text->bv_val, 0, 0 );
1157 /* deindex values */
1158 rc = mdb_index_entry_del( &op, mdb_tool_txn, e );
1160 snprintf( text->bv_val, text->bv_len,
1161 "entry_delete failed: err=%d", rc );
1162 Debug( LDAP_DEBUG_ANY,
1163 "=> " LDAP_XSTRING(mdb_tool_entry_delete) ": %s\n",
1164 text->bv_val, 0, 0 );
1168 /* do the deletion */
1169 rc = mdb_id2entry_delete( be, mdb_tool_txn, e );
1171 snprintf( text->bv_val, text->bv_len,
1172 "id2entry_update failed: err=%d", rc );
1173 Debug( LDAP_DEBUG_ANY,
1174 "=> " LDAP_XSTRING(mdb_tool_entry_delete) ": %s\n",
1175 text->bv_val, 0, 0 );
1182 mdb_entry_return( &op, e );
1186 rc = mdb_txn_commit( mdb_tool_txn );
1188 snprintf( text->bv_val, text->bv_len,
1189 "txn_commit failed: %s (%d)",
1190 mdb_strerror(rc), rc );
1191 Debug( LDAP_DEBUG_ANY,
1192 "=> " LDAP_XSTRING(mdb_tool_entry_delete) ": "
1193 "%s\n", text->bv_val, 0, 0 );
1197 mdb_txn_abort( mdb_tool_txn );
1198 snprintf( text->bv_val, text->bv_len,
1199 "txn_aborted! %s (%d)",
1200 mdb_strerror(rc), rc );
1201 Debug( LDAP_DEBUG_ANY,
1202 "=> " LDAP_XSTRING(mdb_tool_entry_delete) ": %s\n",
1203 text->bv_val, 0, 0 );
1205 mdb_tool_txn = NULL;
1212 mdb_tool_index_task( void *ctx, void *ptr )
1214 int base = *(int *)ptr;
1216 Opheader ohdr = {0};
1217 AttrIxInfo ai = {0}, *aio;
1221 op.o_bd = mdb_tool_ix_be;
1222 op.o_tmpmemctx = NULL;
1223 op.o_tmpmfuncs = &ch_mfuncs;
1224 aio = mdb_tool_axinfo[base];
1225 mdb_tool_axinfo[base] = &ai;
1226 LDAP_SLIST_INSERT_HEAD( &op.o_extra, &ai.ai_oe, oe_next );
1228 ldap_pvt_thread_mutex_lock( &mdb_tool_index_mutex );
1229 mdb_tool_index_tcount--;
1230 if ( !mdb_tool_index_tcount )
1231 ldap_pvt_thread_cond_signal( &mdb_tool_index_cond_main );
1232 ldap_pvt_thread_cond_wait( &mdb_tool_index_cond_work,
1233 &mdb_tool_index_mutex );
1234 if ( slapd_shutdown ) {
1235 mdb_tool_index_tcount--;
1236 if ( !mdb_tool_index_tcount )
1237 ldap_pvt_thread_cond_signal( &mdb_tool_index_cond_main );
1239 mdb_tool_axinfo[base] = aio;
1240 ldap_pvt_thread_mutex_unlock( &mdb_tool_index_mutex );
1243 ldap_pvt_thread_mutex_unlock( &mdb_tool_index_mutex );
1244 mdb_tool_index_rec[base].ir_i = mdb_index_recrun( &op,
1246 mdb_tool_info, mdb_tool_index_rec, mdb_tool_ix_id, base );
1252 #ifdef MDB_TOOL_IDL_CACHING
1254 mdb_tool_idl_cmp( const void *v1, const void *v2 )
1256 const mdb_tool_idl_cache *c1 = v1, *c2 = v2;
1259 if (( rc = c1->kstr.bv_len - c2->kstr.bv_len )) return rc;
1260 return memcmp( c1->kstr.bv_val, c2->kstr.bv_val, c1->kstr.bv_len );
1264 mdb_tool_idl_flush_one( MDB_cursor *mc, AttrIxInfo *ai, mdb_tool_idl_cache *ic )
1266 mdb_tool_idl_cache_entry *ice;
1267 MDB_val key, data[2];
1271 /* Freshly allocated, ignore it */
1272 if ( !ic->head && ic->count <= MDB_IDL_DB_SIZE ) {
1276 key.mv_data = ic->kstr.bv_val;
1277 key.mv_size = ic->kstr.bv_len;
1279 if ( ic->count > MDB_IDL_DB_SIZE ) {
1280 while ( ic->flags & WAS_FOUND ) {
1281 rc = mdb_cursor_get( mc, &key, data, MDB_SET );
1283 /* FIXME: find out why this happens */
1287 if ( ic->flags & WAS_RANGE ) {
1289 rc = mdb_cursor_get( mc, &key, data, MDB_NEXT_DUP );
1292 rc = mdb_cursor_get( mc, &key, data, MDB_NEXT_DUP );
1294 /* Store range hi */
1295 data[0].mv_data = &ic->last;
1296 rc = mdb_cursor_put( mc, &key, data, MDB_CURRENT );
1298 /* Delete old data, replace with range */
1299 ic->first = *(ID *)data[0].mv_data;
1300 mdb_cursor_del( mc, MDB_NODUPDATA );
1304 if ( !(ic->flags & WAS_RANGE)) {
1305 /* range, didn't exist before */
1307 data[0].mv_size = sizeof(ID);
1308 data[0].mv_data = &nid;
1309 rc = mdb_cursor_put( mc, &key, data, 0 );
1311 data[0].mv_data = &ic->first;
1312 rc = mdb_cursor_put( mc, &key, data, 0 );
1314 data[0].mv_data = &ic->last;
1315 rc = mdb_cursor_put( mc, &key, data, 0 );
1326 data[0].mv_size = sizeof(ID);
1328 for ( ice = ic->head, n=0; ice; ice = ice->next, n++ ) {
1333 end = (ic->count-ic->offset) & (IDBLOCK-1);
1337 data[1].mv_size = end;
1338 data[0].mv_data = ice->ids;
1339 rc = mdb_cursor_put( mc, &key, data, MDB_APPENDDUP|MDB_MULTIPLE );
1346 ic->tail->next = ai->ai_flist;
1347 ai->ai_flist = ic->head;
1350 ic->head = ai->ai_clist;
1356 mdb_tool_idl_flush_db( MDB_txn *txn, AttrInfo *ai, AttrIxInfo *ax )
1362 mdb_cursor_open( txn, ai->ai_dbi, &mc );
1363 root = tavl_end( ai->ai_root, TAVL_DIR_LEFT );
1365 rc = mdb_tool_idl_flush_one( mc, ax, root->avl_data );
1368 } while ((root = tavl_next(root, TAVL_DIR_RIGHT)));
1369 mdb_cursor_close( mc );
1375 mdb_tool_idl_flush( BackendDB *be, MDB_txn *txn )
1377 struct mdb_info *mdb = (struct mdb_info *) be->be_private;
1379 unsigned int i, dbi;
1381 for ( i=0; i < mdb->mi_nattrs; i++ ) {
1382 if ( !mdb->mi_attrs[i]->ai_root ) continue;
1383 rc = mdb_tool_idl_flush_db( txn, mdb->mi_attrs[i], mdb_tool_axinfo[i % mdb_tool_threads] );
1384 tavl_free(mdb->mi_attrs[i]->ai_root, NULL);
1385 mdb->mi_attrs[i]->ai_root = NULL;
1392 int mdb_tool_idl_add(
1395 struct berval *keys,
1399 mdb_tool_idl_cache *ic, itmp;
1400 mdb_tool_idl_cache_entry *ice;
1402 AttrIxInfo *ax = (AttrIxInfo *)mc;
1403 AttrInfo *ai = (AttrInfo *)ax->ai_ai;
1407 for (i=0; keys[i].bv_val; i++) {
1408 itmp.kstr = keys[i];
1409 ic = tavl_find( ai->ai_root, &itmp, mdb_tool_idl_cmp );
1411 /* No entry yet, create one */
1417 if ( ax->ai_clist ) {
1419 ax->ai_clist = ic->head;
1421 ic = ch_malloc( sizeof( mdb_tool_idl_cache ) + itmp.kstr.bv_len + 4 );
1423 ic->kstr.bv_len = itmp.kstr.bv_len;
1424 ic->kstr.bv_val = (char *)(ic+1);
1425 memcpy( ic->kstr.bv_val, itmp.kstr.bv_val, ic->kstr.bv_len );
1426 ic->head = ic->tail = NULL;
1431 tavl_insert( &ai->ai_root, ic, mdb_tool_idl_cmp,
1434 /* load existing key count here */
1435 key.mv_size = keys[i].bv_len;
1436 key.mv_data = keys[i].bv_val;
1437 rc = mdb_cursor_get( mc, &key, &data, MDB_SET );
1439 ic->flags |= WAS_FOUND;
1440 nid = *(ID *)data.mv_data;
1442 ic->count = MDB_IDL_DB_SIZE+1;
1443 ic->flags |= WAS_RANGE;
1447 mdb_cursor_count( mc, &count );
1450 ic->offset = count & (IDBLOCK-1);
1454 /* are we a range already? */
1455 if ( ic->count > MDB_IDL_DB_SIZE ) {
1458 /* Are we at the limit, and converting to a range? */
1459 } else if ( ic->count == MDB_IDL_DB_SIZE ) {
1461 ic->tail->next = ax->ai_flist;
1462 ax->ai_flist = ic->head;
1464 ic->head = ic->tail = NULL;
1469 /* No free block, create that too */
1470 lcount = (ic->count-ic->offset) & (IDBLOCK-1);
1471 if ( !ic->tail || lcount == 0) {
1472 if ( ax->ai_flist ) {
1474 ax->ai_flist = ice->next;
1476 ice = ch_malloc( sizeof( mdb_tool_idl_cache_entry ));
1482 ic->tail->next = ice;
1486 ice->ids[lcount-1] = 0;
1491 if (!lcount || ice->ids[lcount-1] != id) {
1492 ice->ids[lcount] = id;
1499 #endif /* MDB_TOOL_IDL_CACHING */
1501 /* Upgrade from pre 2.4.34 dn2id format */
1503 #include <ac/unistd.h>
1504 #include <lutil_meter.h>
1506 #define STACKSIZ 2048
1508 typedef struct rec {
1515 mdb_dn2id_upgrade( BackendDB *be ) {
1516 struct mdb_info *mi = (struct mdb_info *) be->be_private;
1518 MDB_cursor *mc = NULL;
1521 int rc, writes=0, depth=0;
1522 int enable_meter = 0;
1523 ID id = 0, *num, count = 0;
1525 lutil_meter_t meter;
1527 if (!(mi->mi_flags & MDB_NEED_UPGRADE)) {
1528 Debug( LDAP_DEBUG_ANY, "database %s: No upgrade needed.\n",
1529 be->be_suffix[0].bv_val, 0, 0 );
1536 mdb_stat(mdb_cursor_txn(cursor), mi->mi_dbis[MDB_ID2ENTRY], &st);
1537 if (!st.ms_entries) {
1538 /* Empty DB, nothing to upgrade? */
1542 enable_meter = !lutil_meter_open(&meter,
1543 &lutil_meter_text_display,
1544 &lutil_meter_linear_estimator,
1548 num = ch_malloc(STACKSIZ * (sizeof(ID) + sizeof(rec)));
1549 stack = (rec *)(num + STACKSIZ);
1551 rc = mdb_txn_begin(mi->mi_dbenv, NULL, 0, &mt);
1553 Debug(LDAP_DEBUG_ANY, "mdb_dn2id_upgrade: mdb_txn_begin failed, %s (%d)\n",
1554 mdb_strerror(rc), rc, 0 );
1557 rc = mdb_cursor_open(mt, mi->mi_dbis[MDB_DN2ID], &mc);
1559 Debug(LDAP_DEBUG_ANY, "mdb_dn2id_upgrade: mdb_cursor_open failed, %s (%d)\n",
1560 mdb_strerror(rc), rc, 0 );
1564 key.mv_size = sizeof(ID);
1565 /* post-order depth-first update */
1572 stack[depth].id = id;
1573 rc = mdb_cursor_get(mc, &key, &data, MDB_SET);
1575 Debug(LDAP_DEBUG_ANY, "mdb_dn2id_upgrade: mdb_cursor_get failed, %s (%d)\n",
1576 mdb_strerror(rc), rc, 0 );
1581 rc = mdb_cursor_count(mc, &dkids);
1583 Debug(LDAP_DEBUG_ANY, "mdb_dn2id_upgrade: mdb_cursor_count failed, %s (%d)\n",
1584 mdb_strerror(rc), rc, 0 );
1588 rc = mdb_cursor_get(mc, &key, &data, MDB_NEXT_DUP);
1590 ptr = (unsigned char *)data.mv_data + data.mv_size - sizeof(ID);
1591 memcpy(&id, ptr, sizeof(ID));
1593 memcpy(stack[depth].rdn, data.mv_data, data.mv_size);
1594 stack[depth].len = data.mv_size;
1599 /* pop: write updated count, advance to next node */
1601 /* update superior counts */
1603 num[depth-1] += num[depth];
1606 id = stack[depth-1].id;
1607 data.mv_data = stack[depth].rdn;
1608 data.mv_size = stack[depth].len;
1609 rc = mdb_cursor_get(mc, &key, &data, MDB_GET_BOTH);
1611 Debug(LDAP_DEBUG_ANY, "mdb_dn2id_upgrade: mdb_cursor_get(BOTH) failed, %s (%d)\n",
1612 mdb_strerror(rc), rc, 0 );
1615 data.mv_data = stack[depth].rdn;
1616 ptr = (unsigned char *)data.mv_data + data.mv_size;
1617 memcpy(ptr, &num[depth], sizeof(ID));
1618 data.mv_size += sizeof(ID);
1619 rc = mdb_cursor_del(mc, 0);
1621 Debug(LDAP_DEBUG_ANY, "mdb_dn2id_upgrade: mdb_cursor_del failed, %s (%d)\n",
1622 mdb_strerror(rc), rc, 0 );
1625 rc = mdb_cursor_put(mc, &key, &data, 0);
1627 Debug(LDAP_DEBUG_ANY, "mdb_dn2id_upgrade: mdb_cursor_put failed, %s (%d)\n",
1628 mdb_strerror(rc), rc, 0 );
1634 lutil_meter_update(&meter, count, 0);
1639 len = (ptr[0] & 0x7f) << 8 | ptr[1];
1640 printf("ID: %zu, %zu, %.*s\n", stack[depth].id, num[depth], len, ptr+2);
1644 if (writes == 1000) {
1645 mdb_cursor_close(mc);
1646 rc = mdb_txn_commit(mt);
1648 Debug(LDAP_DEBUG_ANY, "mdb_dn2id_upgrade: mdb_txn_commit failed, %s (%d)\n",
1649 mdb_strerror(rc), rc, 0 );
1652 rc = mdb_txn_begin(mi->mi_dbenv, NULL, 0, &mt);
1654 Debug(LDAP_DEBUG_ANY, "mdb_dn2id_upgrade: mdb_txn_begin(2) failed, %s (%d)\n",
1655 mdb_strerror(rc), rc, 0 );
1658 rc = mdb_cursor_open(mt, mi->mi_dbis[MDB_DN2ID], &mc);
1660 Debug(LDAP_DEBUG_ANY, "mdb_dn2id_upgrade: mdb_cursor_open(2) failed, %s (%d)\n",
1661 mdb_strerror(rc), rc, 0 );
1664 rc = mdb_cursor_get(mc, &key, &data, MDB_GET_BOTH);
1666 Debug(LDAP_DEBUG_ANY, "mdb_dn2id_upgrade: mdb_cursor_get(2) failed, %s (%d)\n",
1667 mdb_strerror(rc), rc, 0 );
1674 rc = mdb_cursor_get(mc, &key, &data, MDB_NEXT_DUP);
1684 mdb_cursor_close(mc);
1687 r2 = mdb_txn_commit(mt);
1689 Debug(LDAP_DEBUG_ANY, "mdb_dn2id_upgrade: mdb_txn_commit(2) failed, %s (%d)\n",
1690 mdb_strerror(r2), r2, 0 );
1697 lutil_meter_update(&meter, count, 1);
1698 lutil_meter_close(&meter);