1 /* tools.c - tools for slap tools */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 2011-2014 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 );
364 mdb_entry_return( &op, e );
369 if ( tool_base != NULL ) {
370 if ( !dnIsSuffixScope( &ndn, tool_base, tool_scope ) ) {
371 ch_free( dn.bv_val );
372 ch_free( ndn.bv_val );
373 rc = LDAP_NO_SUCH_OBJECT;
378 rc = mdb_entry_decode( &op, mdb_tool_txn, &data, &e );
380 if ( !BER_BVISNULL( &dn )) {
384 e->e_name.bv_val = NULL;
385 e->e_nname.bv_val = NULL;
397 mdb_tool_entry_get( BackendDB *be, ID id )
402 if ( !mdb_tool_txn ) {
403 struct mdb_info *mdb = (struct mdb_info *) be->be_private;
404 rc = mdb_txn_begin( mdb->mi_dbenv, NULL,
405 (slapMode & SLAP_TOOL_READONLY) ? MDB_RDONLY : 0, &mdb_tool_txn );
410 struct mdb_info *mdb = (struct mdb_info *) be->be_private;
411 rc = mdb_cursor_open( mdb_tool_txn, mdb->mi_id2entry, &cursor );
413 mdb_txn_abort( mdb_tool_txn );
418 (void)mdb_tool_entry_get_int( be, id, &e );
422 static int mdb_tool_next_id(
429 struct mdb_info *mdb = (struct mdb_info *) op->o_bd->be_private;
430 struct berval dn = e->e_name;
431 struct berval ndn = e->e_nname;
432 struct berval pdn, npdn, nmatched;
436 if (ndn.bv_len == 0) {
441 rc = mdb_dn2id( op, tid, mcp, &ndn, &id, NULL, NULL, &nmatched );
442 if ( rc == MDB_NOTFOUND ) {
443 if ( !be_issuffix( op->o_bd, &ndn ) ) {
445 dnParent( &ndn, &npdn );
446 if ( nmatched.bv_len != npdn.bv_len ) {
447 dnParent( &dn, &pdn );
450 rc = mdb_tool_next_id( op, tid, e, text, 1 );
456 /* If parent didn't exist, it was created just now
457 * and its ID is now in e->e_id. Make sure the current
458 * entry gets added under the new parent ID.
460 if ( eid != e->e_id ) {
467 rc = mdb_next_id( op->o_bd, idcursor, &e->e_id );
469 snprintf( text->bv_val, text->bv_len,
470 "next_id failed: %s (%d)",
471 mdb_strerror(rc), rc );
472 Debug( LDAP_DEBUG_ANY,
473 "=> mdb_tool_next_id: %s\n", text->bv_val, 0, 0 );
476 rc = mdb_dn2id_add( op, mcp, mcd, pid, 1, 1, e );
478 snprintf( text->bv_val, text->bv_len,
479 "dn2id_add failed: %s (%d)",
480 mdb_strerror(rc), rc );
481 Debug( LDAP_DEBUG_ANY,
482 "=> mdb_tool_next_id: %s\n", text->bv_val, 0, 0 );
485 if ( nholes == nhmax - 1 ) {
486 if ( holes == hbuf ) {
487 holes = ch_malloc( nhmax * sizeof(dn_id) * 2 );
488 AC_MEMCPY( holes, hbuf, sizeof(hbuf) );
490 holes = ch_realloc( holes, nhmax * sizeof(dn_id) * 2 );
494 ber_dupbv( &holes[nholes].dn, &ndn );
495 holes[nholes++].id = e->e_id;
496 key.mv_size = sizeof(ID);
497 key.mv_data = &e->e_id;
500 rc = mdb_cursor_put( idcursor, &key, &data, MDB_NOOVERWRITE );
501 if ( rc == MDB_KEYEXIST )
504 snprintf( text->bv_val, text->bv_len,
505 "dummy id2entry add failed: %s (%d)",
506 mdb_strerror(rc), rc );
507 Debug( LDAP_DEBUG_ANY,
508 "=> mdb_tool_next_id: %s\n", text->bv_val, 0, 0 );
511 } else if ( !hole ) {
516 for ( i=0; i<nholes; i++) {
517 if ( holes[i].id == e->e_id ) {
518 free(holes[i].dn.bv_val);
519 for (j=i;j<nholes;j++) holes[j] = holes[j+1];
523 } else if ( holes[i].id > e->e_id ) {
537 struct mdb_info *mdb = (struct mdb_info *) op->o_bd->be_private;
539 if ( !mdb->mi_nattrs )
542 if ( mdb_tool_threads > 1 ) {
547 ir = mdb_tool_index_rec;
548 for (i=0; i<mdb->mi_nattrs; i++)
549 ir[i].ir_attrs = NULL;
551 for ( a = e->e_attrs; a != NULL; a = a->a_next ) {
552 rc = mdb_index_recset( mdb, a, a->a_desc->ad_type,
553 &a->a_desc->ad_tags, ir );
557 for (i=0; i<mdb->mi_nattrs; i++) {
560 rc = mdb_cursor_open( txn, ir[i].ir_ai->ai_dbi,
561 &ir[i].ir_ai->ai_cursor );
565 mdb_tool_ix_id = e->e_id;
567 mdb_tool_ix_txn = txn;
568 ldap_pvt_thread_mutex_lock( &mdb_tool_index_mutex );
569 /* Wait for all threads to be ready */
570 while ( mdb_tool_index_tcount ) {
571 ldap_pvt_thread_cond_wait( &mdb_tool_index_cond_main,
572 &mdb_tool_index_mutex );
575 for ( i=1; i<mdb_tool_threads; i++ )
576 mdb_tool_index_rec[i].ir_i = LDAP_BUSY;
577 mdb_tool_index_tcount = mdb_tool_threads - 1;
578 ldap_pvt_thread_mutex_unlock( &mdb_tool_index_mutex );
579 ldap_pvt_thread_cond_broadcast( &mdb_tool_index_cond_work );
581 rc = mdb_index_recrun( op, txn, mdb, ir, e->e_id, 0 );
584 ldap_pvt_thread_mutex_lock( &mdb_tool_index_mutex );
585 for ( i=1; i<mdb_tool_threads; i++ ) {
586 if ( mdb_tool_index_rec[i].ir_i == LDAP_BUSY ) {
587 ldap_pvt_thread_cond_wait( &mdb_tool_index_cond_main,
588 &mdb_tool_index_mutex );
592 if ( mdb_tool_index_rec[i].ir_i ) {
593 rc = mdb_tool_index_rec[i].ir_i;
597 ldap_pvt_thread_mutex_unlock( &mdb_tool_index_mutex );
601 return mdb_index_entry_add( op, txn, e );
605 ID mdb_tool_entry_put(
608 struct berval *text )
611 struct mdb_info *mdb;
615 assert( be != NULL );
616 assert( slapMode & SLAP_TOOL_MODE );
618 assert( text != NULL );
619 assert( text->bv_val != NULL );
620 assert( text->bv_val[0] == '\0' ); /* overconservative? */
622 Debug( LDAP_DEBUG_TRACE, "=> " LDAP_XSTRING(mdb_tool_entry_put)
623 "( %ld, \"%s\" )\n", (long) e->e_id, e->e_dn, 0 );
625 mdb = (struct mdb_info *) be->be_private;
627 if ( !mdb_tool_txn ) {
628 rc = mdb_txn_begin( mdb->mi_dbenv, NULL, 0, &mdb_tool_txn );
630 snprintf( text->bv_val, text->bv_len,
631 "txn_begin failed: %s (%d)",
632 mdb_strerror(rc), rc );
633 Debug( LDAP_DEBUG_ANY,
634 "=> " LDAP_XSTRING(mdb_tool_entry_put) ": %s\n",
635 text->bv_val, 0, 0 );
638 rc = mdb_cursor_open( mdb_tool_txn, mdb->mi_id2entry, &idcursor );
640 snprintf( text->bv_val, text->bv_len,
641 "cursor_open failed: %s (%d)",
642 mdb_strerror(rc), rc );
643 Debug( LDAP_DEBUG_ANY,
644 "=> " LDAP_XSTRING(mdb_tool_entry_put) ": %s\n",
645 text->bv_val, 0, 0 );
648 if ( !mdb->mi_nextid ) {
650 mdb_next_id( be, idcursor, &dummy );
652 rc = mdb_cursor_open( mdb_tool_txn, mdb->mi_dn2id, &mcp );
654 snprintf( text->bv_val, text->bv_len,
655 "cursor_open failed: %s (%d)",
656 mdb_strerror(rc), rc );
657 Debug( LDAP_DEBUG_ANY,
658 "=> " LDAP_XSTRING(mdb_tool_entry_put) ": %s\n",
659 text->bv_val, 0, 0 );
662 rc = mdb_cursor_open( mdb_tool_txn, mdb->mi_dn2id, &mcd );
664 snprintf( text->bv_val, text->bv_len,
665 "cursor_open failed: %s (%d)",
666 mdb_strerror(rc), rc );
667 Debug( LDAP_DEBUG_ANY,
668 "=> " LDAP_XSTRING(mdb_tool_entry_put) ": %s\n",
669 text->bv_val, 0, 0 );
676 op.o_tmpmemctx = NULL;
677 op.o_tmpmfuncs = &ch_mfuncs;
679 /* add dn2id indices */
680 rc = mdb_tool_next_id( &op, mdb_tool_txn, e, text, 0 );
685 rc = mdb_tool_index_add( &op, mdb_tool_txn, e );
687 snprintf( text->bv_val, text->bv_len,
688 "index_entry_add failed: err=%d", rc );
689 Debug( LDAP_DEBUG_ANY,
690 "=> " LDAP_XSTRING(mdb_tool_entry_put) ": %s\n",
691 text->bv_val, 0, 0 );
697 rc = mdb_id2entry_add( &op, mdb_tool_txn, idcursor, e );
699 snprintf( text->bv_val, text->bv_len,
700 "id2entry_add failed: err=%d", rc );
701 Debug( LDAP_DEBUG_ANY,
702 "=> " LDAP_XSTRING(mdb_tool_entry_put) ": %s\n",
703 text->bv_val, 0, 0 );
710 if ( mdb_writes >= mdb_writes_per_commit ) {
712 MDB_TOOL_IDL_FLUSH( be, mdb_tool_txn );
713 rc = mdb_txn_commit( mdb_tool_txn );
714 for ( i=0; i<mdb->mi_nattrs; i++ )
715 mdb->mi_attrs[i]->ai_cursor = NULL;
720 snprintf( text->bv_val, text->bv_len,
721 "txn_commit failed: %s (%d)",
722 mdb_strerror(rc), rc );
723 Debug( LDAP_DEBUG_ANY,
724 "=> " LDAP_XSTRING(mdb_tool_entry_put) ": %s\n",
725 text->bv_val, 0, 0 );
732 mdb_txn_abort( mdb_tool_txn );
735 for ( i=0; i<mdb->mi_nattrs; i++ )
736 mdb->mi_attrs[i]->ai_cursor = NULL;
738 snprintf( text->bv_val, text->bv_len,
739 "txn_aborted! %s (%d)",
740 rc == LDAP_OTHER ? "Internal error" :
741 mdb_strerror(rc), rc );
742 Debug( LDAP_DEBUG_ANY,
743 "=> " LDAP_XSTRING(mdb_tool_entry_put) ": %s\n",
744 text->bv_val, 0, 0 );
751 static int mdb_dn2id_upgrade( BackendDB *be );
753 int mdb_tool_entry_reindex(
756 AttributeDescription **adv )
758 struct mdb_info *mi = (struct mdb_info *) be->be_private;
764 Debug( LDAP_DEBUG_ARGS,
765 "=> " LDAP_XSTRING(mdb_tool_entry_reindex) "( %ld )\n",
767 assert( tool_base == NULL );
768 assert( tool_filter == NULL );
770 /* Special: do a dn2id upgrade */
771 if ( adv && adv[0] == slap_schema.si_ad_entryDN ) {
772 /* short-circuit tool_entry_next() */
773 mdb_cursor_get( cursor, &key, &data, MDB_LAST );
774 return mdb_dn2id_upgrade( be );
777 /* No indexes configured, nothing to do. Could return an
778 * error here to shortcut things.
784 /* Check for explicit list of attrs to index */
788 if ( mi->mi_attrs[0]->ai_desc != adv[0] ) {
790 for ( n = 0; adv[n]; n++ ) ;
793 for ( i = 0; i < n; i++ ) {
794 AttributeDescription *ad = adv[i];
795 for ( j = i-1; j>=0; j--) {
796 if ( SLAP_PTRCMP( adv[j], ad ) <= 0 ) break;
803 for ( i = 0; adv[i]; i++ ) {
804 if ( mi->mi_attrs[i]->ai_desc != adv[i] ) {
805 for ( j = i+1; j < mi->mi_nattrs; j++ ) {
806 if ( mi->mi_attrs[j]->ai_desc == adv[i] ) {
807 AttrInfo *ai = mi->mi_attrs[i];
808 mi->mi_attrs[i] = mi->mi_attrs[j];
809 mi->mi_attrs[j] = ai;
813 if ( j == mi->mi_nattrs ) {
814 Debug( LDAP_DEBUG_ANY,
815 LDAP_XSTRING(mdb_tool_entry_reindex)
816 ": no index configured for %s\n",
817 adv[i]->ad_cname.bv_val, 0, 0 );
825 e = mdb_tool_entry_get( be, id );
828 Debug( LDAP_DEBUG_ANY,
829 LDAP_XSTRING(mdb_tool_entry_reindex)
830 ": could not locate id=%ld\n",
836 rc = mdb_txn_begin( mi->mi_dbenv, NULL, 0, &txi );
838 Debug( LDAP_DEBUG_ANY,
839 "=> " LDAP_XSTRING(mdb_tool_entry_reindex) ": "
840 "txn_begin failed: %s (%d)\n",
841 mdb_strerror(rc), rc, 0 );
846 if ( slapMode & SLAP_TRUNCATE_MODE ) {
848 for ( i=0; i < mi->mi_nattrs; i++ ) {
849 rc = mdb_drop( txi, mi->mi_attrs[i]->ai_dbi, 0 );
851 Debug( LDAP_DEBUG_ANY,
852 LDAP_XSTRING(mdb_tool_entry_reindex)
853 ": (Truncate) mdb_drop(%s) failed: %s (%d)\n",
854 mi->mi_attrs[i]->ai_desc->ad_type->sat_cname.bv_val,
855 mdb_strerror(rc), rc );
859 slapMode ^= SLAP_TRUNCATE_MODE;
863 * just (re)add them for now
864 * Use truncate mode to empty/reset index databases
867 Debug( LDAP_DEBUG_TRACE,
868 "=> " LDAP_XSTRING(mdb_tool_entry_reindex) "( %ld )\n",
873 op.o_tmpmemctx = NULL;
874 op.o_tmpmfuncs = &ch_mfuncs;
876 rc = mdb_tool_index_add( &op, txi, e );
881 if ( mdb_writes >= mdb_writes_per_commit ) {
884 MDB_TOOL_IDL_FLUSH( be, txi );
885 rc = mdb_txn_commit( txi );
887 for ( i=0; i<mi->mi_nattrs; i++ )
888 mi->mi_attrs[i]->ai_cursor = NULL;
890 Debug( LDAP_DEBUG_ANY,
891 "=> " LDAP_XSTRING(mdb_tool_entry_reindex)
892 ": txn_commit failed: %s (%d)\n",
893 mdb_strerror(rc), rc, 0 );
896 mdb_cursor_close( cursor );
898 /* Must close the read txn to allow old pages to be reclaimed. */
899 mdb_txn_abort( mdb_tool_txn );
900 /* and then reopen it so that tool_entry_next still works. */
901 mdb_txn_begin( mi->mi_dbenv, NULL, MDB_RDONLY, &mdb_tool_txn );
902 mdb_cursor_open( mdb_tool_txn, mi->mi_id2entry, &cursor );
904 key.mv_size = sizeof(ID);
905 mdb_cursor_get( cursor, &key, NULL, MDB_SET );
911 mdb_cursor_close( cursor );
913 mdb_txn_abort( txi );
914 for ( i=0; i<mi->mi_nattrs; i++ )
915 mi->mi_attrs[i]->ai_cursor = NULL;
916 Debug( LDAP_DEBUG_ANY,
917 "=> " LDAP_XSTRING(mdb_tool_entry_reindex)
918 ": txn_aborted! err=%d\n",
923 mdb_entry_release( &op, e, 0 );
928 ID mdb_tool_entry_modify(
931 struct berval *text )
934 struct mdb_info *mdb;
938 assert( be != NULL );
939 assert( slapMode & SLAP_TOOL_MODE );
941 assert( text != NULL );
942 assert( text->bv_val != NULL );
943 assert( text->bv_val[0] == '\0' ); /* overconservative? */
945 assert ( e->e_id != NOID );
947 Debug( LDAP_DEBUG_TRACE,
948 "=> " LDAP_XSTRING(mdb_tool_entry_modify) "( %ld, \"%s\" )\n",
949 (long) e->e_id, e->e_dn, 0 );
951 mdb = (struct mdb_info *) be->be_private;
954 mdb_cursor_close( cursor );
957 if ( !mdb_tool_txn ) {
958 rc = mdb_txn_begin( mdb->mi_dbenv, NULL, 0, &mdb_tool_txn );
960 snprintf( text->bv_val, text->bv_len,
961 "txn_begin failed: %s (%d)",
962 mdb_strerror(rc), rc );
963 Debug( LDAP_DEBUG_ANY,
964 "=> " LDAP_XSTRING(mdb_tool_entry_modify) ": %s\n",
965 text->bv_val, 0, 0 );
972 op.o_tmpmemctx = NULL;
973 op.o_tmpmfuncs = &ch_mfuncs;
976 rc = mdb_id2entry_update( &op, mdb_tool_txn, NULL, e );
978 snprintf( text->bv_val, text->bv_len,
979 "id2entry_update failed: err=%d", rc );
980 Debug( LDAP_DEBUG_ANY,
981 "=> " LDAP_XSTRING(mdb_tool_entry_modify) ": %s\n",
982 text->bv_val, 0, 0 );
988 rc = mdb_txn_commit( mdb_tool_txn );
990 snprintf( text->bv_val, text->bv_len,
991 "txn_commit failed: %s (%d)",
992 mdb_strerror(rc), rc );
993 Debug( LDAP_DEBUG_ANY,
994 "=> " LDAP_XSTRING(mdb_tool_entry_modify) ": "
995 "%s\n", text->bv_val, 0, 0 );
1000 mdb_txn_abort( mdb_tool_txn );
1001 snprintf( text->bv_val, text->bv_len,
1002 "txn_aborted! %s (%d)",
1003 mdb_strerror(rc), rc );
1004 Debug( LDAP_DEBUG_ANY,
1005 "=> " LDAP_XSTRING(mdb_tool_entry_modify) ": %s\n",
1006 text->bv_val, 0, 0 );
1009 mdb_tool_txn = NULL;
1016 mdb_tool_index_task( void *ctx, void *ptr )
1018 int base = *(int *)ptr;
1022 ldap_pvt_thread_mutex_lock( &mdb_tool_index_mutex );
1023 mdb_tool_index_tcount--;
1024 if ( !mdb_tool_index_tcount )
1025 ldap_pvt_thread_cond_signal( &mdb_tool_index_cond_main );
1026 ldap_pvt_thread_cond_wait( &mdb_tool_index_cond_work,
1027 &mdb_tool_index_mutex );
1028 if ( slapd_shutdown ) {
1029 mdb_tool_index_tcount--;
1030 if ( !mdb_tool_index_tcount )
1031 ldap_pvt_thread_cond_signal( &mdb_tool_index_cond_main );
1032 ldap_pvt_thread_mutex_unlock( &mdb_tool_index_mutex );
1035 ldap_pvt_thread_mutex_unlock( &mdb_tool_index_mutex );
1036 mdb_tool_index_rec[base].ir_i = mdb_index_recrun( mdb_tool_ix_op,
1038 mdb_tool_info, mdb_tool_index_rec, mdb_tool_ix_id, base );
1044 #ifdef MDB_TOOL_IDL_CACHING
1046 mdb_tool_idl_cmp( const void *v1, const void *v2 )
1048 const mdb_tool_idl_cache *c1 = v1, *c2 = v2;
1051 if (( rc = c1->kstr.bv_len - c2->kstr.bv_len )) return rc;
1052 return memcmp( c1->kstr.bv_val, c2->kstr.bv_val, c1->kstr.bv_len );
1056 mdb_tool_idl_flush_one( MDB_cursor *mc, AttrInfo *ai, mdb_tool_idl_cache *ic )
1058 mdb_tool_idl_cache_entry *ice;
1059 MDB_val key, data[2];
1063 /* Freshly allocated, ignore it */
1064 if ( !ic->head && ic->count <= MDB_IDL_DB_SIZE ) {
1068 key.mv_data = ic->kstr.bv_val;
1069 key.mv_size = ic->kstr.bv_len;
1071 if ( ic->count > MDB_IDL_DB_SIZE ) {
1072 while ( ic->flags & WAS_FOUND ) {
1073 rc = mdb_cursor_get( mc, &key, data, MDB_SET );
1075 /* FIXME: find out why this happens */
1079 if ( ic->flags & WAS_RANGE ) {
1081 rc = mdb_cursor_get( mc, &key, data, MDB_NEXT_DUP );
1084 rc = mdb_cursor_get( mc, &key, data, MDB_NEXT_DUP );
1086 /* Store range hi */
1087 data[0].mv_data = &ic->last;
1088 rc = mdb_cursor_put( mc, &key, data, MDB_CURRENT );
1090 /* Delete old data, replace with range */
1091 ic->first = *(ID *)data[0].mv_data;
1092 mdb_cursor_del( mc, MDB_NODUPDATA );
1096 if ( !(ic->flags & WAS_RANGE)) {
1097 /* range, didn't exist before */
1099 data[0].mv_size = sizeof(ID);
1100 data[0].mv_data = &nid;
1101 rc = mdb_cursor_put( mc, &key, data, 0 );
1103 data[0].mv_data = &ic->first;
1104 rc = mdb_cursor_put( mc, &key, data, 0 );
1106 data[0].mv_data = &ic->last;
1107 rc = mdb_cursor_put( mc, &key, data, 0 );
1118 data[0].mv_size = sizeof(ID);
1121 for ( ice = ic->head, n=0; ice; ice = ice->next, n++ ) {
1126 end = ic->count & (IDBLOCK-1);
1130 data[1].mv_size = end - i;
1131 data[0].mv_data = &ice->ids[i];
1133 rc = mdb_cursor_put( mc, &key, data, MDB_NODUPDATA|MDB_APPEND|MDB_MULTIPLE );
1135 if ( rc == MDB_KEYEXIST ) {
1144 ic->tail->next = ai->ai_flist;
1145 ai->ai_flist = ic->head;
1148 ic->head = ai->ai_clist;
1154 mdb_tool_idl_flush_db( MDB_txn *txn, AttrInfo *ai )
1160 mdb_cursor_open( txn, ai->ai_dbi, &mc );
1161 root = tavl_end( ai->ai_root, TAVL_DIR_LEFT );
1163 rc = mdb_tool_idl_flush_one( mc, ai, root->avl_data );
1166 } while ((root = tavl_next(root, TAVL_DIR_RIGHT)));
1167 mdb_cursor_close( mc );
1173 mdb_tool_idl_flush( BackendDB *be, MDB_txn *txn )
1175 struct mdb_info *mdb = (struct mdb_info *) be->be_private;
1177 unsigned int i, dbi;
1179 for ( i=0; i < mdb->mi_nattrs; i++ ) {
1180 if ( !mdb->mi_attrs[i]->ai_root ) continue;
1181 rc = mdb_tool_idl_flush_db( txn, mdb->mi_attrs[i] );
1182 tavl_free(mdb->mi_attrs[i]->ai_root, NULL);
1183 mdb->mi_attrs[i]->ai_root = NULL;
1190 int mdb_tool_idl_add(
1193 struct berval *keys,
1197 mdb_tool_idl_cache *ic, itmp;
1198 mdb_tool_idl_cache_entry *ice;
1200 AttrInfo *ai = (AttrInfo *)mc;
1204 for (i=0; keys[i].bv_val; i++) {
1205 itmp.kstr = keys[i];
1206 ic = tavl_find( (Avlnode *)ai->ai_root, &itmp, mdb_tool_idl_cmp );
1208 /* No entry yet, create one */
1214 if ( ai->ai_clist ) {
1216 ai->ai_clist = ic->head;
1218 ic = ch_malloc( sizeof( mdb_tool_idl_cache ) + itmp.kstr.bv_len + 4 );
1220 ic->kstr.bv_len = itmp.kstr.bv_len;
1221 ic->kstr.bv_val = (char *)(ic+1);
1222 memcpy( ic->kstr.bv_val, itmp.kstr.bv_val, ic->kstr.bv_len );
1223 ic->head = ic->tail = NULL;
1228 tavl_insert( (Avlnode **)&ai->ai_root, ic, mdb_tool_idl_cmp,
1231 /* load existing key count here */
1232 key.mv_size = keys[i].bv_len;
1233 key.mv_data = keys[i].bv_val;
1234 rc = mdb_cursor_get( mc, &key, &data, MDB_SET );
1236 ic->flags |= WAS_FOUND;
1237 nid = *(ID *)data.mv_data;
1239 ic->count = MDB_IDL_DB_SIZE+1;
1240 ic->flags |= WAS_RANGE;
1244 mdb_cursor_count( mc, &count );
1247 ic->offset = count & (IDBLOCK-1);
1251 /* are we a range already? */
1252 if ( ic->count > MDB_IDL_DB_SIZE ) {
1255 /* Are we at the limit, and converting to a range? */
1256 } else if ( ic->count == MDB_IDL_DB_SIZE ) {
1258 ic->tail->next = ai->ai_flist;
1259 ai->ai_flist = ic->head;
1261 ic->head = ic->tail = NULL;
1266 /* No free block, create that too */
1267 lcount = ic->count & (IDBLOCK-1);
1268 if ( !ic->tail || lcount == 0) {
1269 if ( ai->ai_flist ) {
1271 ai->ai_flist = ice->next;
1273 ice = ch_malloc( sizeof( mdb_tool_idl_cache_entry ));
1279 ic->tail->next = ice;
1283 ice->ids[lcount-1] = 0;
1288 if (!lcount || ice->ids[lcount-1] != id)
1289 ice->ids[lcount] = id;
1295 #endif /* MDB_TOOL_IDL_CACHING */
1297 /* Upgrade from pre 2.4.34 dn2id format */
1299 #include <ac/unistd.h>
1300 #include <lutil_meter.h>
1302 #define STACKSIZ 2048
1304 typedef struct rec {
1311 mdb_dn2id_upgrade( BackendDB *be ) {
1312 struct mdb_info *mi = (struct mdb_info *) be->be_private;
1314 MDB_cursor *mc = NULL;
1317 int rc, writes=0, depth=0;
1318 int enable_meter = 0;
1319 ID id = 0, *num, count = 0;
1321 lutil_meter_t meter;
1323 if (!(mi->mi_flags & MDB_NEED_UPGRADE)) {
1324 Debug( LDAP_DEBUG_ANY, "database %s: No upgrade needed.\n",
1325 be->be_suffix[0].bv_val, 0, 0 );
1332 mdb_stat(mdb_cursor_txn(cursor), mi->mi_dbis[MDB_ID2ENTRY], &st);
1333 if (!st.ms_entries) {
1334 /* Empty DB, nothing to upgrade? */
1338 enable_meter = !lutil_meter_open(&meter,
1339 &lutil_meter_text_display,
1340 &lutil_meter_linear_estimator,
1344 num = ch_malloc(STACKSIZ * (sizeof(ID) + sizeof(rec)));
1345 stack = (rec *)(num + STACKSIZ);
1347 rc = mdb_txn_begin(mi->mi_dbenv, NULL, 0, &mt);
1349 Debug(LDAP_DEBUG_ANY, "mdb_dn2id_upgrade: mdb_txn_begin failed, %s (%d)\n",
1350 mdb_strerror(rc), rc, 0 );
1353 rc = mdb_cursor_open(mt, mi->mi_dbis[MDB_DN2ID], &mc);
1355 Debug(LDAP_DEBUG_ANY, "mdb_dn2id_upgrade: mdb_cursor_open failed, %s (%d)\n",
1356 mdb_strerror(rc), rc, 0 );
1360 key.mv_size = sizeof(ID);
1361 /* post-order depth-first update */
1368 stack[depth].id = id;
1369 rc = mdb_cursor_get(mc, &key, &data, MDB_SET);
1371 Debug(LDAP_DEBUG_ANY, "mdb_dn2id_upgrade: mdb_cursor_get failed, %s (%d)\n",
1372 mdb_strerror(rc), rc, 0 );
1377 rc = mdb_cursor_count(mc, &dkids);
1379 Debug(LDAP_DEBUG_ANY, "mdb_dn2id_upgrade: mdb_cursor_count failed, %s (%d)\n",
1380 mdb_strerror(rc), rc, 0 );
1384 rc = mdb_cursor_get(mc, &key, &data, MDB_NEXT_DUP);
1386 ptr = (unsigned char *)data.mv_data + data.mv_size - sizeof(ID);
1387 memcpy(&id, ptr, sizeof(ID));
1389 memcpy(stack[depth].rdn, data.mv_data, data.mv_size);
1390 stack[depth].len = data.mv_size;
1395 /* pop: write updated count, advance to next node */
1397 /* update superior counts */
1399 num[depth-1] += num[depth];
1402 id = stack[depth-1].id;
1403 data.mv_data = stack[depth].rdn;
1404 data.mv_size = stack[depth].len;
1405 rc = mdb_cursor_get(mc, &key, &data, MDB_GET_BOTH);
1407 Debug(LDAP_DEBUG_ANY, "mdb_dn2id_upgrade: mdb_cursor_get(BOTH) failed, %s (%d)\n",
1408 mdb_strerror(rc), rc, 0 );
1411 data.mv_data = stack[depth].rdn;
1412 ptr = (unsigned char *)data.mv_data + data.mv_size;
1413 memcpy(ptr, &num[depth], sizeof(ID));
1414 data.mv_size += sizeof(ID);
1415 rc = mdb_cursor_del(mc, 0);
1417 Debug(LDAP_DEBUG_ANY, "mdb_dn2id_upgrade: mdb_cursor_del failed, %s (%d)\n",
1418 mdb_strerror(rc), rc, 0 );
1421 rc = mdb_cursor_put(mc, &key, &data, 0);
1423 Debug(LDAP_DEBUG_ANY, "mdb_dn2id_upgrade: mdb_cursor_put failed, %s (%d)\n",
1424 mdb_strerror(rc), rc, 0 );
1430 lutil_meter_update(&meter, count, 0);
1435 len = (ptr[0] & 0x7f) << 8 | ptr[1];
1436 printf("ID: %zu, %zu, %.*s\n", stack[depth].id, num[depth], len, ptr+2);
1440 if (writes == 1000) {
1441 mdb_cursor_close(mc);
1442 rc = mdb_txn_commit(mt);
1444 Debug(LDAP_DEBUG_ANY, "mdb_dn2id_upgrade: mdb_txn_commit failed, %s (%d)\n",
1445 mdb_strerror(rc), rc, 0 );
1448 rc = mdb_txn_begin(mi->mi_dbenv, NULL, 0, &mt);
1450 Debug(LDAP_DEBUG_ANY, "mdb_dn2id_upgrade: mdb_txn_begin(2) failed, %s (%d)\n",
1451 mdb_strerror(rc), rc, 0 );
1454 rc = mdb_cursor_open(mt, mi->mi_dbis[MDB_DN2ID], &mc);
1456 Debug(LDAP_DEBUG_ANY, "mdb_dn2id_upgrade: mdb_cursor_open(2) failed, %s (%d)\n",
1457 mdb_strerror(rc), rc, 0 );
1460 rc = mdb_cursor_get(mc, &key, &data, MDB_GET_BOTH);
1462 Debug(LDAP_DEBUG_ANY, "mdb_dn2id_upgrade: mdb_cursor_get(2) failed, %s (%d)\n",
1463 mdb_strerror(rc), rc, 0 );
1470 rc = mdb_cursor_get(mc, &key, &data, MDB_NEXT_DUP);
1480 mdb_cursor_close(mc);
1483 r2 = mdb_txn_commit(mt);
1485 Debug(LDAP_DEBUG_ANY, "mdb_dn2id_upgrade: mdb_txn_commit(2) failed, %s (%d)\n",
1486 mdb_strerror(r2), r2, 0 );
1493 lutil_meter_update(&meter, count, 1);
1494 lutil_meter_close(&meter);