1 /* tools.c - tools for slap tools */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 2011 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 static MDB_txn *txn = NULL, *txi = NULL;
28 static MDB_cursor *cursor = NULL, *idcursor = NULL;
29 static MDB_val key, data;
30 static EntryHeader eh;
31 static ID previd = NOID;
33 typedef struct dn_id {
38 #define HOLE_SIZE 4096
39 static dn_id hbuf[HOLE_SIZE], *holes = hbuf;
40 static unsigned nhmax = HOLE_SIZE;
41 static unsigned nholes;
43 static struct berval *tool_base;
44 static int tool_scope;
45 static Filter *tool_filter;
46 static Entry *tool_next_entry;
49 static ID mdb_tool_ix_id;
50 static Operation *mdb_tool_ix_op;
51 static int *mdb_tool_index_threads, mdb_tool_index_tcount;
52 static void *mdb_tool_index_rec;
53 static struct mdb_info *mdb_tool_info;
54 static ldap_pvt_thread_mutex_t mdb_tool_index_mutex;
55 static ldap_pvt_thread_cond_t mdb_tool_index_cond_main;
56 static ldap_pvt_thread_cond_t mdb_tool_index_cond_work;
57 static void * mdb_tool_index_task( void *ctx, void *ptr );
60 static int mdb_writes, mdb_writes_per_commit;
63 mdb_tool_entry_get_int( BackendDB *be, ID id, Entry **ep );
65 int mdb_tool_entry_open(
66 BackendDB *be, int mode )
68 /* In Quick mode, commit once per 1000 entries */
70 if ( slapMode & SLAP_TOOL_QUICK )
71 mdb_writes_per_commit = 1000;
73 mdb_writes_per_commit = 1;
76 /* Set up for threaded slapindex */
77 if (( slapMode & (SLAP_TOOL_QUICK|SLAP_TOOL_READONLY)) == SLAP_TOOL_QUICK ) {
78 if ( !mdb_tool_info ) {
79 ldap_pvt_thread_mutex_init( &mdb_tool_index_mutex );
80 ldap_pvt_thread_cond_init( &mdb_tool_index_cond_main );
81 ldap_pvt_thread_cond_init( &mdb_tool_index_cond_work );
82 if ( mdb->bi_nattrs ) {
84 mdb_tool_index_threads = ch_malloc( slap_tool_thread_max * sizeof( int ));
85 mdb_tool_index_rec = ch_malloc( mdb->bi_nattrs * sizeof( IndexRec ));
86 mdb_tool_index_tcount = slap_tool_thread_max - 1;
87 for (i=1; i<slap_tool_thread_max; i++) {
88 int *ptr = ch_malloc( sizeof( int ));
90 ldap_pvt_thread_pool_submit( &connection_pool,
91 mdb_tool_index_task, ptr );
102 int mdb_tool_entry_close(
106 if ( mdb_tool_info ) {
108 ldap_pvt_thread_mutex_lock( &mdb_tool_index_mutex );
110 /* There might still be some threads starting */
111 while ( mdb_tool_index_tcount ) {
112 ldap_pvt_thread_cond_wait( &mdb_tool_index_cond_main,
113 &mdb_tool_index_mutex );
116 mdb_tool_index_tcount = slap_tool_thread_max - 1;
117 ldap_pvt_thread_cond_broadcast( &mdb_tool_index_cond_work );
119 /* Make sure all threads are stopped */
120 while ( mdb_tool_index_tcount ) {
121 ldap_pvt_thread_cond_wait( &mdb_tool_index_cond_main,
122 &mdb_tool_index_mutex );
124 ldap_pvt_thread_mutex_unlock( &mdb_tool_index_mutex );
126 mdb_tool_info = NULL;
128 ch_free( mdb_tool_index_threads );
129 ch_free( mdb_tool_index_rec );
130 mdb_tool_index_tcount = slap_tool_thread_max - 1;
135 mdb_cursor_close( idcursor );
139 mdb_cursor_close( cursor );
143 if ( mdb_txn_commit( txn ))
150 fprintf( stderr, "Error, entries missing!\n");
151 for (i=0; i<nholes; i++) {
152 fprintf(stderr, " entry %ld: %s\n",
153 holes[i].id, holes[i].dn.bv_val);
163 mdb_tool_entry_first_x(
173 return mdb_tool_entry_next( be );
176 ID mdb_tool_entry_next(
181 struct mdb_info *mdb;
183 assert( be != NULL );
184 assert( slapMode & SLAP_TOOL_MODE );
186 mdb = (struct mdb_info *) be->be_private;
187 assert( mdb != NULL );
190 rc = mdb_txn_begin( mdb->mi_dbenv, MDB_RDONLY, &txn );
193 rc = mdb_cursor_open( txn, mdb->mi_id2entry, &cursor );
195 mdb_txn_abort( txn );
201 rc = mdb_cursor_get( cursor, &key, &data, MDB_NEXT );
207 previd = *(ID *)key.mv_data;
210 if ( tool_filter || tool_base ) {
211 static Operation op = {0};
212 static Opheader ohdr = {0};
216 op.o_tmpmemctx = NULL;
217 op.o_tmpmfuncs = &ch_mfuncs;
219 if ( tool_next_entry ) {
220 mdb_entry_release( &op, tool_next_entry, 0 );
221 tool_next_entry = NULL;
224 rc = mdb_tool_entry_get_int( be, id, &tool_next_entry );
225 if ( rc == LDAP_NO_SUCH_OBJECT ) {
229 assert( tool_next_entry != NULL );
231 if ( tool_filter && test_filter( NULL, tool_next_entry, tool_filter ) != LDAP_COMPARE_TRUE )
233 mdb_entry_release( &op, tool_next_entry, 0 );
234 tool_next_entry = NULL;
242 ID mdb_tool_dn2id_get(
247 struct mdb_info *mdb;
253 if ( BER_BVISEMPTY(dn) )
256 mdb = (struct mdb_info *) be->be_private;
259 rc = mdb_txn_begin( mdb->mi_dbenv, (slapMode & SLAP_TOOL_READONLY) != 0 ?
260 MDB_RDONLY : 0, &txn );
267 op.o_tmpmemctx = NULL;
268 op.o_tmpmfuncs = &ch_mfuncs;
270 rc = mdb_dn2id( &op, txn, dn, &id, NULL, NULL );
271 if ( rc == MDB_NOTFOUND )
278 mdb_tool_entry_get_int( BackendDB *be, ID id, Entry **ep )
281 struct berval dn = BER_BVNULL, ndn = BER_BVNULL;
284 assert( be != NULL );
285 assert( slapMode & SLAP_TOOL_MODE );
287 if ( ( tool_filter || tool_base ) && id == previd && tool_next_entry != NULL ) {
288 *ep = tool_next_entry;
289 tool_next_entry = NULL;
293 if ( id != previd ) {
294 key.mv_size = sizeof(ID);
296 rc = mdb_cursor_get( cursor, &key, &data, MDB_SET );
303 if ( slapMode & SLAP_TOOL_READONLY ) {
309 op.o_tmpmemctx = NULL;
310 op.o_tmpmfuncs = &ch_mfuncs;
312 rc = mdb_id2name( &op, txn, &idcursor, id, &dn, &ndn );
315 mdb_entry_return( e );
319 if ( tool_base != NULL ) {
320 if ( !dnIsSuffixScope( &ndn, tool_base, tool_scope ) ) {
321 ch_free( dn.bv_val );
322 ch_free( ndn.bv_val );
323 rc = LDAP_NO_SUCH_OBJECT;
328 eh.bv.bv_val = data.mv_data;
329 eh.bv.bv_len = data.mv_size;
331 rc = entry_header( &eh );
336 eh.bv.bv_len = eh.nvals * sizeof( struct berval );
337 eh.bv.bv_val = ch_malloc( eh.bv.bv_len );
338 rc = entry_decode( &eh, &e );
340 if ( !BER_BVISNULL( &dn )) {
344 e->e_name.bv_val = NULL;
345 e->e_nname.bv_val = NULL;
358 mdb_tool_entry_get( BackendDB *be, ID id )
362 (void)mdb_tool_entry_get_int( be, id, &e );
366 static int mdb_tool_next_id(
373 struct berval dn = e->e_name;
374 struct berval ndn = e->e_nname;
375 struct berval pdn, npdn, nmatched;
379 if (ndn.bv_len == 0) {
384 rc = mdb_dn2id( op, tid, &ndn, &id, NULL, &nmatched );
385 if ( rc == MDB_NOTFOUND ) {
386 if ( !be_issuffix( op->o_bd, &ndn ) ) {
388 dnParent( &ndn, &npdn );
389 if ( nmatched.bv_len != npdn.bv_len ) {
390 dnParent( &dn, &pdn );
393 rc = mdb_tool_next_id( op, tid, e, text, 1 );
399 /* If parent didn't exist, it was created just now
400 * and its ID is now in e->e_id. Make sure the current
401 * entry gets added under the new parent ID.
403 if ( eid != e->e_id ) {
410 rc = mdb_next_id( op->o_bd, tid, &e->e_id );
412 snprintf( text->bv_val, text->bv_len,
413 "next_id failed: %s (%d)",
414 mdb_strerror(rc), rc );
415 Debug( LDAP_DEBUG_ANY,
416 "=> mdb_tool_next_id: %s\n", text->bv_val, 0, 0 );
419 rc = mdb_dn2id_add( op, tid, pid, e );
421 snprintf( text->bv_val, text->bv_len,
422 "dn2id_add failed: %s (%d)",
423 mdb_strerror(rc), rc );
424 Debug( LDAP_DEBUG_ANY,
425 "=> mdb_tool_next_id: %s\n", text->bv_val, 0, 0 );
427 if ( nholes == nhmax - 1 ) {
428 if ( holes == hbuf ) {
429 holes = ch_malloc( nhmax * sizeof(dn_id) * 2 );
430 AC_MEMCPY( holes, hbuf, sizeof(hbuf) );
432 holes = ch_realloc( holes, nhmax * sizeof(dn_id) * 2 );
436 ber_dupbv( &holes[nholes].dn, &ndn );
437 holes[nholes++].id = e->e_id;
439 } else if ( !hole ) {
444 for ( i=0; i<nholes; i++) {
445 if ( holes[i].id == e->e_id ) {
446 free(holes[i].dn.bv_val);
447 for (j=i;j<nholes;j++) holes[j] = holes[j+1];
451 } else if ( holes[i].id > e->e_id ) {
465 struct mdb_info *mdb = (struct mdb_info *) op->o_bd->be_private;
467 if ( !mdb->mi_nattrs )
471 if ( slapMode & SLAP_TOOL_QUICK ) {
476 ir = mdb_tool_index_rec;
477 memset(ir, 0, mdb->bi_nattrs * sizeof( IndexRec ));
479 for ( a = e->e_attrs; a != NULL; a = a->a_next ) {
480 rc = mdb_index_recset( mdb, a, a->a_desc->ad_type,
481 &a->a_desc->ad_tags, ir );
485 mdb_tool_ix_id = e->e_id;
487 ldap_pvt_thread_mutex_lock( &mdb_tool_index_mutex );
488 /* Wait for all threads to be ready */
489 while ( mdb_tool_index_tcount ) {
490 ldap_pvt_thread_cond_wait( &mdb_tool_index_cond_main,
491 &mdb_tool_index_mutex );
493 for ( i=1; i<slap_tool_thread_max; i++ )
494 mdb_tool_index_threads[i] = LDAP_BUSY;
495 mdb_tool_index_tcount = slap_tool_thread_max - 1;
496 ldap_pvt_thread_cond_broadcast( &mdb_tool_index_cond_work );
497 ldap_pvt_thread_mutex_unlock( &mdb_tool_index_mutex );
498 rc = mdb_index_recrun( op, mdb, ir, e->e_id, 0 );
501 ldap_pvt_thread_mutex_lock( &mdb_tool_index_mutex );
502 for ( i=1; i<slap_tool_thread_max; i++ ) {
503 if ( mdb_tool_index_threads[i] == LDAP_BUSY ) {
504 ldap_pvt_thread_cond_wait( &mdb_tool_index_cond_main,
505 &mdb_tool_index_mutex );
509 if ( mdb_tool_index_threads[i] ) {
510 rc = mdb_tool_index_threads[i];
514 ldap_pvt_thread_mutex_unlock( &mdb_tool_index_mutex );
519 return mdb_index_entry_add( op, txn, e );
523 ID mdb_tool_entry_put(
526 struct berval *text )
529 struct mdb_info *mdb;
533 assert( be != NULL );
534 assert( slapMode & SLAP_TOOL_MODE );
536 assert( text != NULL );
537 assert( text->bv_val != NULL );
538 assert( text->bv_val[0] == '\0' ); /* overconservative? */
540 Debug( LDAP_DEBUG_TRACE, "=> " LDAP_XSTRING(mdb_tool_entry_put)
541 "( %ld, \"%s\" )\n", (long) e->e_id, e->e_dn, 0 );
543 mdb = (struct mdb_info *) be->be_private;
546 rc = mdb_txn_begin( mdb->mi_dbenv, 0, &txn );
548 snprintf( text->bv_val, text->bv_len,
549 "txn_begin failed: %s (%d)",
550 mdb_strerror(rc), rc );
551 Debug( LDAP_DEBUG_ANY,
552 "=> " LDAP_XSTRING(mdb_tool_entry_put) ": %s\n",
553 text->bv_val, 0, 0 );
560 op.o_tmpmemctx = NULL;
561 op.o_tmpmfuncs = &ch_mfuncs;
563 /* add dn2id indices */
564 rc = mdb_tool_next_id( &op, txn, e, text, 0 );
569 rc = mdb_tool_index_add( &op, txn, e );
571 snprintf( text->bv_val, text->bv_len,
572 "index_entry_add failed: %s (%d)",
573 rc == LDAP_OTHER ? "Internal error" :
574 mdb_strerror(rc), rc );
575 Debug( LDAP_DEBUG_ANY,
576 "=> " LDAP_XSTRING(mdb_tool_entry_put) ": %s\n",
577 text->bv_val, 0, 0 );
583 rc = mdb_id2entry_add( &op, txn, e );
585 snprintf( text->bv_val, text->bv_len,
586 "id2entry_add failed: %s (%d)",
587 mdb_strerror(rc), rc );
588 Debug( LDAP_DEBUG_ANY,
589 "=> " LDAP_XSTRING(mdb_tool_entry_put) ": %s\n",
590 text->bv_val, 0, 0 );
597 if ( mdb_writes >= mdb_writes_per_commit ) {
598 rc = mdb_txn_commit( txn );
602 snprintf( text->bv_val, text->bv_len,
603 "txn_commit failed: %s (%d)",
604 mdb_strerror(rc), rc );
605 Debug( LDAP_DEBUG_ANY,
606 "=> " LDAP_XSTRING(mdb_tool_entry_put) ": %s\n",
607 text->bv_val, 0, 0 );
613 mdb_txn_abort( txn );
615 snprintf( text->bv_val, text->bv_len,
616 "txn_aborted! %s (%d)",
617 rc == LDAP_OTHER ? "Internal error" :
618 mdb_strerror(rc), rc );
619 Debug( LDAP_DEBUG_ANY,
620 "=> " LDAP_XSTRING(mdb_tool_entry_put) ": %s\n",
621 text->bv_val, 0, 0 );
628 int mdb_tool_entry_reindex(
631 AttributeDescription **adv )
633 struct mdb_info *mi = (struct mdb_info *) be->be_private;
639 Debug( LDAP_DEBUG_ARGS,
640 "=> " LDAP_XSTRING(mdb_tool_entry_reindex) "( %ld )\n",
642 assert( tool_base == NULL );
643 assert( tool_filter == NULL );
645 /* No indexes configured, nothing to do. Could return an
646 * error here to shortcut things.
652 /* Check for explicit list of attrs to index */
656 if ( mi->mi_attrs[0]->ai_desc != adv[0] ) {
658 for ( n = 0; adv[n]; n++ ) ;
661 for ( i = 0; i < n; i++ ) {
662 AttributeDescription *ad = adv[i];
663 for ( j = i-1; j>=0; j--) {
664 if ( SLAP_PTRCMP( adv[j], ad ) <= 0 ) break;
671 for ( i = 0; adv[i]; i++ ) {
672 if ( mi->mi_attrs[i]->ai_desc != adv[i] ) {
673 for ( j = i+1; j < mi->mi_nattrs; j++ ) {
674 if ( mi->mi_attrs[j]->ai_desc == adv[i] ) {
675 AttrInfo *ai = mi->mi_attrs[i];
676 mi->mi_attrs[i] = mi->mi_attrs[j];
677 mi->mi_attrs[j] = ai;
681 if ( j == mi->mi_nattrs ) {
682 Debug( LDAP_DEBUG_ANY,
683 LDAP_XSTRING(mdb_tool_entry_reindex)
684 ": no index configured for %s\n",
685 adv[i]->ad_cname.bv_val, 0, 0 );
693 e = mdb_tool_entry_get( be, id );
696 Debug( LDAP_DEBUG_ANY,
697 LDAP_XSTRING(mdb_tool_entry_reindex)
698 ": could not locate id=%ld\n",
704 rc = mdb_txn_begin( mi->mi_dbenv, 0, &txi );
706 Debug( LDAP_DEBUG_ANY,
707 "=> " LDAP_XSTRING(mdb_tool_entry_reindex) ": "
708 "txn_begin failed: %s (%d)\n",
709 mdb_strerror(rc), rc, 0 );
715 * just (re)add them for now
716 * assume that some other routine (not yet implemented)
717 * will zap index databases
721 Debug( LDAP_DEBUG_TRACE,
722 "=> " LDAP_XSTRING(mdb_tool_entry_reindex) "( %ld, \"%s\" )\n",
723 (long) id, e->e_dn, 0 );
727 op.o_tmpmemctx = NULL;
728 op.o_tmpmfuncs = &ch_mfuncs;
730 rc = mdb_tool_index_add( &op, txi, e );
735 if ( mdb_writes >= mdb_writes_per_commit ) {
736 rc = mdb_txn_commit( txi );
738 Debug( LDAP_DEBUG_ANY,
739 "=> " LDAP_XSTRING(mdb_tool_entry_reindex)
740 ": txn_commit failed: %s (%d)\n",
741 mdb_strerror(rc), rc, 0 );
748 mdb_txn_abort( txi );
749 Debug( LDAP_DEBUG_ANY,
750 "=> " LDAP_XSTRING(mdb_tool_entry_reindex)
751 ": txn_aborted! %s (%d)\n",
752 mdb_strerror(rc), rc, 0 );
756 mdb_entry_release( &op, e, 0 );
761 ID mdb_tool_entry_modify(
764 struct berval *text )
767 struct mdb_info *mdb;
772 assert( be != NULL );
773 assert( slapMode & SLAP_TOOL_MODE );
775 assert( text != NULL );
776 assert( text->bv_val != NULL );
777 assert( text->bv_val[0] == '\0' ); /* overconservative? */
779 assert ( e->e_id != NOID );
781 Debug( LDAP_DEBUG_TRACE,
782 "=> " LDAP_XSTRING(mdb_tool_entry_modify) "( %ld, \"%s\" )\n",
783 (long) e->e_id, e->e_dn, 0 );
785 mdb = (struct mdb_info *) be->be_private;
788 mdb_cursor_close( cursor );
791 rc = mdb_txn_begin( mdb->mi_dbenv, 0, &tid );
793 snprintf( text->bv_val, text->bv_len,
794 "txn_begin failed: %s (%d)",
795 mdb_strerror(rc), rc );
796 Debug( LDAP_DEBUG_ANY,
797 "=> " LDAP_XSTRING(mdb_tool_entry_modify) ": %s\n",
798 text->bv_val, 0, 0 );
804 op.o_tmpmemctx = NULL;
805 op.o_tmpmfuncs = &ch_mfuncs;
808 rc = mdb_id2entry_update( &op, tid, e );
810 snprintf( text->bv_val, text->bv_len,
811 "id2entry_add failed: %s (%d)",
812 mdb_strerror(rc), rc );
813 Debug( LDAP_DEBUG_ANY,
814 "=> " LDAP_XSTRING(mdb_tool_entry_modify) ": %s\n",
815 text->bv_val, 0, 0 );
821 rc = mdb_txn_commit( tid );
823 snprintf( text->bv_val, text->bv_len,
824 "txn_commit failed: %s (%d)",
825 mdb_strerror(rc), rc );
826 Debug( LDAP_DEBUG_ANY,
827 "=> " LDAP_XSTRING(mdb_tool_entry_modify) ": "
828 "%s\n", text->bv_val, 0, 0 );
833 mdb_txn_abort( tid );
834 snprintf( text->bv_val, text->bv_len,
835 "txn_aborted! %s (%d)",
836 mdb_strerror(rc), rc );
837 Debug( LDAP_DEBUG_ANY,
838 "=> " LDAP_XSTRING(mdb_tool_entry_modify) ": %s\n",
839 text->bv_val, 0, 0 );
848 mdb_tool_index_task( void *ctx, void *ptr )
850 int base = *(int *)ptr;
854 ldap_pvt_thread_mutex_lock( &mdb_tool_index_mutex );
855 mdb_tool_index_tcount--;
856 if ( !mdb_tool_index_tcount )
857 ldap_pvt_thread_cond_signal( &mdb_tool_index_cond_main );
858 ldap_pvt_thread_cond_wait( &mdb_tool_index_cond_work,
859 &mdb_tool_index_mutex );
860 if ( slapd_shutdown ) {
861 mdb_tool_index_tcount--;
862 if ( !mdb_tool_index_tcount )
863 ldap_pvt_thread_cond_signal( &mdb_tool_index_cond_main );
864 ldap_pvt_thread_mutex_unlock( &mdb_tool_index_mutex );
867 ldap_pvt_thread_mutex_unlock( &mdb_tool_index_mutex );
869 mdb_tool_index_threads[base] = mdb_index_recrun( mdb_tool_ix_op,
870 mdb_tool_info, mdb_tool_index_rec, mdb_tool_ix_id, base );