1 /* tools.c - tools for slap tools */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 2011-2015 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 Operation *mdb_tool_ix_op;
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 struct mdb_info *mdb_tool_info;
82 static ldap_pvt_thread_mutex_t mdb_tool_index_mutex;
83 static ldap_pvt_thread_cond_t mdb_tool_index_cond_main;
84 static ldap_pvt_thread_cond_t mdb_tool_index_cond_work;
85 static void * mdb_tool_index_task( void *ctx, void *ptr );
87 static int mdb_writes, mdb_writes_per_commit;
89 /* Number of ops per commit in Quick mode.
90 * Batching speeds writes overall, but too large a
91 * batch will fail with MDB_TXN_FULL.
93 #ifndef MDB_WRITES_PER_COMMIT
94 #define MDB_WRITES_PER_COMMIT 500
98 mdb_tool_entry_get_int( BackendDB *be, ID id, Entry **ep );
100 int mdb_tool_entry_open(
101 BackendDB *be, int mode )
103 /* In Quick mode, commit once per 500 entries */
105 if ( slapMode & SLAP_TOOL_QUICK )
106 mdb_writes_per_commit = MDB_WRITES_PER_COMMIT;
108 mdb_writes_per_commit = 1;
110 /* Set up for threaded slapindex */
111 if (( slapMode & (SLAP_TOOL_QUICK|SLAP_TOOL_READONLY)) == SLAP_TOOL_QUICK ) {
112 if ( !mdb_tool_info ) {
113 struct mdb_info *mdb = (struct mdb_info *) be->be_private;
114 ldap_pvt_thread_mutex_init( &mdb_tool_index_mutex );
115 ldap_pvt_thread_cond_init( &mdb_tool_index_cond_main );
116 ldap_pvt_thread_cond_init( &mdb_tool_index_cond_work );
117 if ( mdb->mi_nattrs ) {
119 #if 0 /* threaded indexing has no performance advantage */
120 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_index_tcount = mdb_tool_threads - 1;
125 for (i=1; i<mdb_tool_threads; i++) {
126 int *ptr = ch_malloc( sizeof( int ));
128 ldap_pvt_thread_pool_submit( &connection_pool,
129 mdb_tool_index_task, ptr );
140 int mdb_tool_entry_close(
143 if ( mdb_tool_info ) {
145 ldap_pvt_thread_mutex_lock( &mdb_tool_index_mutex );
147 /* There might still be some threads starting */
148 while ( mdb_tool_index_tcount > 0 ) {
149 ldap_pvt_thread_cond_wait( &mdb_tool_index_cond_main,
150 &mdb_tool_index_mutex );
153 mdb_tool_index_tcount = mdb_tool_threads - 1;
154 ldap_pvt_thread_cond_broadcast( &mdb_tool_index_cond_work );
156 /* Make sure all threads are stopped */
157 while ( mdb_tool_index_tcount > 0 ) {
158 ldap_pvt_thread_cond_wait( &mdb_tool_index_cond_main,
159 &mdb_tool_index_mutex );
161 ldap_pvt_thread_mutex_unlock( &mdb_tool_index_mutex );
163 mdb_tool_info = NULL;
165 ch_free( mdb_tool_index_rec );
166 mdb_tool_index_tcount = mdb_tool_threads - 1;
170 mdb_cursor_close( idcursor );
174 mdb_cursor_close( cursor );
179 MDB_TOOL_IDL_FLUSH( be, mdb_tool_txn );
180 if (( rc = mdb_txn_commit( mdb_tool_txn ))) {
181 Debug( LDAP_DEBUG_ANY,
182 LDAP_XSTRING(mdb_tool_entry_close) ": database %s: "
183 "txn_commit failed: %s (%d)\n",
184 be->be_suffix[0].bv_val, mdb_strerror(rc), rc );
192 fprintf( stderr, "Error, entries missing!\n");
193 for (i=0; i<nholes; i++) {
194 fprintf(stderr, " entry %ld: %s\n",
195 holes[i].id, holes[i].dn.bv_val);
205 mdb_tool_entry_first_x(
215 return mdb_tool_entry_next( be );
218 ID mdb_tool_entry_next(
223 struct mdb_info *mdb;
225 assert( be != NULL );
226 assert( slapMode & SLAP_TOOL_MODE );
228 mdb = (struct mdb_info *) be->be_private;
229 assert( mdb != NULL );
231 if ( !mdb_tool_txn ) {
232 rc = mdb_txn_begin( mdb->mi_dbenv, NULL, MDB_RDONLY, &mdb_tool_txn );
235 rc = mdb_cursor_open( mdb_tool_txn, mdb->mi_id2entry, &cursor );
237 mdb_txn_abort( mdb_tool_txn );
243 rc = mdb_cursor_get( cursor, &key, &data, MDB_NEXT );
249 previd = *(ID *)key.mv_data;
255 if ( tool_filter || tool_base ) {
256 static Operation op = {0};
257 static Opheader ohdr = {0};
261 op.o_tmpmemctx = NULL;
262 op.o_tmpmfuncs = &ch_mfuncs;
264 if ( tool_next_entry ) {
265 mdb_entry_release( &op, tool_next_entry, 0 );
266 tool_next_entry = NULL;
269 rc = mdb_tool_entry_get_int( be, id, &tool_next_entry );
270 if ( rc == LDAP_NO_SUCH_OBJECT ) {
274 assert( tool_next_entry != NULL );
276 if ( tool_filter && test_filter( NULL, tool_next_entry, tool_filter ) != LDAP_COMPARE_TRUE )
278 mdb_entry_release( &op, tool_next_entry, 0 );
279 tool_next_entry = NULL;
287 ID mdb_tool_dn2id_get(
292 struct mdb_info *mdb;
298 if ( BER_BVISEMPTY(dn) )
301 mdb = (struct mdb_info *) be->be_private;
303 if ( !mdb_tool_txn ) {
304 rc = mdb_txn_begin( mdb->mi_dbenv, NULL, (slapMode & SLAP_TOOL_READONLY) != 0 ?
305 MDB_RDONLY : 0, &mdb_tool_txn );
312 op.o_tmpmemctx = NULL;
313 op.o_tmpmfuncs = &ch_mfuncs;
315 rc = mdb_dn2id( &op, mdb_tool_txn, NULL, dn, &id, NULL, NULL, NULL );
316 if ( rc == MDB_NOTFOUND )
323 mdb_tool_entry_get_int( BackendDB *be, ID id, Entry **ep )
329 struct berval dn = BER_BVNULL, ndn = BER_BVNULL;
332 assert( be != NULL );
333 assert( slapMode & SLAP_TOOL_MODE );
335 if ( ( tool_filter || tool_base ) && id == previd && tool_next_entry != NULL ) {
336 *ep = tool_next_entry;
337 tool_next_entry = NULL;
341 if ( id != previd ) {
342 key.mv_size = sizeof(ID);
344 rc = mdb_cursor_get( cursor, &key, &data, MDB_SET );
350 if ( !data.mv_size ) {
351 rc = LDAP_NO_SUCH_OBJECT;
357 op.o_tmpmemctx = NULL;
358 op.o_tmpmfuncs = &ch_mfuncs;
359 if ( slapMode & SLAP_TOOL_READONLY ) {
360 rc = mdb_id2name( &op, mdb_tool_txn, &idcursor, id, &dn, &ndn );
365 if ( tool_base != NULL ) {
366 if ( !dnIsSuffixScope( &ndn, tool_base, tool_scope ) ) {
367 ch_free( dn.bv_val );
368 ch_free( ndn.bv_val );
369 rc = LDAP_NO_SUCH_OBJECT;
374 rc = mdb_entry_decode( &op, mdb_tool_txn, &data, &e );
376 if ( !BER_BVISNULL( &dn )) {
380 e->e_name.bv_val = NULL;
381 e->e_nname.bv_val = NULL;
393 mdb_tool_entry_get( BackendDB *be, ID id )
398 if ( !mdb_tool_txn ) {
399 struct mdb_info *mdb = (struct mdb_info *) be->be_private;
400 rc = mdb_txn_begin( mdb->mi_dbenv, NULL,
401 (slapMode & SLAP_TOOL_READONLY) ? MDB_RDONLY : 0, &mdb_tool_txn );
406 struct mdb_info *mdb = (struct mdb_info *) be->be_private;
407 rc = mdb_cursor_open( mdb_tool_txn, mdb->mi_id2entry, &cursor );
409 mdb_txn_abort( mdb_tool_txn );
414 (void)mdb_tool_entry_get_int( be, id, &e );
418 static int mdb_tool_next_id(
425 struct mdb_info *mdb = (struct mdb_info *) op->o_bd->be_private;
426 struct berval dn = e->e_name;
427 struct berval ndn = e->e_nname;
428 struct berval pdn, npdn, nmatched;
432 if (ndn.bv_len == 0) {
437 rc = mdb_dn2id( op, tid, mcp, &ndn, &id, NULL, NULL, &nmatched );
438 if ( rc == MDB_NOTFOUND ) {
439 if ( !be_issuffix( op->o_bd, &ndn ) ) {
441 dnParent( &ndn, &npdn );
442 if ( nmatched.bv_len != npdn.bv_len ) {
443 dnParent( &dn, &pdn );
446 rc = mdb_tool_next_id( op, tid, e, text, 1 );
452 /* If parent didn't exist, it was created just now
453 * and its ID is now in e->e_id. Make sure the current
454 * entry gets added under the new parent ID.
456 if ( eid != e->e_id ) {
463 rc = mdb_next_id( op->o_bd, idcursor, &e->e_id );
465 snprintf( text->bv_val, text->bv_len,
466 "next_id failed: %s (%d)",
467 mdb_strerror(rc), rc );
468 Debug( LDAP_DEBUG_ANY,
469 "=> mdb_tool_next_id: %s\n", text->bv_val, 0, 0 );
472 rc = mdb_dn2id_add( op, mcp, mcd, pid, 1, 1, e );
474 snprintf( text->bv_val, text->bv_len,
475 "dn2id_add failed: %s (%d)",
476 mdb_strerror(rc), rc );
477 Debug( LDAP_DEBUG_ANY,
478 "=> mdb_tool_next_id: %s\n", text->bv_val, 0, 0 );
481 if ( nholes == nhmax - 1 ) {
482 if ( holes == hbuf ) {
483 holes = ch_malloc( nhmax * sizeof(dn_id) * 2 );
484 AC_MEMCPY( holes, hbuf, sizeof(hbuf) );
486 holes = ch_realloc( holes, nhmax * sizeof(dn_id) * 2 );
490 ber_dupbv( &holes[nholes].dn, &ndn );
491 holes[nholes++].id = e->e_id;
492 key.mv_size = sizeof(ID);
493 key.mv_data = &e->e_id;
496 rc = mdb_cursor_put( idcursor, &key, &data, MDB_NOOVERWRITE );
497 if ( rc == MDB_KEYEXIST )
500 snprintf( text->bv_val, text->bv_len,
501 "dummy id2entry add failed: %s (%d)",
502 mdb_strerror(rc), rc );
503 Debug( LDAP_DEBUG_ANY,
504 "=> mdb_tool_next_id: %s\n", text->bv_val, 0, 0 );
507 } else if ( !hole ) {
512 for ( i=0; i<nholes; i++) {
513 if ( holes[i].id == e->e_id ) {
514 free(holes[i].dn.bv_val);
515 for (j=i;j<nholes;j++) holes[j] = holes[j+1];
519 } else if ( holes[i].id > e->e_id ) {
533 struct mdb_info *mdb = (struct mdb_info *) op->o_bd->be_private;
535 if ( !mdb->mi_nattrs )
538 if ( mdb_tool_threads > 1 ) {
543 ir = mdb_tool_index_rec;
544 for (i=0; i<mdb->mi_nattrs; i++)
545 ir[i].ir_attrs = NULL;
547 for ( a = e->e_attrs; a != NULL; a = a->a_next ) {
548 rc = mdb_index_recset( mdb, a, a->a_desc->ad_type,
549 &a->a_desc->ad_tags, ir );
553 for (i=0; i<mdb->mi_nattrs; i++) {
556 rc = mdb_cursor_open( txn, ir[i].ir_ai->ai_dbi,
557 &ir[i].ir_ai->ai_cursor );
561 mdb_tool_ix_id = e->e_id;
563 mdb_tool_ix_txn = txn;
564 ldap_pvt_thread_mutex_lock( &mdb_tool_index_mutex );
565 /* Wait for all threads to be ready */
566 while ( mdb_tool_index_tcount ) {
567 ldap_pvt_thread_cond_wait( &mdb_tool_index_cond_main,
568 &mdb_tool_index_mutex );
571 for ( i=1; i<mdb_tool_threads; i++ )
572 mdb_tool_index_rec[i].ir_i = LDAP_BUSY;
573 mdb_tool_index_tcount = mdb_tool_threads - 1;
574 ldap_pvt_thread_mutex_unlock( &mdb_tool_index_mutex );
575 ldap_pvt_thread_cond_broadcast( &mdb_tool_index_cond_work );
577 rc = mdb_index_recrun( op, txn, mdb, ir, e->e_id, 0 );
580 ldap_pvt_thread_mutex_lock( &mdb_tool_index_mutex );
581 for ( i=1; i<mdb_tool_threads; i++ ) {
582 if ( mdb_tool_index_rec[i].ir_i == LDAP_BUSY ) {
583 ldap_pvt_thread_cond_wait( &mdb_tool_index_cond_main,
584 &mdb_tool_index_mutex );
588 if ( mdb_tool_index_rec[i].ir_i ) {
589 rc = mdb_tool_index_rec[i].ir_i;
593 ldap_pvt_thread_mutex_unlock( &mdb_tool_index_mutex );
597 return mdb_index_entry_add( op, txn, e );
601 ID mdb_tool_entry_put(
604 struct berval *text )
607 struct mdb_info *mdb;
611 assert( be != NULL );
612 assert( slapMode & SLAP_TOOL_MODE );
614 assert( text != NULL );
615 assert( text->bv_val != NULL );
616 assert( text->bv_val[0] == '\0' ); /* overconservative? */
618 Debug( LDAP_DEBUG_TRACE, "=> " LDAP_XSTRING(mdb_tool_entry_put)
619 "( %ld, \"%s\" )\n", (long) e->e_id, e->e_dn, 0 );
621 mdb = (struct mdb_info *) be->be_private;
623 if ( !mdb_tool_txn ) {
624 rc = mdb_txn_begin( mdb->mi_dbenv, NULL, 0, &mdb_tool_txn );
626 snprintf( text->bv_val, text->bv_len,
627 "txn_begin failed: %s (%d)",
628 mdb_strerror(rc), rc );
629 Debug( LDAP_DEBUG_ANY,
630 "=> " LDAP_XSTRING(mdb_tool_entry_put) ": %s\n",
631 text->bv_val, 0, 0 );
634 rc = mdb_cursor_open( mdb_tool_txn, mdb->mi_id2entry, &idcursor );
636 snprintf( text->bv_val, text->bv_len,
637 "cursor_open failed: %s (%d)",
638 mdb_strerror(rc), rc );
639 Debug( LDAP_DEBUG_ANY,
640 "=> " LDAP_XSTRING(mdb_tool_entry_put) ": %s\n",
641 text->bv_val, 0, 0 );
644 if ( !mdb->mi_nextid ) {
646 mdb_next_id( be, idcursor, &dummy );
648 rc = mdb_cursor_open( mdb_tool_txn, mdb->mi_dn2id, &mcp );
650 snprintf( text->bv_val, text->bv_len,
651 "cursor_open failed: %s (%d)",
652 mdb_strerror(rc), rc );
653 Debug( LDAP_DEBUG_ANY,
654 "=> " LDAP_XSTRING(mdb_tool_entry_put) ": %s\n",
655 text->bv_val, 0, 0 );
658 rc = mdb_cursor_open( mdb_tool_txn, mdb->mi_dn2id, &mcd );
660 snprintf( text->bv_val, text->bv_len,
661 "cursor_open 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 );
672 op.o_tmpmemctx = NULL;
673 op.o_tmpmfuncs = &ch_mfuncs;
675 /* add dn2id indices */
676 rc = mdb_tool_next_id( &op, mdb_tool_txn, e, text, 0 );
681 rc = mdb_tool_index_add( &op, mdb_tool_txn, e );
683 snprintf( text->bv_val, text->bv_len,
684 "index_entry_add failed: err=%d", rc );
685 Debug( LDAP_DEBUG_ANY,
686 "=> " LDAP_XSTRING(mdb_tool_entry_put) ": %s\n",
687 text->bv_val, 0, 0 );
693 rc = mdb_id2entry_add( &op, mdb_tool_txn, idcursor, e );
695 snprintf( text->bv_val, text->bv_len,
696 "id2entry_add failed: err=%d", rc );
697 Debug( LDAP_DEBUG_ANY,
698 "=> " LDAP_XSTRING(mdb_tool_entry_put) ": %s\n",
699 text->bv_val, 0, 0 );
706 if ( mdb_writes >= mdb_writes_per_commit ) {
708 MDB_TOOL_IDL_FLUSH( be, mdb_tool_txn );
709 rc = mdb_txn_commit( mdb_tool_txn );
710 for ( i=0; i<mdb->mi_nattrs; i++ )
711 mdb->mi_attrs[i]->ai_cursor = NULL;
716 snprintf( text->bv_val, text->bv_len,
717 "txn_commit failed: %s (%d)",
718 mdb_strerror(rc), rc );
719 Debug( LDAP_DEBUG_ANY,
720 "=> " LDAP_XSTRING(mdb_tool_entry_put) ": %s\n",
721 text->bv_val, 0, 0 );
728 mdb_txn_abort( mdb_tool_txn );
731 for ( i=0; i<mdb->mi_nattrs; i++ )
732 mdb->mi_attrs[i]->ai_cursor = NULL;
734 snprintf( text->bv_val, text->bv_len,
735 "txn_aborted! %s (%d)",
736 rc == LDAP_OTHER ? "Internal error" :
737 mdb_strerror(rc), rc );
738 Debug( LDAP_DEBUG_ANY,
739 "=> " LDAP_XSTRING(mdb_tool_entry_put) ": %s\n",
740 text->bv_val, 0, 0 );
747 static int mdb_dn2id_upgrade( BackendDB *be );
749 int mdb_tool_entry_reindex(
752 AttributeDescription **adv )
754 struct mdb_info *mi = (struct mdb_info *) be->be_private;
760 Debug( LDAP_DEBUG_ARGS,
761 "=> " LDAP_XSTRING(mdb_tool_entry_reindex) "( %ld )\n",
763 assert( tool_base == NULL );
764 assert( tool_filter == NULL );
766 /* Special: do a dn2id upgrade */
767 if ( adv && adv[0] == slap_schema.si_ad_entryDN ) {
768 /* short-circuit tool_entry_next() */
769 mdb_cursor_get( cursor, &key, &data, MDB_LAST );
770 return mdb_dn2id_upgrade( be );
773 /* No indexes configured, nothing to do. Could return an
774 * error here to shortcut things.
780 /* Check for explicit list of attrs to index */
784 if ( mi->mi_attrs[0]->ai_desc != adv[0] ) {
786 for ( n = 0; adv[n]; n++ ) ;
789 for ( i = 0; i < n; i++ ) {
790 AttributeDescription *ad = adv[i];
791 for ( j = i-1; j>=0; j--) {
792 if ( SLAP_PTRCMP( adv[j], ad ) <= 0 ) break;
799 for ( i = 0; adv[i]; i++ ) {
800 if ( mi->mi_attrs[i]->ai_desc != adv[i] ) {
801 for ( j = i+1; j < mi->mi_nattrs; j++ ) {
802 if ( mi->mi_attrs[j]->ai_desc == adv[i] ) {
803 AttrInfo *ai = mi->mi_attrs[i];
804 mi->mi_attrs[i] = mi->mi_attrs[j];
805 mi->mi_attrs[j] = ai;
809 if ( j == mi->mi_nattrs ) {
810 Debug( LDAP_DEBUG_ANY,
811 LDAP_XSTRING(mdb_tool_entry_reindex)
812 ": no index configured for %s\n",
813 adv[i]->ad_cname.bv_val, 0, 0 );
821 e = mdb_tool_entry_get( be, id );
824 Debug( LDAP_DEBUG_ANY,
825 LDAP_XSTRING(mdb_tool_entry_reindex)
826 ": could not locate id=%ld\n",
832 rc = mdb_txn_begin( mi->mi_dbenv, NULL, 0, &txi );
834 Debug( LDAP_DEBUG_ANY,
835 "=> " LDAP_XSTRING(mdb_tool_entry_reindex) ": "
836 "txn_begin failed: %s (%d)\n",
837 mdb_strerror(rc), rc, 0 );
842 if ( slapMode & SLAP_TRUNCATE_MODE ) {
844 for ( i=0; i < mi->mi_nattrs; i++ ) {
845 rc = mdb_drop( txi, mi->mi_attrs[i]->ai_dbi, 0 );
847 Debug( LDAP_DEBUG_ANY,
848 LDAP_XSTRING(mdb_tool_entry_reindex)
849 ": (Truncate) mdb_drop(%s) failed: %s (%d)\n",
850 mi->mi_attrs[i]->ai_desc->ad_type->sat_cname.bv_val,
851 mdb_strerror(rc), rc );
855 slapMode ^= SLAP_TRUNCATE_MODE;
859 * just (re)add them for now
860 * Use truncate mode to empty/reset index databases
863 Debug( LDAP_DEBUG_TRACE,
864 "=> " LDAP_XSTRING(mdb_tool_entry_reindex) "( %ld )\n",
869 op.o_tmpmemctx = NULL;
870 op.o_tmpmfuncs = &ch_mfuncs;
872 rc = mdb_tool_index_add( &op, txi, e );
877 if ( mdb_writes >= mdb_writes_per_commit ) {
880 MDB_TOOL_IDL_FLUSH( be, txi );
881 rc = mdb_txn_commit( txi );
883 for ( i=0; i<mi->mi_nattrs; i++ )
884 mi->mi_attrs[i]->ai_cursor = NULL;
886 Debug( LDAP_DEBUG_ANY,
887 "=> " LDAP_XSTRING(mdb_tool_entry_reindex)
888 ": txn_commit failed: %s (%d)\n",
889 mdb_strerror(rc), rc, 0 );
892 mdb_cursor_close( cursor );
894 /* Must close the read txn to allow old pages to be reclaimed. */
895 mdb_txn_abort( mdb_tool_txn );
896 /* and then reopen it so that tool_entry_next still works. */
897 mdb_txn_begin( mi->mi_dbenv, NULL, MDB_RDONLY, &mdb_tool_txn );
898 mdb_cursor_open( mdb_tool_txn, mi->mi_id2entry, &cursor );
900 key.mv_size = sizeof(ID);
901 mdb_cursor_get( cursor, &key, NULL, MDB_SET );
907 mdb_cursor_close( cursor );
909 mdb_txn_abort( txi );
910 for ( i=0; i<mi->mi_nattrs; i++ )
911 mi->mi_attrs[i]->ai_cursor = NULL;
912 Debug( LDAP_DEBUG_ANY,
913 "=> " LDAP_XSTRING(mdb_tool_entry_reindex)
914 ": txn_aborted! err=%d\n",
919 mdb_entry_release( &op, e, 0 );
924 ID mdb_tool_entry_modify(
927 struct berval *text )
930 struct mdb_info *mdb;
934 assert( be != NULL );
935 assert( slapMode & SLAP_TOOL_MODE );
937 assert( text != NULL );
938 assert( text->bv_val != NULL );
939 assert( text->bv_val[0] == '\0' ); /* overconservative? */
941 assert ( e->e_id != NOID );
943 Debug( LDAP_DEBUG_TRACE,
944 "=> " LDAP_XSTRING(mdb_tool_entry_modify) "( %ld, \"%s\" )\n",
945 (long) e->e_id, e->e_dn, 0 );
947 mdb = (struct mdb_info *) be->be_private;
950 mdb_cursor_close( cursor );
953 if ( !mdb_tool_txn ) {
954 rc = mdb_txn_begin( mdb->mi_dbenv, NULL, 0, &mdb_tool_txn );
956 snprintf( text->bv_val, text->bv_len,
957 "txn_begin failed: %s (%d)",
958 mdb_strerror(rc), rc );
959 Debug( LDAP_DEBUG_ANY,
960 "=> " LDAP_XSTRING(mdb_tool_entry_modify) ": %s\n",
961 text->bv_val, 0, 0 );
968 op.o_tmpmemctx = NULL;
969 op.o_tmpmfuncs = &ch_mfuncs;
972 rc = mdb_id2entry_update( &op, mdb_tool_txn, NULL, e );
974 snprintf( text->bv_val, text->bv_len,
975 "id2entry_update failed: err=%d", rc );
976 Debug( LDAP_DEBUG_ANY,
977 "=> " LDAP_XSTRING(mdb_tool_entry_modify) ": %s\n",
978 text->bv_val, 0, 0 );
984 rc = mdb_txn_commit( mdb_tool_txn );
986 snprintf( text->bv_val, text->bv_len,
987 "txn_commit failed: %s (%d)",
988 mdb_strerror(rc), rc );
989 Debug( LDAP_DEBUG_ANY,
990 "=> " LDAP_XSTRING(mdb_tool_entry_modify) ": "
991 "%s\n", text->bv_val, 0, 0 );
996 mdb_txn_abort( mdb_tool_txn );
997 snprintf( text->bv_val, text->bv_len,
998 "txn_aborted! %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 );
1005 mdb_tool_txn = NULL;
1012 mdb_tool_index_task( void *ctx, void *ptr )
1014 int base = *(int *)ptr;
1018 ldap_pvt_thread_mutex_lock( &mdb_tool_index_mutex );
1019 mdb_tool_index_tcount--;
1020 if ( !mdb_tool_index_tcount )
1021 ldap_pvt_thread_cond_signal( &mdb_tool_index_cond_main );
1022 ldap_pvt_thread_cond_wait( &mdb_tool_index_cond_work,
1023 &mdb_tool_index_mutex );
1024 if ( slapd_shutdown ) {
1025 mdb_tool_index_tcount--;
1026 if ( !mdb_tool_index_tcount )
1027 ldap_pvt_thread_cond_signal( &mdb_tool_index_cond_main );
1028 ldap_pvt_thread_mutex_unlock( &mdb_tool_index_mutex );
1031 ldap_pvt_thread_mutex_unlock( &mdb_tool_index_mutex );
1032 mdb_tool_index_rec[base].ir_i = mdb_index_recrun( mdb_tool_ix_op,
1034 mdb_tool_info, mdb_tool_index_rec, mdb_tool_ix_id, base );
1040 #ifdef MDB_TOOL_IDL_CACHING
1042 mdb_tool_idl_cmp( const void *v1, const void *v2 )
1044 const mdb_tool_idl_cache *c1 = v1, *c2 = v2;
1047 if (( rc = c1->kstr.bv_len - c2->kstr.bv_len )) return rc;
1048 return memcmp( c1->kstr.bv_val, c2->kstr.bv_val, c1->kstr.bv_len );
1052 mdb_tool_idl_flush_one( MDB_cursor *mc, AttrInfo *ai, mdb_tool_idl_cache *ic )
1054 mdb_tool_idl_cache_entry *ice;
1055 MDB_val key, data[2];
1059 /* Freshly allocated, ignore it */
1060 if ( !ic->head && ic->count <= MDB_IDL_DB_SIZE ) {
1064 key.mv_data = ic->kstr.bv_val;
1065 key.mv_size = ic->kstr.bv_len;
1067 if ( ic->count > MDB_IDL_DB_SIZE ) {
1068 while ( ic->flags & WAS_FOUND ) {
1069 rc = mdb_cursor_get( mc, &key, data, MDB_SET );
1071 /* FIXME: find out why this happens */
1075 if ( ic->flags & WAS_RANGE ) {
1077 rc = mdb_cursor_get( mc, &key, data, MDB_NEXT_DUP );
1080 rc = mdb_cursor_get( mc, &key, data, MDB_NEXT_DUP );
1082 /* Store range hi */
1083 data[0].mv_data = &ic->last;
1084 rc = mdb_cursor_put( mc, &key, data, MDB_CURRENT );
1086 /* Delete old data, replace with range */
1087 ic->first = *(ID *)data[0].mv_data;
1088 mdb_cursor_del( mc, MDB_NODUPDATA );
1092 if ( !(ic->flags & WAS_RANGE)) {
1093 /* range, didn't exist before */
1095 data[0].mv_size = sizeof(ID);
1096 data[0].mv_data = &nid;
1097 rc = mdb_cursor_put( mc, &key, data, 0 );
1099 data[0].mv_data = &ic->first;
1100 rc = mdb_cursor_put( mc, &key, data, 0 );
1102 data[0].mv_data = &ic->last;
1103 rc = mdb_cursor_put( mc, &key, data, 0 );
1114 data[0].mv_size = sizeof(ID);
1117 for ( ice = ic->head, n=0; ice; ice = ice->next, n++ ) {
1122 end = ic->count & (IDBLOCK-1);
1126 data[1].mv_size = end - i;
1127 data[0].mv_data = &ice->ids[i];
1129 rc = mdb_cursor_put( mc, &key, data, MDB_NODUPDATA|MDB_APPEND|MDB_MULTIPLE );
1131 if ( rc == MDB_KEYEXIST ) {
1140 ic->tail->next = ai->ai_flist;
1141 ai->ai_flist = ic->head;
1144 ic->head = ai->ai_clist;
1150 mdb_tool_idl_flush_db( MDB_txn *txn, AttrInfo *ai )
1156 mdb_cursor_open( txn, ai->ai_dbi, &mc );
1157 root = tavl_end( ai->ai_root, TAVL_DIR_LEFT );
1159 rc = mdb_tool_idl_flush_one( mc, ai, root->avl_data );
1162 } while ((root = tavl_next(root, TAVL_DIR_RIGHT)));
1163 mdb_cursor_close( mc );
1169 mdb_tool_idl_flush( BackendDB *be, MDB_txn *txn )
1171 struct mdb_info *mdb = (struct mdb_info *) be->be_private;
1173 unsigned int i, dbi;
1175 for ( i=0; i < mdb->mi_nattrs; i++ ) {
1176 if ( !mdb->mi_attrs[i]->ai_root ) continue;
1177 rc = mdb_tool_idl_flush_db( txn, mdb->mi_attrs[i] );
1178 tavl_free(mdb->mi_attrs[i]->ai_root, NULL);
1179 mdb->mi_attrs[i]->ai_root = NULL;
1186 int mdb_tool_idl_add(
1189 struct berval *keys,
1193 mdb_tool_idl_cache *ic, itmp;
1194 mdb_tool_idl_cache_entry *ice;
1196 AttrInfo *ai = (AttrInfo *)mc;
1200 for (i=0; keys[i].bv_val; i++) {
1201 itmp.kstr = keys[i];
1202 ic = tavl_find( (Avlnode *)ai->ai_root, &itmp, mdb_tool_idl_cmp );
1204 /* No entry yet, create one */
1210 if ( ai->ai_clist ) {
1212 ai->ai_clist = ic->head;
1214 ic = ch_malloc( sizeof( mdb_tool_idl_cache ) + itmp.kstr.bv_len + 4 );
1216 ic->kstr.bv_len = itmp.kstr.bv_len;
1217 ic->kstr.bv_val = (char *)(ic+1);
1218 memcpy( ic->kstr.bv_val, itmp.kstr.bv_val, ic->kstr.bv_len );
1219 ic->head = ic->tail = NULL;
1224 tavl_insert( (Avlnode **)&ai->ai_root, ic, mdb_tool_idl_cmp,
1227 /* load existing key count here */
1228 key.mv_size = keys[i].bv_len;
1229 key.mv_data = keys[i].bv_val;
1230 rc = mdb_cursor_get( mc, &key, &data, MDB_SET );
1232 ic->flags |= WAS_FOUND;
1233 nid = *(ID *)data.mv_data;
1235 ic->count = MDB_IDL_DB_SIZE+1;
1236 ic->flags |= WAS_RANGE;
1240 mdb_cursor_count( mc, &count );
1243 ic->offset = count & (IDBLOCK-1);
1247 /* are we a range already? */
1248 if ( ic->count > MDB_IDL_DB_SIZE ) {
1251 /* Are we at the limit, and converting to a range? */
1252 } else if ( ic->count == MDB_IDL_DB_SIZE ) {
1254 ic->tail->next = ai->ai_flist;
1255 ai->ai_flist = ic->head;
1257 ic->head = ic->tail = NULL;
1262 /* No free block, create that too */
1263 lcount = ic->count & (IDBLOCK-1);
1264 if ( !ic->tail || lcount == 0) {
1265 if ( ai->ai_flist ) {
1267 ai->ai_flist = ice->next;
1269 ice = ch_malloc( sizeof( mdb_tool_idl_cache_entry ));
1275 ic->tail->next = ice;
1279 ice->ids[lcount-1] = 0;
1284 if (!lcount || ice->ids[lcount-1] != id)
1285 ice->ids[lcount] = id;
1291 #endif /* MDB_TOOL_IDL_CACHING */
1293 /* Upgrade from pre 2.4.34 dn2id format */
1295 #include <ac/unistd.h>
1296 #include <lutil_meter.h>
1298 #define STACKSIZ 2048
1300 typedef struct rec {
1307 mdb_dn2id_upgrade( BackendDB *be ) {
1308 struct mdb_info *mi = (struct mdb_info *) be->be_private;
1310 MDB_cursor *mc = NULL;
1313 int rc, writes=0, depth=0;
1314 int enable_meter = 0;
1315 ID id = 0, *num, count = 0;
1317 lutil_meter_t meter;
1319 if (!(mi->mi_flags & MDB_NEED_UPGRADE)) {
1320 Debug( LDAP_DEBUG_ANY, "database %s: No upgrade needed.\n",
1321 be->be_suffix[0].bv_val, 0, 0 );
1328 mdb_stat(mdb_cursor_txn(cursor), mi->mi_dbis[MDB_ID2ENTRY], &st);
1329 if (!st.ms_entries) {
1330 /* Empty DB, nothing to upgrade? */
1334 enable_meter = !lutil_meter_open(&meter,
1335 &lutil_meter_text_display,
1336 &lutil_meter_linear_estimator,
1340 num = ch_malloc(STACKSIZ * (sizeof(ID) + sizeof(rec)));
1341 stack = (rec *)(num + STACKSIZ);
1343 rc = mdb_txn_begin(mi->mi_dbenv, NULL, 0, &mt);
1345 Debug(LDAP_DEBUG_ANY, "mdb_dn2id_upgrade: mdb_txn_begin failed, %s (%d)\n",
1346 mdb_strerror(rc), rc, 0 );
1349 rc = mdb_cursor_open(mt, mi->mi_dbis[MDB_DN2ID], &mc);
1351 Debug(LDAP_DEBUG_ANY, "mdb_dn2id_upgrade: mdb_cursor_open failed, %s (%d)\n",
1352 mdb_strerror(rc), rc, 0 );
1356 key.mv_size = sizeof(ID);
1357 /* post-order depth-first update */
1364 stack[depth].id = id;
1365 rc = mdb_cursor_get(mc, &key, &data, MDB_SET);
1367 Debug(LDAP_DEBUG_ANY, "mdb_dn2id_upgrade: mdb_cursor_get failed, %s (%d)\n",
1368 mdb_strerror(rc), rc, 0 );
1373 rc = mdb_cursor_count(mc, &dkids);
1375 Debug(LDAP_DEBUG_ANY, "mdb_dn2id_upgrade: mdb_cursor_count failed, %s (%d)\n",
1376 mdb_strerror(rc), rc, 0 );
1380 rc = mdb_cursor_get(mc, &key, &data, MDB_NEXT_DUP);
1382 ptr = (unsigned char *)data.mv_data + data.mv_size - sizeof(ID);
1383 memcpy(&id, ptr, sizeof(ID));
1385 memcpy(stack[depth].rdn, data.mv_data, data.mv_size);
1386 stack[depth].len = data.mv_size;
1391 /* pop: write updated count, advance to next node */
1393 /* update superior counts */
1395 num[depth-1] += num[depth];
1398 id = stack[depth-1].id;
1399 data.mv_data = stack[depth].rdn;
1400 data.mv_size = stack[depth].len;
1401 rc = mdb_cursor_get(mc, &key, &data, MDB_GET_BOTH);
1403 Debug(LDAP_DEBUG_ANY, "mdb_dn2id_upgrade: mdb_cursor_get(BOTH) failed, %s (%d)\n",
1404 mdb_strerror(rc), rc, 0 );
1407 data.mv_data = stack[depth].rdn;
1408 ptr = (unsigned char *)data.mv_data + data.mv_size;
1409 memcpy(ptr, &num[depth], sizeof(ID));
1410 data.mv_size += sizeof(ID);
1411 rc = mdb_cursor_del(mc, 0);
1413 Debug(LDAP_DEBUG_ANY, "mdb_dn2id_upgrade: mdb_cursor_del failed, %s (%d)\n",
1414 mdb_strerror(rc), rc, 0 );
1417 rc = mdb_cursor_put(mc, &key, &data, 0);
1419 Debug(LDAP_DEBUG_ANY, "mdb_dn2id_upgrade: mdb_cursor_put failed, %s (%d)\n",
1420 mdb_strerror(rc), rc, 0 );
1426 lutil_meter_update(&meter, count, 0);
1431 len = (ptr[0] & 0x7f) << 8 | ptr[1];
1432 printf("ID: %zu, %zu, %.*s\n", stack[depth].id, num[depth], len, ptr+2);
1436 if (writes == 1000) {
1437 mdb_cursor_close(mc);
1438 rc = mdb_txn_commit(mt);
1440 Debug(LDAP_DEBUG_ANY, "mdb_dn2id_upgrade: mdb_txn_commit failed, %s (%d)\n",
1441 mdb_strerror(rc), rc, 0 );
1444 rc = mdb_txn_begin(mi->mi_dbenv, NULL, 0, &mt);
1446 Debug(LDAP_DEBUG_ANY, "mdb_dn2id_upgrade: mdb_txn_begin(2) failed, %s (%d)\n",
1447 mdb_strerror(rc), rc, 0 );
1450 rc = mdb_cursor_open(mt, mi->mi_dbis[MDB_DN2ID], &mc);
1452 Debug(LDAP_DEBUG_ANY, "mdb_dn2id_upgrade: mdb_cursor_open(2) failed, %s (%d)\n",
1453 mdb_strerror(rc), rc, 0 );
1456 rc = mdb_cursor_get(mc, &key, &data, MDB_GET_BOTH);
1458 Debug(LDAP_DEBUG_ANY, "mdb_dn2id_upgrade: mdb_cursor_get(2) failed, %s (%d)\n",
1459 mdb_strerror(rc), rc, 0 );
1466 rc = mdb_cursor_get(mc, &key, &data, MDB_NEXT_DUP);
1476 mdb_cursor_close(mc);
1479 r2 = mdb_txn_commit(mt);
1481 Debug(LDAP_DEBUG_ANY, "mdb_dn2id_upgrade: mdb_txn_commit(2) failed, %s (%d)\n",
1482 mdb_strerror(r2), r2, 0 );
1489 lutil_meter_update(&meter, count, 1);
1490 lutil_meter_close(&meter);