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 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 );
203 if (( rc = mdb_txn_commit( mdb_tool_txn ))) {
204 Debug( LDAP_DEBUG_ANY,
205 LDAP_XSTRING(mdb_tool_entry_close) ": database %s: "
206 "txn_commit failed: %s (%d)\n",
207 be->be_suffix[0].bv_val, mdb_strerror(rc), rc );
215 fprintf( stderr, "Error, entries missing!\n");
216 for (i=0; i<nholes; i++) {
217 fprintf(stderr, " entry %ld: %s\n",
218 holes[i].id, holes[i].dn.bv_val);
228 mdb_tool_entry_first_x(
238 return mdb_tool_entry_next( be );
241 ID mdb_tool_entry_next(
246 struct mdb_info *mdb;
248 assert( be != NULL );
249 assert( slapMode & SLAP_TOOL_MODE );
251 mdb = (struct mdb_info *) be->be_private;
252 assert( mdb != NULL );
254 if ( !mdb_tool_txn ) {
255 rc = mdb_txn_begin( mdb->mi_dbenv, NULL, MDB_RDONLY, &mdb_tool_txn );
258 rc = mdb_cursor_open( mdb_tool_txn, mdb->mi_id2entry, &cursor );
260 mdb_txn_abort( mdb_tool_txn );
266 rc = mdb_cursor_get( cursor, &key, &data, MDB_NEXT );
272 previd = *(ID *)key.mv_data;
278 if ( tool_filter || tool_base ) {
279 static Operation op = {0};
280 static Opheader ohdr = {0};
284 op.o_tmpmemctx = NULL;
285 op.o_tmpmfuncs = &ch_mfuncs;
287 if ( tool_next_entry ) {
288 mdb_entry_release( &op, tool_next_entry, 0 );
289 tool_next_entry = NULL;
292 rc = mdb_tool_entry_get_int( be, id, &tool_next_entry );
293 if ( rc == LDAP_NO_SUCH_OBJECT ) {
297 assert( tool_next_entry != NULL );
299 if ( tool_filter && test_filter( NULL, tool_next_entry, tool_filter ) != LDAP_COMPARE_TRUE )
301 mdb_entry_release( &op, tool_next_entry, 0 );
302 tool_next_entry = NULL;
310 ID mdb_tool_dn2id_get(
315 struct mdb_info *mdb;
321 if ( BER_BVISEMPTY(dn) )
324 mdb = (struct mdb_info *) be->be_private;
326 if ( !mdb_tool_txn ) {
327 rc = mdb_txn_begin( mdb->mi_dbenv, NULL, (slapMode & SLAP_TOOL_READONLY) != 0 ?
328 MDB_RDONLY : 0, &mdb_tool_txn );
335 op.o_tmpmemctx = NULL;
336 op.o_tmpmfuncs = &ch_mfuncs;
338 rc = mdb_dn2id( &op, mdb_tool_txn, NULL, dn, &id, NULL, NULL, NULL );
339 if ( rc == MDB_NOTFOUND )
346 mdb_tool_entry_get_int( BackendDB *be, ID id, Entry **ep )
352 struct berval dn = BER_BVNULL, ndn = BER_BVNULL;
355 assert( be != NULL );
356 assert( slapMode & SLAP_TOOL_MODE );
358 if ( ( tool_filter || tool_base ) && id == previd && tool_next_entry != NULL ) {
359 *ep = tool_next_entry;
360 tool_next_entry = NULL;
364 if ( id != previd ) {
365 key.mv_size = sizeof(ID);
367 rc = mdb_cursor_get( cursor, &key, &data, MDB_SET );
373 if ( !data.mv_size ) {
374 rc = LDAP_NO_SUCH_OBJECT;
380 op.o_tmpmemctx = NULL;
381 op.o_tmpmfuncs = &ch_mfuncs;
382 if ( slapMode & SLAP_TOOL_READONLY ) {
383 rc = mdb_id2name( &op, mdb_tool_txn, &idcursor, id, &dn, &ndn );
388 if ( tool_base != NULL ) {
389 if ( !dnIsSuffixScope( &ndn, tool_base, tool_scope ) ) {
390 ch_free( dn.bv_val );
391 ch_free( ndn.bv_val );
392 rc = LDAP_NO_SUCH_OBJECT;
397 rc = mdb_entry_decode( &op, mdb_tool_txn, &data, &e );
399 if ( !BER_BVISNULL( &dn )) {
403 e->e_name.bv_val = NULL;
404 e->e_nname.bv_val = NULL;
416 mdb_tool_entry_get( BackendDB *be, ID id )
421 if ( !mdb_tool_txn ) {
422 struct mdb_info *mdb = (struct mdb_info *) be->be_private;
423 rc = mdb_txn_begin( mdb->mi_dbenv, NULL,
424 (slapMode & SLAP_TOOL_READONLY) ? MDB_RDONLY : 0, &mdb_tool_txn );
429 struct mdb_info *mdb = (struct mdb_info *) be->be_private;
430 rc = mdb_cursor_open( mdb_tool_txn, mdb->mi_id2entry, &cursor );
432 mdb_txn_abort( mdb_tool_txn );
437 (void)mdb_tool_entry_get_int( be, id, &e );
441 static int mdb_tool_next_id(
448 struct mdb_info *mdb = (struct mdb_info *) op->o_bd->be_private;
449 struct berval dn = e->e_name;
450 struct berval ndn = e->e_nname;
451 struct berval pdn, npdn, nmatched;
455 if (ndn.bv_len == 0) {
460 rc = mdb_dn2id( op, tid, mcp, &ndn, &id, NULL, NULL, &nmatched );
461 if ( rc == MDB_NOTFOUND ) {
462 if ( !be_issuffix( op->o_bd, &ndn ) ) {
464 dnParent( &ndn, &npdn );
465 if ( nmatched.bv_len != npdn.bv_len ) {
466 dnParent( &dn, &pdn );
469 rc = mdb_tool_next_id( op, tid, e, text, 1 );
475 /* If parent didn't exist, it was created just now
476 * and its ID is now in e->e_id. Make sure the current
477 * entry gets added under the new parent ID.
479 if ( eid != e->e_id ) {
486 rc = mdb_next_id( op->o_bd, idcursor, &e->e_id );
488 snprintf( text->bv_val, text->bv_len,
489 "next_id failed: %s (%d)",
490 mdb_strerror(rc), rc );
491 Debug( LDAP_DEBUG_ANY,
492 "=> mdb_tool_next_id: %s\n", text->bv_val, 0, 0 );
495 rc = mdb_dn2id_add( op, mcp, mcd, pid, 1, 1, e );
497 snprintf( text->bv_val, text->bv_len,
498 "dn2id_add failed: %s (%d)",
499 mdb_strerror(rc), rc );
500 Debug( LDAP_DEBUG_ANY,
501 "=> mdb_tool_next_id: %s\n", text->bv_val, 0, 0 );
504 if ( nholes == nhmax - 1 ) {
505 if ( holes == hbuf ) {
506 holes = ch_malloc( nhmax * sizeof(dn_id) * 2 );
507 AC_MEMCPY( holes, hbuf, sizeof(hbuf) );
509 holes = ch_realloc( holes, nhmax * sizeof(dn_id) * 2 );
513 ber_dupbv( &holes[nholes].dn, &ndn );
514 holes[nholes++].id = e->e_id;
515 key.mv_size = sizeof(ID);
516 key.mv_data = &e->e_id;
519 rc = mdb_cursor_put( idcursor, &key, &data, MDB_NOOVERWRITE );
520 if ( rc == MDB_KEYEXIST )
523 snprintf( text->bv_val, text->bv_len,
524 "dummy id2entry add failed: %s (%d)",
525 mdb_strerror(rc), rc );
526 Debug( LDAP_DEBUG_ANY,
527 "=> mdb_tool_next_id: %s\n", text->bv_val, 0, 0 );
530 } else if ( !hole ) {
535 for ( i=0; i<nholes; i++) {
536 if ( holes[i].id == e->e_id ) {
537 free(holes[i].dn.bv_val);
538 for (j=i;j<nholes;j++) holes[j] = holes[j+1];
542 } else if ( holes[i].id > e->e_id ) {
556 struct mdb_info *mdb = (struct mdb_info *) op->o_bd->be_private;
558 if ( !mdb->mi_nattrs )
561 if ( mdb_tool_threads > 1 ) {
566 ir = mdb_tool_index_rec;
567 for (i=0; i<mdb->mi_nattrs; i++)
568 ir[i].ir_attrs = NULL;
570 for ( a = e->e_attrs; a != NULL; a = a->a_next ) {
571 rc = mdb_index_recset( mdb, a, a->a_desc->ad_type,
572 &a->a_desc->ad_tags, ir );
576 for (i=0; i<mdb->mi_nattrs; i++) {
579 rc = mdb_cursor_open( txn, ir[i].ir_ai->ai_dbi,
580 &ir[i].ir_ai->ai_cursor );
584 mdb_tool_ix_id = e->e_id;
585 mdb_tool_ix_txn = txn;
586 ldap_pvt_thread_mutex_lock( &mdb_tool_index_mutex );
587 /* Wait for all threads to be ready */
588 while ( mdb_tool_index_tcount ) {
589 ldap_pvt_thread_cond_wait( &mdb_tool_index_cond_main,
590 &mdb_tool_index_mutex );
593 for ( i=1; i<mdb_tool_threads; i++ )
594 mdb_tool_index_rec[i].ir_i = LDAP_BUSY;
595 mdb_tool_index_tcount = mdb_tool_threads - 1;
596 ldap_pvt_thread_cond_broadcast( &mdb_tool_index_cond_work );
597 ldap_pvt_thread_mutex_unlock( &mdb_tool_index_mutex );
599 return mdb_index_recrun( op, txn, mdb, ir, e->e_id, 0 );
602 return mdb_index_entry_add( op, txn, e );
607 mdb_tool_index_finish()
610 ldap_pvt_thread_mutex_lock( &mdb_tool_index_mutex );
611 for ( i=1; i<mdb_tool_threads; i++ ) {
612 if ( mdb_tool_index_rec[i].ir_i == LDAP_BUSY ) {
613 ldap_pvt_thread_cond_wait( &mdb_tool_index_cond_main,
614 &mdb_tool_index_mutex );
618 if ( mdb_tool_index_rec[i].ir_i ) {
619 rc = mdb_tool_index_rec[i].ir_i;
623 ldap_pvt_thread_mutex_unlock( &mdb_tool_index_mutex );
627 ID mdb_tool_entry_put(
630 struct berval *text )
633 struct mdb_info *mdb;
637 assert( be != NULL );
638 assert( slapMode & SLAP_TOOL_MODE );
640 assert( text != NULL );
641 assert( text->bv_val != NULL );
642 assert( text->bv_val[0] == '\0' ); /* overconservative? */
644 Debug( LDAP_DEBUG_TRACE, "=> " LDAP_XSTRING(mdb_tool_entry_put)
645 "( %ld, \"%s\" )\n", (long) e->e_id, e->e_dn, 0 );
647 mdb = (struct mdb_info *) be->be_private;
649 if ( !mdb_tool_txn ) {
650 rc = mdb_txn_begin( mdb->mi_dbenv, NULL, 0, &mdb_tool_txn );
652 snprintf( text->bv_val, text->bv_len,
653 "txn_begin failed: %s (%d)",
654 mdb_strerror(rc), rc );
655 Debug( LDAP_DEBUG_ANY,
656 "=> " LDAP_XSTRING(mdb_tool_entry_put) ": %s\n",
657 text->bv_val, 0, 0 );
660 rc = mdb_cursor_open( mdb_tool_txn, mdb->mi_id2entry, &idcursor );
662 snprintf( text->bv_val, text->bv_len,
663 "cursor_open failed: %s (%d)",
664 mdb_strerror(rc), rc );
665 Debug( LDAP_DEBUG_ANY,
666 "=> " LDAP_XSTRING(mdb_tool_entry_put) ": %s\n",
667 text->bv_val, 0, 0 );
670 if ( !mdb->mi_nextid ) {
672 mdb_next_id( be, idcursor, &dummy );
674 rc = mdb_cursor_open( mdb_tool_txn, mdb->mi_dn2id, &mcp );
676 snprintf( text->bv_val, text->bv_len,
677 "cursor_open failed: %s (%d)",
678 mdb_strerror(rc), rc );
679 Debug( LDAP_DEBUG_ANY,
680 "=> " LDAP_XSTRING(mdb_tool_entry_put) ": %s\n",
681 text->bv_val, 0, 0 );
684 rc = mdb_cursor_open( mdb_tool_txn, mdb->mi_dn2id, &mcd );
686 snprintf( text->bv_val, text->bv_len,
687 "cursor_open failed: %s (%d)",
688 mdb_strerror(rc), rc );
689 Debug( LDAP_DEBUG_ANY,
690 "=> " LDAP_XSTRING(mdb_tool_entry_put) ": %s\n",
691 text->bv_val, 0, 0 );
698 op.o_tmpmemctx = NULL;
699 op.o_tmpmfuncs = &ch_mfuncs;
701 /* add dn2id indices */
702 rc = mdb_tool_next_id( &op, mdb_tool_txn, e, text, 0 );
707 if ( mdb_tool_threads > 1 ) {
708 LDAP_SLIST_INSERT_HEAD( &op.o_extra, &mdb_tool_axinfo[0]->ai_oe, oe_next );
710 rc = mdb_tool_index_add( &op, mdb_tool_txn, e );
712 snprintf( text->bv_val, text->bv_len,
713 "index_entry_add failed: err=%d", rc );
714 Debug( LDAP_DEBUG_ANY,
715 "=> " LDAP_XSTRING(mdb_tool_entry_put) ": %s\n",
716 text->bv_val, 0, 0 );
722 rc = mdb_id2entry_add( &op, mdb_tool_txn, idcursor, e );
724 snprintf( text->bv_val, text->bv_len,
725 "id2entry_add failed: err=%d", rc );
726 Debug( LDAP_DEBUG_ANY,
727 "=> " LDAP_XSTRING(mdb_tool_entry_put) ": %s\n",
728 text->bv_val, 0, 0 );
732 if( mdb->mi_nattrs && mdb_tool_threads > 1 )
733 rc = mdb_tool_index_finish();
738 if ( mdb_writes >= mdb_writes_per_commit ) {
740 MDB_TOOL_IDL_FLUSH( be, mdb_tool_txn );
741 rc = mdb_txn_commit( mdb_tool_txn );
742 for ( i=0; i<mdb->mi_nattrs; i++ )
743 mdb->mi_attrs[i]->ai_cursor = NULL;
748 snprintf( text->bv_val, text->bv_len,
749 "txn_commit failed: %s (%d)",
750 mdb_strerror(rc), rc );
751 Debug( LDAP_DEBUG_ANY,
752 "=> " LDAP_XSTRING(mdb_tool_entry_put) ": %s\n",
753 text->bv_val, 0, 0 );
760 mdb_txn_abort( mdb_tool_txn );
763 for ( i=0; i<mdb->mi_nattrs; i++ )
764 mdb->mi_attrs[i]->ai_cursor = NULL;
766 snprintf( text->bv_val, text->bv_len,
767 "txn_aborted! %s (%d)",
768 rc == LDAP_OTHER ? "Internal error" :
769 mdb_strerror(rc), rc );
770 Debug( LDAP_DEBUG_ANY,
771 "=> " LDAP_XSTRING(mdb_tool_entry_put) ": %s\n",
772 text->bv_val, 0, 0 );
779 static int mdb_dn2id_upgrade( BackendDB *be );
781 int mdb_tool_entry_reindex(
784 AttributeDescription **adv )
786 struct mdb_info *mi = (struct mdb_info *) be->be_private;
792 Debug( LDAP_DEBUG_ARGS,
793 "=> " LDAP_XSTRING(mdb_tool_entry_reindex) "( %ld )\n",
795 assert( tool_base == NULL );
796 assert( tool_filter == NULL );
798 /* Special: do a dn2id upgrade */
799 if ( adv && adv[0] == slap_schema.si_ad_entryDN ) {
800 /* short-circuit tool_entry_next() */
801 mdb_cursor_get( cursor, &key, &data, MDB_LAST );
802 return mdb_dn2id_upgrade( be );
805 /* No indexes configured, nothing to do. Could return an
806 * error here to shortcut things.
812 /* Check for explicit list of attrs to index */
816 if ( mi->mi_attrs[0]->ai_desc != adv[0] ) {
818 for ( n = 0; adv[n]; n++ ) ;
821 for ( i = 0; i < n; i++ ) {
822 AttributeDescription *ad = adv[i];
823 for ( j = i-1; j>=0; j--) {
824 if ( SLAP_PTRCMP( adv[j], ad ) <= 0 ) break;
831 for ( i = 0; adv[i]; i++ ) {
832 if ( mi->mi_attrs[i]->ai_desc != adv[i] ) {
833 for ( j = i+1; j < mi->mi_nattrs; j++ ) {
834 if ( mi->mi_attrs[j]->ai_desc == adv[i] ) {
835 AttrInfo *ai = mi->mi_attrs[i];
836 mi->mi_attrs[i] = mi->mi_attrs[j];
837 mi->mi_attrs[j] = ai;
841 if ( j == mi->mi_nattrs ) {
842 Debug( LDAP_DEBUG_ANY,
843 LDAP_XSTRING(mdb_tool_entry_reindex)
844 ": no index configured for %s\n",
845 adv[i]->ad_cname.bv_val, 0, 0 );
853 e = mdb_tool_entry_get( be, id );
856 Debug( LDAP_DEBUG_ANY,
857 LDAP_XSTRING(mdb_tool_entry_reindex)
858 ": could not locate id=%ld\n",
864 rc = mdb_txn_begin( mi->mi_dbenv, NULL, 0, &txi );
866 Debug( LDAP_DEBUG_ANY,
867 "=> " LDAP_XSTRING(mdb_tool_entry_reindex) ": "
868 "txn_begin failed: %s (%d)\n",
869 mdb_strerror(rc), rc, 0 );
874 if ( slapMode & SLAP_TRUNCATE_MODE ) {
876 for ( i=0; i < mi->mi_nattrs; i++ ) {
877 rc = mdb_drop( txi, mi->mi_attrs[i]->ai_dbi, 0 );
879 Debug( LDAP_DEBUG_ANY,
880 LDAP_XSTRING(mdb_tool_entry_reindex)
881 ": (Truncate) mdb_drop(%s) failed: %s (%d)\n",
882 mi->mi_attrs[i]->ai_desc->ad_type->sat_cname.bv_val,
883 mdb_strerror(rc), rc );
887 slapMode ^= SLAP_TRUNCATE_MODE;
891 * just (re)add them for now
892 * Use truncate mode to empty/reset index databases
895 Debug( LDAP_DEBUG_TRACE,
896 "=> " LDAP_XSTRING(mdb_tool_entry_reindex) "( %ld )\n",
901 op.o_tmpmemctx = NULL;
902 op.o_tmpmfuncs = &ch_mfuncs;
904 rc = mdb_tool_index_add( &op, txi, e );
909 if ( mdb_writes >= mdb_writes_per_commit ) {
912 MDB_TOOL_IDL_FLUSH( be, txi );
913 rc = mdb_txn_commit( txi );
915 for ( i=0; i<mi->mi_nattrs; i++ )
916 mi->mi_attrs[i]->ai_cursor = NULL;
918 Debug( LDAP_DEBUG_ANY,
919 "=> " LDAP_XSTRING(mdb_tool_entry_reindex)
920 ": txn_commit failed: %s (%d)\n",
921 mdb_strerror(rc), rc, 0 );
924 mdb_cursor_close( cursor );
926 /* Must close the read txn to allow old pages to be reclaimed. */
927 mdb_txn_abort( mdb_tool_txn );
928 /* and then reopen it so that tool_entry_next still works. */
929 mdb_txn_begin( mi->mi_dbenv, NULL, MDB_RDONLY, &mdb_tool_txn );
930 mdb_cursor_open( mdb_tool_txn, mi->mi_id2entry, &cursor );
932 key.mv_size = sizeof(ID);
933 mdb_cursor_get( cursor, &key, NULL, MDB_SET );
939 mdb_cursor_close( cursor );
941 mdb_txn_abort( txi );
942 for ( i=0; i<mi->mi_nattrs; i++ )
943 mi->mi_attrs[i]->ai_cursor = NULL;
944 Debug( LDAP_DEBUG_ANY,
945 "=> " LDAP_XSTRING(mdb_tool_entry_reindex)
946 ": txn_aborted! err=%d\n",
951 mdb_entry_release( &op, e, 0 );
956 ID mdb_tool_entry_modify(
959 struct berval *text )
962 struct mdb_info *mdb;
966 assert( be != NULL );
967 assert( slapMode & SLAP_TOOL_MODE );
969 assert( text != NULL );
970 assert( text->bv_val != NULL );
971 assert( text->bv_val[0] == '\0' ); /* overconservative? */
973 assert ( e->e_id != NOID );
975 Debug( LDAP_DEBUG_TRACE,
976 "=> " LDAP_XSTRING(mdb_tool_entry_modify) "( %ld, \"%s\" )\n",
977 (long) e->e_id, e->e_dn, 0 );
979 mdb = (struct mdb_info *) be->be_private;
982 mdb_cursor_close( cursor );
985 if ( !mdb_tool_txn ) {
986 rc = mdb_txn_begin( mdb->mi_dbenv, NULL, 0, &mdb_tool_txn );
988 snprintf( text->bv_val, text->bv_len,
989 "txn_begin failed: %s (%d)",
990 mdb_strerror(rc), rc );
991 Debug( LDAP_DEBUG_ANY,
992 "=> " LDAP_XSTRING(mdb_tool_entry_modify) ": %s\n",
993 text->bv_val, 0, 0 );
1000 op.o_tmpmemctx = NULL;
1001 op.o_tmpmfuncs = &ch_mfuncs;
1003 /* id2entry index */
1004 rc = mdb_id2entry_update( &op, mdb_tool_txn, NULL, e );
1006 snprintf( text->bv_val, text->bv_len,
1007 "id2entry_update failed: err=%d", rc );
1008 Debug( LDAP_DEBUG_ANY,
1009 "=> " LDAP_XSTRING(mdb_tool_entry_modify) ": %s\n",
1010 text->bv_val, 0, 0 );
1016 rc = mdb_txn_commit( mdb_tool_txn );
1018 snprintf( text->bv_val, text->bv_len,
1019 "txn_commit failed: %s (%d)",
1020 mdb_strerror(rc), rc );
1021 Debug( LDAP_DEBUG_ANY,
1022 "=> " LDAP_XSTRING(mdb_tool_entry_modify) ": "
1023 "%s\n", text->bv_val, 0, 0 );
1028 mdb_txn_abort( mdb_tool_txn );
1029 snprintf( text->bv_val, text->bv_len,
1030 "txn_aborted! %s (%d)",
1031 mdb_strerror(rc), rc );
1032 Debug( LDAP_DEBUG_ANY,
1033 "=> " LDAP_XSTRING(mdb_tool_entry_modify) ": %s\n",
1034 text->bv_val, 0, 0 );
1037 mdb_tool_txn = NULL;
1044 mdb_tool_index_task( void *ctx, void *ptr )
1046 int base = *(int *)ptr;
1048 Opheader ohdr = {0};
1049 AttrIxInfo ai = {0}, *aio;
1053 op.o_bd = mdb_tool_ix_be;
1054 op.o_tmpmemctx = NULL;
1055 op.o_tmpmfuncs = &ch_mfuncs;
1056 aio = mdb_tool_axinfo[base];
1057 mdb_tool_axinfo[base] = &ai;
1058 LDAP_SLIST_INSERT_HEAD( &op.o_extra, &ai.ai_oe, oe_next );
1060 ldap_pvt_thread_mutex_lock( &mdb_tool_index_mutex );
1061 mdb_tool_index_tcount--;
1062 if ( !mdb_tool_index_tcount )
1063 ldap_pvt_thread_cond_signal( &mdb_tool_index_cond_main );
1064 ldap_pvt_thread_cond_wait( &mdb_tool_index_cond_work,
1065 &mdb_tool_index_mutex );
1066 if ( slapd_shutdown ) {
1067 mdb_tool_index_tcount--;
1068 if ( !mdb_tool_index_tcount )
1069 ldap_pvt_thread_cond_signal( &mdb_tool_index_cond_main );
1071 mdb_tool_axinfo[base] = aio;
1072 ldap_pvt_thread_mutex_unlock( &mdb_tool_index_mutex );
1075 ldap_pvt_thread_mutex_unlock( &mdb_tool_index_mutex );
1076 mdb_tool_index_rec[base].ir_i = mdb_index_recrun( &op,
1078 mdb_tool_info, mdb_tool_index_rec, mdb_tool_ix_id, base );
1084 #ifdef MDB_TOOL_IDL_CACHING
1086 mdb_tool_idl_cmp( const void *v1, const void *v2 )
1088 const mdb_tool_idl_cache *c1 = v1, *c2 = v2;
1091 if (( rc = c1->kstr.bv_len - c2->kstr.bv_len )) return rc;
1092 return memcmp( c1->kstr.bv_val, c2->kstr.bv_val, c1->kstr.bv_len );
1096 mdb_tool_idl_flush_one( MDB_cursor *mc, AttrIxInfo *ai, mdb_tool_idl_cache *ic )
1098 mdb_tool_idl_cache_entry *ice;
1099 MDB_val key, data[2];
1103 /* Freshly allocated, ignore it */
1104 if ( !ic->head && ic->count <= MDB_IDL_DB_SIZE ) {
1108 key.mv_data = ic->kstr.bv_val;
1109 key.mv_size = ic->kstr.bv_len;
1111 if ( ic->count > MDB_IDL_DB_SIZE ) {
1112 while ( ic->flags & WAS_FOUND ) {
1113 rc = mdb_cursor_get( mc, &key, data, MDB_SET );
1115 /* FIXME: find out why this happens */
1119 if ( ic->flags & WAS_RANGE ) {
1121 rc = mdb_cursor_get( mc, &key, data, MDB_NEXT_DUP );
1124 rc = mdb_cursor_get( mc, &key, data, MDB_NEXT_DUP );
1126 /* Store range hi */
1127 data[0].mv_data = &ic->last;
1128 rc = mdb_cursor_put( mc, &key, data, MDB_CURRENT );
1130 /* Delete old data, replace with range */
1131 ic->first = *(ID *)data[0].mv_data;
1132 mdb_cursor_del( mc, MDB_NODUPDATA );
1136 if ( !(ic->flags & WAS_RANGE)) {
1137 /* range, didn't exist before */
1139 data[0].mv_size = sizeof(ID);
1140 data[0].mv_data = &nid;
1141 rc = mdb_cursor_put( mc, &key, data, 0 );
1143 data[0].mv_data = &ic->first;
1144 rc = mdb_cursor_put( mc, &key, data, 0 );
1146 data[0].mv_data = &ic->last;
1147 rc = mdb_cursor_put( mc, &key, data, 0 );
1158 data[0].mv_size = sizeof(ID);
1160 for ( ice = ic->head, n=0; ice; ice = ice->next, n++ ) {
1165 end = (ic->count-ic->offset) & (IDBLOCK-1);
1169 data[1].mv_size = end;
1170 data[0].mv_data = ice->ids;
1171 rc = mdb_cursor_put( mc, &key, data, MDB_APPENDDUP|MDB_MULTIPLE );
1178 ic->tail->next = ai->ai_flist;
1179 ai->ai_flist = ic->head;
1182 ic->head = ai->ai_clist;
1188 mdb_tool_idl_flush_db( MDB_txn *txn, AttrInfo *ai, AttrIxInfo *ax )
1194 mdb_cursor_open( txn, ai->ai_dbi, &mc );
1195 root = tavl_end( ai->ai_root, TAVL_DIR_LEFT );
1197 rc = mdb_tool_idl_flush_one( mc, ax, root->avl_data );
1200 } while ((root = tavl_next(root, TAVL_DIR_RIGHT)));
1201 mdb_cursor_close( mc );
1207 mdb_tool_idl_flush( BackendDB *be, MDB_txn *txn )
1209 struct mdb_info *mdb = (struct mdb_info *) be->be_private;
1211 unsigned int i, dbi;
1213 for ( i=0; i < mdb->mi_nattrs; i++ ) {
1214 if ( !mdb->mi_attrs[i]->ai_root ) continue;
1215 rc = mdb_tool_idl_flush_db( txn, mdb->mi_attrs[i], mdb_tool_axinfo[i % mdb_tool_threads] );
1216 tavl_free(mdb->mi_attrs[i]->ai_root, NULL);
1217 mdb->mi_attrs[i]->ai_root = NULL;
1224 int mdb_tool_idl_add(
1227 struct berval *keys,
1231 mdb_tool_idl_cache *ic, itmp;
1232 mdb_tool_idl_cache_entry *ice;
1234 AttrIxInfo *ax = (AttrIxInfo *)mc;
1235 AttrInfo *ai = (AttrInfo *)ax->ai_ai;
1239 for (i=0; keys[i].bv_val; i++) {
1240 itmp.kstr = keys[i];
1241 ic = tavl_find( (Avlnode *)ai->ai_root, &itmp, mdb_tool_idl_cmp );
1243 /* No entry yet, create one */
1249 if ( ax->ai_clist ) {
1251 ax->ai_clist = ic->head;
1253 ic = ch_malloc( sizeof( mdb_tool_idl_cache ) + itmp.kstr.bv_len + 4 );
1255 ic->kstr.bv_len = itmp.kstr.bv_len;
1256 ic->kstr.bv_val = (char *)(ic+1);
1257 memcpy( ic->kstr.bv_val, itmp.kstr.bv_val, ic->kstr.bv_len );
1258 ic->head = ic->tail = NULL;
1263 tavl_insert( (Avlnode **)&ai->ai_root, ic, mdb_tool_idl_cmp,
1266 /* load existing key count here */
1267 key.mv_size = keys[i].bv_len;
1268 key.mv_data = keys[i].bv_val;
1269 rc = mdb_cursor_get( mc, &key, &data, MDB_SET );
1271 ic->flags |= WAS_FOUND;
1272 nid = *(ID *)data.mv_data;
1274 ic->count = MDB_IDL_DB_SIZE+1;
1275 ic->flags |= WAS_RANGE;
1279 mdb_cursor_count( mc, &count );
1282 ic->offset = count & (IDBLOCK-1);
1286 /* are we a range already? */
1287 if ( ic->count > MDB_IDL_DB_SIZE ) {
1290 /* Are we at the limit, and converting to a range? */
1291 } else if ( ic->count == MDB_IDL_DB_SIZE ) {
1293 ic->tail->next = ax->ai_flist;
1294 ax->ai_flist = ic->head;
1296 ic->head = ic->tail = NULL;
1301 /* No free block, create that too */
1302 lcount = (ic->count-ic->offset) & (IDBLOCK-1);
1303 if ( !ic->tail || lcount == 0) {
1304 if ( ax->ai_flist ) {
1306 ax->ai_flist = ice->next;
1308 ice = ch_malloc( sizeof( mdb_tool_idl_cache_entry ));
1314 ic->tail->next = ice;
1318 ice->ids[lcount-1] = 0;
1323 if (!lcount || ice->ids[lcount-1] != id) {
1324 ice->ids[lcount] = id;
1331 #endif /* MDB_TOOL_IDL_CACHING */
1333 /* Upgrade from pre 2.4.34 dn2id format */
1335 #include <ac/unistd.h>
1336 #include <lutil_meter.h>
1338 #define STACKSIZ 2048
1340 typedef struct rec {
1347 mdb_dn2id_upgrade( BackendDB *be ) {
1348 struct mdb_info *mi = (struct mdb_info *) be->be_private;
1350 MDB_cursor *mc = NULL;
1353 int rc, writes=0, depth=0;
1354 int enable_meter = 0;
1355 ID id = 0, *num, count = 0;
1357 lutil_meter_t meter;
1359 if (!(mi->mi_flags & MDB_NEED_UPGRADE)) {
1360 Debug( LDAP_DEBUG_ANY, "database %s: No upgrade needed.\n",
1361 be->be_suffix[0].bv_val, 0, 0 );
1368 mdb_stat(mdb_cursor_txn(cursor), mi->mi_dbis[MDB_ID2ENTRY], &st);
1369 if (!st.ms_entries) {
1370 /* Empty DB, nothing to upgrade? */
1374 enable_meter = !lutil_meter_open(&meter,
1375 &lutil_meter_text_display,
1376 &lutil_meter_linear_estimator,
1380 num = ch_malloc(STACKSIZ * (sizeof(ID) + sizeof(rec)));
1381 stack = (rec *)(num + STACKSIZ);
1383 rc = mdb_txn_begin(mi->mi_dbenv, NULL, 0, &mt);
1385 Debug(LDAP_DEBUG_ANY, "mdb_dn2id_upgrade: mdb_txn_begin failed, %s (%d)\n",
1386 mdb_strerror(rc), rc, 0 );
1389 rc = mdb_cursor_open(mt, mi->mi_dbis[MDB_DN2ID], &mc);
1391 Debug(LDAP_DEBUG_ANY, "mdb_dn2id_upgrade: mdb_cursor_open failed, %s (%d)\n",
1392 mdb_strerror(rc), rc, 0 );
1396 key.mv_size = sizeof(ID);
1397 /* post-order depth-first update */
1404 stack[depth].id = id;
1405 rc = mdb_cursor_get(mc, &key, &data, MDB_SET);
1407 Debug(LDAP_DEBUG_ANY, "mdb_dn2id_upgrade: mdb_cursor_get failed, %s (%d)\n",
1408 mdb_strerror(rc), rc, 0 );
1413 rc = mdb_cursor_count(mc, &dkids);
1415 Debug(LDAP_DEBUG_ANY, "mdb_dn2id_upgrade: mdb_cursor_count failed, %s (%d)\n",
1416 mdb_strerror(rc), rc, 0 );
1420 rc = mdb_cursor_get(mc, &key, &data, MDB_NEXT_DUP);
1422 ptr = (unsigned char *)data.mv_data + data.mv_size - sizeof(ID);
1423 memcpy(&id, ptr, sizeof(ID));
1425 memcpy(stack[depth].rdn, data.mv_data, data.mv_size);
1426 stack[depth].len = data.mv_size;
1431 /* pop: write updated count, advance to next node */
1433 /* update superior counts */
1435 num[depth-1] += num[depth];
1438 id = stack[depth-1].id;
1439 data.mv_data = stack[depth].rdn;
1440 data.mv_size = stack[depth].len;
1441 rc = mdb_cursor_get(mc, &key, &data, MDB_GET_BOTH);
1443 Debug(LDAP_DEBUG_ANY, "mdb_dn2id_upgrade: mdb_cursor_get(BOTH) failed, %s (%d)\n",
1444 mdb_strerror(rc), rc, 0 );
1447 data.mv_data = stack[depth].rdn;
1448 ptr = (unsigned char *)data.mv_data + data.mv_size;
1449 memcpy(ptr, &num[depth], sizeof(ID));
1450 data.mv_size += sizeof(ID);
1451 rc = mdb_cursor_del(mc, 0);
1453 Debug(LDAP_DEBUG_ANY, "mdb_dn2id_upgrade: mdb_cursor_del failed, %s (%d)\n",
1454 mdb_strerror(rc), rc, 0 );
1457 rc = mdb_cursor_put(mc, &key, &data, 0);
1459 Debug(LDAP_DEBUG_ANY, "mdb_dn2id_upgrade: mdb_cursor_put failed, %s (%d)\n",
1460 mdb_strerror(rc), rc, 0 );
1466 lutil_meter_update(&meter, count, 0);
1471 len = (ptr[0] & 0x7f) << 8 | ptr[1];
1472 printf("ID: %zu, %zu, %.*s\n", stack[depth].id, num[depth], len, ptr+2);
1476 if (writes == 1000) {
1477 mdb_cursor_close(mc);
1478 rc = mdb_txn_commit(mt);
1480 Debug(LDAP_DEBUG_ANY, "mdb_dn2id_upgrade: mdb_txn_commit failed, %s (%d)\n",
1481 mdb_strerror(rc), rc, 0 );
1484 rc = mdb_txn_begin(mi->mi_dbenv, NULL, 0, &mt);
1486 Debug(LDAP_DEBUG_ANY, "mdb_dn2id_upgrade: mdb_txn_begin(2) failed, %s (%d)\n",
1487 mdb_strerror(rc), rc, 0 );
1490 rc = mdb_cursor_open(mt, mi->mi_dbis[MDB_DN2ID], &mc);
1492 Debug(LDAP_DEBUG_ANY, "mdb_dn2id_upgrade: mdb_cursor_open(2) failed, %s (%d)\n",
1493 mdb_strerror(rc), rc, 0 );
1496 rc = mdb_cursor_get(mc, &key, &data, MDB_GET_BOTH);
1498 Debug(LDAP_DEBUG_ANY, "mdb_dn2id_upgrade: mdb_cursor_get(2) failed, %s (%d)\n",
1499 mdb_strerror(rc), rc, 0 );
1506 rc = mdb_cursor_get(mc, &key, &data, MDB_NEXT_DUP);
1516 mdb_cursor_close(mc);
1519 r2 = mdb_txn_commit(mt);
1521 Debug(LDAP_DEBUG_ANY, "mdb_dn2id_upgrade: mdb_txn_commit(2) failed, %s (%d)\n",
1522 mdb_strerror(r2), r2, 0 );
1529 lutil_meter_update(&meter, count, 1);
1530 lutil_meter_close(&meter);