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 ID previd = NOID;
32 typedef struct dn_id {
37 #define HOLE_SIZE 4096
38 static dn_id hbuf[HOLE_SIZE], *holes = hbuf;
39 static unsigned nhmax = HOLE_SIZE;
40 static unsigned nholes;
42 static struct berval *tool_base;
43 static int tool_scope;
44 static Filter *tool_filter;
45 static Entry *tool_next_entry;
48 static ID mdb_tool_ix_id;
49 static Operation *mdb_tool_ix_op;
50 static int *mdb_tool_index_threads, mdb_tool_index_tcount;
51 static void *mdb_tool_index_rec;
52 static struct mdb_info *mdb_tool_info;
53 static ldap_pvt_thread_mutex_t mdb_tool_index_mutex;
54 static ldap_pvt_thread_cond_t mdb_tool_index_cond_main;
55 static ldap_pvt_thread_cond_t mdb_tool_index_cond_work;
56 static void * mdb_tool_index_task( void *ctx, void *ptr );
59 static int mdb_writes, mdb_writes_per_commit;
62 mdb_tool_entry_get_int( BackendDB *be, ID id, Entry **ep );
64 int mdb_tool_entry_open(
65 BackendDB *be, int mode )
67 /* In Quick mode, commit once per 1000 entries */
69 if ( slapMode & SLAP_TOOL_QUICK )
70 mdb_writes_per_commit = 1000;
72 mdb_writes_per_commit = 1;
75 /* Set up for threaded slapindex */
76 if (( slapMode & (SLAP_TOOL_QUICK|SLAP_TOOL_READONLY)) == SLAP_TOOL_QUICK ) {
77 if ( !mdb_tool_info ) {
78 ldap_pvt_thread_mutex_init( &mdb_tool_index_mutex );
79 ldap_pvt_thread_cond_init( &mdb_tool_index_cond_main );
80 ldap_pvt_thread_cond_init( &mdb_tool_index_cond_work );
81 if ( mdb->bi_nattrs ) {
83 mdb_tool_index_threads = ch_malloc( slap_tool_thread_max * sizeof( int ));
84 mdb_tool_index_rec = ch_malloc( mdb->bi_nattrs * sizeof( IndexRec ));
85 mdb_tool_index_tcount = slap_tool_thread_max - 1;
86 for (i=1; i<slap_tool_thread_max; i++) {
87 int *ptr = ch_malloc( sizeof( int ));
89 ldap_pvt_thread_pool_submit( &connection_pool,
90 mdb_tool_index_task, ptr );
101 int mdb_tool_entry_close(
105 if ( mdb_tool_info ) {
107 ldap_pvt_thread_mutex_lock( &mdb_tool_index_mutex );
109 /* There might still be some threads starting */
110 while ( mdb_tool_index_tcount ) {
111 ldap_pvt_thread_cond_wait( &mdb_tool_index_cond_main,
112 &mdb_tool_index_mutex );
115 mdb_tool_index_tcount = slap_tool_thread_max - 1;
116 ldap_pvt_thread_cond_broadcast( &mdb_tool_index_cond_work );
118 /* Make sure all threads are stopped */
119 while ( mdb_tool_index_tcount ) {
120 ldap_pvt_thread_cond_wait( &mdb_tool_index_cond_main,
121 &mdb_tool_index_mutex );
123 ldap_pvt_thread_mutex_unlock( &mdb_tool_index_mutex );
125 mdb_tool_info = NULL;
127 ch_free( mdb_tool_index_threads );
128 ch_free( mdb_tool_index_rec );
129 mdb_tool_index_tcount = slap_tool_thread_max - 1;
134 mdb_cursor_close( idcursor );
138 mdb_cursor_close( cursor );
142 if ( mdb_txn_commit( txn ))
149 fprintf( stderr, "Error, entries missing!\n");
150 for (i=0; i<nholes; i++) {
151 fprintf(stderr, " entry %ld: %s\n",
152 holes[i].id, holes[i].dn.bv_val);
162 mdb_tool_entry_first_x(
172 return mdb_tool_entry_next( be );
175 ID mdb_tool_entry_next(
180 struct mdb_info *mdb;
182 assert( be != NULL );
183 assert( slapMode & SLAP_TOOL_MODE );
185 mdb = (struct mdb_info *) be->be_private;
186 assert( mdb != NULL );
189 rc = mdb_txn_begin( mdb->mi_dbenv, MDB_RDONLY, &txn );
192 rc = mdb_cursor_open( txn, mdb->mi_id2entry, &cursor );
194 mdb_txn_abort( txn );
200 rc = mdb_cursor_get( cursor, &key, &data, MDB_NEXT );
206 previd = *(ID *)key.mv_data;
209 if ( tool_filter || tool_base ) {
210 static Operation op = {0};
211 static Opheader ohdr = {0};
215 op.o_tmpmemctx = NULL;
216 op.o_tmpmfuncs = &ch_mfuncs;
218 if ( tool_next_entry ) {
219 mdb_entry_release( &op, tool_next_entry, 0 );
220 tool_next_entry = NULL;
223 rc = mdb_tool_entry_get_int( be, id, &tool_next_entry );
224 if ( rc == LDAP_NO_SUCH_OBJECT ) {
228 assert( tool_next_entry != NULL );
230 if ( tool_filter && test_filter( NULL, tool_next_entry, tool_filter ) != LDAP_COMPARE_TRUE )
232 mdb_entry_release( &op, tool_next_entry, 0 );
233 tool_next_entry = NULL;
241 ID mdb_tool_dn2id_get(
246 struct mdb_info *mdb;
252 if ( BER_BVISEMPTY(dn) )
255 mdb = (struct mdb_info *) be->be_private;
258 rc = mdb_txn_begin( mdb->mi_dbenv, (slapMode & SLAP_TOOL_READONLY) != 0 ?
259 MDB_RDONLY : 0, &txn );
266 op.o_tmpmemctx = NULL;
267 op.o_tmpmfuncs = &ch_mfuncs;
269 rc = mdb_dn2id( &op, txn, dn, &id, NULL, NULL );
270 if ( rc == MDB_NOTFOUND )
277 mdb_tool_entry_get_int( BackendDB *be, ID id, Entry **ep )
283 struct berval dn = BER_BVNULL, ndn = BER_BVNULL;
286 assert( be != NULL );
287 assert( slapMode & SLAP_TOOL_MODE );
289 if ( ( tool_filter || tool_base ) && id == previd && tool_next_entry != NULL ) {
290 *ep = tool_next_entry;
291 tool_next_entry = NULL;
295 if ( id != previd ) {
296 key.mv_size = sizeof(ID);
298 rc = mdb_cursor_get( cursor, &key, &data, MDB_SET );
307 op.o_tmpmemctx = NULL;
308 op.o_tmpmfuncs = &ch_mfuncs;
309 if ( slapMode & SLAP_TOOL_READONLY ) {
310 rc = mdb_id2name( &op, txn, &idcursor, id, &dn, &ndn );
313 mdb_entry_return( &op, e );
317 if ( tool_base != NULL ) {
318 if ( !dnIsSuffixScope( &ndn, tool_base, tool_scope ) ) {
319 ch_free( dn.bv_val );
320 ch_free( ndn.bv_val );
321 rc = LDAP_NO_SUCH_OBJECT;
325 rc = mdb_entry_decode( &op, &data, &e );
327 if ( !BER_BVISNULL( &dn )) {
331 e->e_name.bv_val = NULL;
332 e->e_nname.bv_val = NULL;
344 mdb_tool_entry_get( BackendDB *be, ID id )
348 (void)mdb_tool_entry_get_int( be, id, &e );
352 static int mdb_tool_next_id(
359 struct berval dn = e->e_name;
360 struct berval ndn = e->e_nname;
361 struct berval pdn, npdn, nmatched;
365 if (ndn.bv_len == 0) {
370 rc = mdb_dn2id( op, tid, &ndn, &id, NULL, &nmatched );
371 if ( rc == MDB_NOTFOUND ) {
372 if ( !be_issuffix( op->o_bd, &ndn ) ) {
374 dnParent( &ndn, &npdn );
375 if ( nmatched.bv_len != npdn.bv_len ) {
376 dnParent( &dn, &pdn );
379 rc = mdb_tool_next_id( op, tid, e, text, 1 );
385 /* If parent didn't exist, it was created just now
386 * and its ID is now in e->e_id. Make sure the current
387 * entry gets added under the new parent ID.
389 if ( eid != e->e_id ) {
396 rc = mdb_next_id( op->o_bd, tid, &e->e_id );
398 snprintf( text->bv_val, text->bv_len,
399 "next_id failed: %s (%d)",
400 mdb_strerror(rc), rc );
401 Debug( LDAP_DEBUG_ANY,
402 "=> mdb_tool_next_id: %s\n", text->bv_val, 0, 0 );
405 rc = mdb_dn2id_add( op, tid, pid, e );
407 snprintf( text->bv_val, text->bv_len,
408 "dn2id_add failed: %s (%d)",
409 mdb_strerror(rc), rc );
410 Debug( LDAP_DEBUG_ANY,
411 "=> mdb_tool_next_id: %s\n", text->bv_val, 0, 0 );
413 if ( nholes == nhmax - 1 ) {
414 if ( holes == hbuf ) {
415 holes = ch_malloc( nhmax * sizeof(dn_id) * 2 );
416 AC_MEMCPY( holes, hbuf, sizeof(hbuf) );
418 holes = ch_realloc( holes, nhmax * sizeof(dn_id) * 2 );
422 ber_dupbv( &holes[nholes].dn, &ndn );
423 holes[nholes++].id = e->e_id;
425 } else if ( !hole ) {
430 for ( i=0; i<nholes; i++) {
431 if ( holes[i].id == e->e_id ) {
432 free(holes[i].dn.bv_val);
433 for (j=i;j<nholes;j++) holes[j] = holes[j+1];
437 } else if ( holes[i].id > e->e_id ) {
451 struct mdb_info *mdb = (struct mdb_info *) op->o_bd->be_private;
453 if ( !mdb->mi_nattrs )
457 if ( slapMode & SLAP_TOOL_QUICK ) {
462 ir = mdb_tool_index_rec;
463 memset(ir, 0, mdb->bi_nattrs * sizeof( IndexRec ));
465 for ( a = e->e_attrs; a != NULL; a = a->a_next ) {
466 rc = mdb_index_recset( mdb, a, a->a_desc->ad_type,
467 &a->a_desc->ad_tags, ir );
471 mdb_tool_ix_id = e->e_id;
473 ldap_pvt_thread_mutex_lock( &mdb_tool_index_mutex );
474 /* Wait for all threads to be ready */
475 while ( mdb_tool_index_tcount ) {
476 ldap_pvt_thread_cond_wait( &mdb_tool_index_cond_main,
477 &mdb_tool_index_mutex );
479 for ( i=1; i<slap_tool_thread_max; i++ )
480 mdb_tool_index_threads[i] = LDAP_BUSY;
481 mdb_tool_index_tcount = slap_tool_thread_max - 1;
482 ldap_pvt_thread_cond_broadcast( &mdb_tool_index_cond_work );
483 ldap_pvt_thread_mutex_unlock( &mdb_tool_index_mutex );
484 rc = mdb_index_recrun( op, mdb, ir, e->e_id, 0 );
487 ldap_pvt_thread_mutex_lock( &mdb_tool_index_mutex );
488 for ( i=1; i<slap_tool_thread_max; i++ ) {
489 if ( mdb_tool_index_threads[i] == LDAP_BUSY ) {
490 ldap_pvt_thread_cond_wait( &mdb_tool_index_cond_main,
491 &mdb_tool_index_mutex );
495 if ( mdb_tool_index_threads[i] ) {
496 rc = mdb_tool_index_threads[i];
500 ldap_pvt_thread_mutex_unlock( &mdb_tool_index_mutex );
505 return mdb_index_entry_add( op, txn, e );
509 ID mdb_tool_entry_put(
512 struct berval *text )
515 struct mdb_info *mdb;
519 assert( be != NULL );
520 assert( slapMode & SLAP_TOOL_MODE );
522 assert( text != NULL );
523 assert( text->bv_val != NULL );
524 assert( text->bv_val[0] == '\0' ); /* overconservative? */
526 Debug( LDAP_DEBUG_TRACE, "=> " LDAP_XSTRING(mdb_tool_entry_put)
527 "( %ld, \"%s\" )\n", (long) e->e_id, e->e_dn, 0 );
529 mdb = (struct mdb_info *) be->be_private;
532 rc = mdb_txn_begin( mdb->mi_dbenv, 0, &txn );
534 snprintf( text->bv_val, text->bv_len,
535 "txn_begin failed: %s (%d)",
536 mdb_strerror(rc), rc );
537 Debug( LDAP_DEBUG_ANY,
538 "=> " LDAP_XSTRING(mdb_tool_entry_put) ": %s\n",
539 text->bv_val, 0, 0 );
546 op.o_tmpmemctx = NULL;
547 op.o_tmpmfuncs = &ch_mfuncs;
549 /* add dn2id indices */
550 rc = mdb_tool_next_id( &op, txn, e, text, 0 );
555 rc = mdb_tool_index_add( &op, txn, e );
557 snprintf( text->bv_val, text->bv_len,
558 "index_entry_add failed: err=%d", rc );
559 Debug( LDAP_DEBUG_ANY,
560 "=> " LDAP_XSTRING(mdb_tool_entry_put) ": %s\n",
561 text->bv_val, 0, 0 );
567 rc = mdb_id2entry_add( &op, txn, e );
569 snprintf( text->bv_val, text->bv_len,
570 "id2entry_add failed: err=%d", rc );
571 Debug( LDAP_DEBUG_ANY,
572 "=> " LDAP_XSTRING(mdb_tool_entry_put) ": %s\n",
573 text->bv_val, 0, 0 );
580 if ( mdb_writes >= mdb_writes_per_commit ) {
581 rc = mdb_txn_commit( txn );
585 snprintf( text->bv_val, text->bv_len,
586 "txn_commit 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 );
596 mdb_txn_abort( txn );
598 snprintf( text->bv_val, text->bv_len,
599 "txn_aborted! %s (%d)",
600 rc == LDAP_OTHER ? "Internal error" :
601 mdb_strerror(rc), rc );
602 Debug( LDAP_DEBUG_ANY,
603 "=> " LDAP_XSTRING(mdb_tool_entry_put) ": %s\n",
604 text->bv_val, 0, 0 );
611 int mdb_tool_entry_reindex(
614 AttributeDescription **adv )
616 struct mdb_info *mi = (struct mdb_info *) be->be_private;
622 Debug( LDAP_DEBUG_ARGS,
623 "=> " LDAP_XSTRING(mdb_tool_entry_reindex) "( %ld )\n",
625 assert( tool_base == NULL );
626 assert( tool_filter == NULL );
628 /* No indexes configured, nothing to do. Could return an
629 * error here to shortcut things.
635 /* Check for explicit list of attrs to index */
639 if ( mi->mi_attrs[0]->ai_desc != adv[0] ) {
641 for ( n = 0; adv[n]; n++ ) ;
644 for ( i = 0; i < n; i++ ) {
645 AttributeDescription *ad = adv[i];
646 for ( j = i-1; j>=0; j--) {
647 if ( SLAP_PTRCMP( adv[j], ad ) <= 0 ) break;
654 for ( i = 0; adv[i]; i++ ) {
655 if ( mi->mi_attrs[i]->ai_desc != adv[i] ) {
656 for ( j = i+1; j < mi->mi_nattrs; j++ ) {
657 if ( mi->mi_attrs[j]->ai_desc == adv[i] ) {
658 AttrInfo *ai = mi->mi_attrs[i];
659 mi->mi_attrs[i] = mi->mi_attrs[j];
660 mi->mi_attrs[j] = ai;
664 if ( j == mi->mi_nattrs ) {
665 Debug( LDAP_DEBUG_ANY,
666 LDAP_XSTRING(mdb_tool_entry_reindex)
667 ": no index configured for %s\n",
668 adv[i]->ad_cname.bv_val, 0, 0 );
676 e = mdb_tool_entry_get( be, id );
679 Debug( LDAP_DEBUG_ANY,
680 LDAP_XSTRING(mdb_tool_entry_reindex)
681 ": could not locate id=%ld\n",
687 rc = mdb_txn_begin( mi->mi_dbenv, 0, &txi );
689 Debug( LDAP_DEBUG_ANY,
690 "=> " LDAP_XSTRING(mdb_tool_entry_reindex) ": "
691 "txn_begin failed: %s (%d)\n",
692 mdb_strerror(rc), rc, 0 );
698 * just (re)add them for now
699 * assume that some other routine (not yet implemented)
700 * will zap index databases
704 Debug( LDAP_DEBUG_TRACE,
705 "=> " LDAP_XSTRING(mdb_tool_entry_reindex) "( %ld, \"%s\" )\n",
706 (long) id, e->e_dn, 0 );
710 op.o_tmpmemctx = NULL;
711 op.o_tmpmfuncs = &ch_mfuncs;
713 rc = mdb_tool_index_add( &op, txi, e );
718 if ( mdb_writes >= mdb_writes_per_commit ) {
719 rc = mdb_txn_commit( txi );
721 Debug( LDAP_DEBUG_ANY,
722 "=> " LDAP_XSTRING(mdb_tool_entry_reindex)
723 ": txn_commit failed: %s (%d)\n",
724 mdb_strerror(rc), rc, 0 );
731 mdb_txn_abort( txi );
732 Debug( LDAP_DEBUG_ANY,
733 "=> " LDAP_XSTRING(mdb_tool_entry_reindex)
734 ": txn_aborted! err=%d\n",
739 mdb_entry_release( &op, e, 0 );
744 ID mdb_tool_entry_modify(
747 struct berval *text )
750 struct mdb_info *mdb;
755 assert( be != NULL );
756 assert( slapMode & SLAP_TOOL_MODE );
758 assert( text != NULL );
759 assert( text->bv_val != NULL );
760 assert( text->bv_val[0] == '\0' ); /* overconservative? */
762 assert ( e->e_id != NOID );
764 Debug( LDAP_DEBUG_TRACE,
765 "=> " LDAP_XSTRING(mdb_tool_entry_modify) "( %ld, \"%s\" )\n",
766 (long) e->e_id, e->e_dn, 0 );
768 mdb = (struct mdb_info *) be->be_private;
771 mdb_cursor_close( cursor );
774 rc = mdb_txn_begin( mdb->mi_dbenv, 0, &tid );
776 snprintf( text->bv_val, text->bv_len,
777 "txn_begin failed: %s (%d)",
778 mdb_strerror(rc), rc );
779 Debug( LDAP_DEBUG_ANY,
780 "=> " LDAP_XSTRING(mdb_tool_entry_modify) ": %s\n",
781 text->bv_val, 0, 0 );
787 op.o_tmpmemctx = NULL;
788 op.o_tmpmfuncs = &ch_mfuncs;
791 rc = mdb_id2entry_update( &op, tid, e );
793 snprintf( text->bv_val, text->bv_len,
794 "id2entry_update failed: err=%d", rc );
795 Debug( LDAP_DEBUG_ANY,
796 "=> " LDAP_XSTRING(mdb_tool_entry_modify) ": %s\n",
797 text->bv_val, 0, 0 );
803 rc = mdb_txn_commit( tid );
805 snprintf( text->bv_val, text->bv_len,
806 "txn_commit failed: %s (%d)",
807 mdb_strerror(rc), rc );
808 Debug( LDAP_DEBUG_ANY,
809 "=> " LDAP_XSTRING(mdb_tool_entry_modify) ": "
810 "%s\n", text->bv_val, 0, 0 );
815 mdb_txn_abort( tid );
816 snprintf( text->bv_val, text->bv_len,
817 "txn_aborted! %s (%d)",
818 mdb_strerror(rc), rc );
819 Debug( LDAP_DEBUG_ANY,
820 "=> " LDAP_XSTRING(mdb_tool_entry_modify) ": %s\n",
821 text->bv_val, 0, 0 );
830 mdb_tool_index_task( void *ctx, void *ptr )
832 int base = *(int *)ptr;
836 ldap_pvt_thread_mutex_lock( &mdb_tool_index_mutex );
837 mdb_tool_index_tcount--;
838 if ( !mdb_tool_index_tcount )
839 ldap_pvt_thread_cond_signal( &mdb_tool_index_cond_main );
840 ldap_pvt_thread_cond_wait( &mdb_tool_index_cond_work,
841 &mdb_tool_index_mutex );
842 if ( slapd_shutdown ) {
843 mdb_tool_index_tcount--;
844 if ( !mdb_tool_index_tcount )
845 ldap_pvt_thread_cond_signal( &mdb_tool_index_cond_main );
846 ldap_pvt_thread_mutex_unlock( &mdb_tool_index_mutex );
849 ldap_pvt_thread_mutex_unlock( &mdb_tool_index_mutex );
851 mdb_tool_index_threads[base] = mdb_index_recrun( mdb_tool_ix_op,
852 mdb_tool_info, mdb_tool_index_rec, mdb_tool_ix_id, base );