]> git.sur5r.net Git - openldap/blob - servers/slapd/back-bdb/tools.c
Use a thread for LDIF parsing in slapadd -q
[openldap] / servers / slapd / back-bdb / tools.c
1 /* tools.c - tools for slap tools */
2 /* $OpenLDAP$ */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4  *
5  * Copyright 2000-2011 The OpenLDAP Foundation.
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted only as authorized by the OpenLDAP
10  * Public License.
11  *
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>.
15  */
16
17 #include "portable.h"
18
19 #include <stdio.h>
20 #include <ac/string.h>
21 #include <ac/errno.h>
22
23 #define AVL_INTERNAL
24 #include "back-bdb.h"
25 #include "idl.h"
26
27 static DBC *cursor = NULL;
28 static DBT key, data;
29 static EntryHeader eh;
30 static ID nid, previd = NOID;
31 static char ehbuf[16];
32
33 typedef struct dn_id {
34         ID id;
35         struct berval dn;
36 } dn_id;
37
38 #define HOLE_SIZE       4096
39 static dn_id hbuf[HOLE_SIZE], *holes = hbuf;
40 static unsigned nhmax = HOLE_SIZE;
41 static unsigned nholes;
42
43 static int index_nattrs;
44
45 static struct berval    *tool_base;
46 static int              tool_scope;
47 static Filter           *tool_filter;
48 static Entry            *tool_next_entry;
49
50 #ifdef BDB_TOOL_IDL_CACHING
51 #define bdb_tool_idl_cmp                BDB_SYMBOL(tool_idl_cmp)
52 #define bdb_tool_idl_flush_one          BDB_SYMBOL(tool_idl_flush_one)
53 #define bdb_tool_idl_flush              BDB_SYMBOL(tool_idl_flush)
54
55 static int bdb_tool_idl_flush( BackendDB *be );
56
57 #define IDBLOCK 1024
58
59 typedef struct bdb_tool_idl_cache_entry {
60         struct bdb_tool_idl_cache_entry *next;
61         ID ids[IDBLOCK];
62 } bdb_tool_idl_cache_entry;
63  
64 typedef struct bdb_tool_idl_cache {
65         struct berval kstr;
66         bdb_tool_idl_cache_entry *head, *tail;
67         ID first, last;
68         int count;
69 } bdb_tool_idl_cache;
70
71 static bdb_tool_idl_cache_entry *bdb_tool_idl_free_list;
72 #endif  /* BDB_TOOL_IDL_CACHING */
73
74 static ID bdb_tool_ix_id;
75 static Operation *bdb_tool_ix_op;
76 static int *bdb_tool_index_threads, bdb_tool_index_tcount;
77 static void *bdb_tool_index_rec;
78 static struct bdb_info *bdb_tool_info;
79 static ldap_pvt_thread_mutex_t bdb_tool_index_mutex;
80 static ldap_pvt_thread_cond_t bdb_tool_index_cond_main;
81 static ldap_pvt_thread_cond_t bdb_tool_index_cond_work;
82
83 #if DB_VERSION_FULL >= 0x04060000
84 #define USE_TRICKLE     1
85 #else
86 /* Seems to slow things down too much in BDB 4.5 */
87 #undef USE_TRICKLE
88 #endif
89
90 #ifdef USE_TRICKLE
91 static ldap_pvt_thread_mutex_t bdb_tool_trickle_mutex;
92 static ldap_pvt_thread_cond_t bdb_tool_trickle_cond;
93 static ldap_pvt_thread_cond_t bdb_tool_trickle_cond_end;
94
95 static void * bdb_tool_trickle_task( void *ctx, void *ptr );
96 static int bdb_tool_trickle_active;
97 #endif
98
99 static void * bdb_tool_index_task( void *ctx, void *ptr );
100
101 static int
102 bdb_tool_entry_get_int( BackendDB *be, ID id, Entry **ep );
103
104 static int bdb_tool_threads;
105
106 int bdb_tool_entry_open(
107         BackendDB *be, int mode )
108 {
109         struct bdb_info *bdb = (struct bdb_info *) be->be_private;
110
111         /* initialize key and data thangs */
112         DBTzero( &key );
113         DBTzero( &data );
114         key.flags = DB_DBT_USERMEM;
115         key.data = &nid;
116         key.size = key.ulen = sizeof( nid );
117         data.flags = DB_DBT_USERMEM;
118
119         if (cursor == NULL) {
120                 int rc = bdb->bi_id2entry->bdi_db->cursor(
121                         bdb->bi_id2entry->bdi_db, bdb->bi_cache.c_txn, &cursor,
122                         bdb->bi_db_opflags );
123                 if( rc != 0 ) {
124                         return -1;
125                 }
126         }
127
128         /* Set up for threaded slapindex */
129         if (( slapMode & (SLAP_TOOL_QUICK|SLAP_TOOL_READONLY)) == SLAP_TOOL_QUICK ) {
130                 if ( !bdb_tool_info ) {
131 #ifdef USE_TRICKLE
132                         ldap_pvt_thread_mutex_init( &bdb_tool_trickle_mutex );
133                         ldap_pvt_thread_cond_init( &bdb_tool_trickle_cond );
134                         ldap_pvt_thread_cond_init( &bdb_tool_trickle_cond_end );
135                         ldap_pvt_thread_pool_submit( &connection_pool, bdb_tool_trickle_task, bdb->bi_dbenv );
136 #endif
137
138                         ldap_pvt_thread_mutex_init( &bdb_tool_index_mutex );
139                         ldap_pvt_thread_cond_init( &bdb_tool_index_cond_main );
140                         ldap_pvt_thread_cond_init( &bdb_tool_index_cond_work );
141                         if ( bdb->bi_nattrs ) {
142                                 int i;
143                                 bdb_tool_threads = slap_tool_thread_max - 1;
144                                 bdb_tool_index_threads = ch_malloc( bdb_tool_threads * sizeof( int ));
145                                 bdb_tool_index_rec = ch_malloc( bdb->bi_nattrs * sizeof( IndexRec ));
146                                 bdb_tool_index_tcount = bdb_tool_threads - 1;
147                                 for (i=1; i<bdb_tool_threads; i++) {
148                                         int *ptr = ch_malloc( sizeof( int ));
149                                         *ptr = i;
150                                         ldap_pvt_thread_pool_submit( &connection_pool,
151                                                 bdb_tool_index_task, ptr );
152                                 }
153                         }
154                         bdb_tool_info = bdb;
155                 }
156         }
157
158         return 0;
159 }
160
161 int bdb_tool_entry_close(
162         BackendDB *be )
163 {
164         if ( bdb_tool_info ) {
165                 slapd_shutdown = 1;
166 #ifdef USE_TRICKLE
167                 ldap_pvt_thread_mutex_lock( &bdb_tool_trickle_mutex );
168
169                 /* trickle thread may not have started yet */
170                 while ( !bdb_tool_trickle_active )
171                         ldap_pvt_thread_cond_wait( &bdb_tool_trickle_cond_end,
172                                         &bdb_tool_trickle_mutex );
173
174                 ldap_pvt_thread_cond_signal( &bdb_tool_trickle_cond );
175                 while ( bdb_tool_trickle_active )
176                         ldap_pvt_thread_cond_wait( &bdb_tool_trickle_cond_end,
177                                         &bdb_tool_trickle_mutex );
178                 ldap_pvt_thread_mutex_unlock( &bdb_tool_trickle_mutex );
179 #endif
180                 ldap_pvt_thread_mutex_lock( &bdb_tool_index_mutex );
181
182                 /* There might still be some threads starting */
183                 while ( bdb_tool_index_tcount ) {
184                         ldap_pvt_thread_cond_wait( &bdb_tool_index_cond_main,
185                                         &bdb_tool_index_mutex );
186                 }
187
188                 bdb_tool_index_tcount = bdb_tool_threads - 1;
189                 ldap_pvt_thread_cond_broadcast( &bdb_tool_index_cond_work );
190
191                 /* Make sure all threads are stopped */
192                 while ( bdb_tool_index_tcount ) {
193                         ldap_pvt_thread_cond_wait( &bdb_tool_index_cond_main,
194                                 &bdb_tool_index_mutex );
195                 }
196                 ldap_pvt_thread_mutex_unlock( &bdb_tool_index_mutex );
197
198                 bdb_tool_info = NULL;
199                 slapd_shutdown = 0;
200                 ch_free( bdb_tool_index_threads );
201                 ch_free( bdb_tool_index_rec );
202                 bdb_tool_index_tcount = bdb_tool_threads - 1;
203         }
204
205         if( eh.bv.bv_val ) {
206                 ch_free( eh.bv.bv_val );
207                 eh.bv.bv_val = NULL;
208         }
209
210         if( cursor ) {
211                 cursor->c_close( cursor );
212                 cursor = NULL;
213         }
214
215 #ifdef BDB_TOOL_IDL_CACHING
216         bdb_tool_idl_flush( be );
217 #endif
218
219         if( nholes ) {
220                 unsigned i;
221                 fprintf( stderr, "Error, entries missing!\n");
222                 for (i=0; i<nholes; i++) {
223                         fprintf(stderr, "  entry %ld: %s\n",
224                                 holes[i].id, holes[i].dn.bv_val);
225                 }
226                 return -1;
227         }
228                         
229         return 0;
230 }
231
232 ID
233 bdb_tool_entry_first_x(
234         BackendDB *be,
235         struct berval *base,
236         int scope,
237         Filter *f )
238 {
239         tool_base = base;
240         tool_scope = scope;
241         tool_filter = f;
242         
243         return bdb_tool_entry_next( be );
244 }
245
246 ID bdb_tool_entry_next(
247         BackendDB *be )
248 {
249         int rc;
250         ID id;
251         struct bdb_info *bdb;
252
253         assert( be != NULL );
254         assert( slapMode & SLAP_TOOL_MODE );
255
256         bdb = (struct bdb_info *) be->be_private;
257         assert( bdb != NULL );
258
259 next:;
260         /* Get the header */
261         data.ulen = data.dlen = sizeof( ehbuf );
262         data.data = ehbuf;
263         data.flags |= DB_DBT_PARTIAL;
264         rc = cursor->c_get( cursor, &key, &data, DB_NEXT );
265
266         if( rc ) {
267                 /* If we're doing linear indexing and there are more attrs to
268                  * index, and we're at the end of the database, start over.
269                  */
270                 if ( index_nattrs && rc == DB_NOTFOUND ) {
271                         /* optional - do a checkpoint here? */
272                         bdb_attr_info_free( bdb->bi_attrs[0] );
273                         bdb->bi_attrs[0] = bdb->bi_attrs[index_nattrs];
274                         index_nattrs--;
275                         rc = cursor->c_get( cursor, &key, &data, DB_FIRST );
276                         if ( rc ) {
277                                 return NOID;
278                         }
279                 } else {
280                         return NOID;
281                 }
282         }
283
284         BDB_DISK2ID( key.data, &id );
285         previd = id;
286
287         if ( tool_filter || tool_base ) {
288                 static Operation op = {0};
289                 static Opheader ohdr = {0};
290
291                 op.o_hdr = &ohdr;
292                 op.o_bd = be;
293                 op.o_tmpmemctx = NULL;
294                 op.o_tmpmfuncs = &ch_mfuncs;
295
296                 if ( tool_next_entry ) {
297                         bdb_entry_release( &op, tool_next_entry, 0 );
298                         tool_next_entry = NULL;
299                 }
300
301                 rc = bdb_tool_entry_get_int( be, id, &tool_next_entry );
302                 if ( rc == LDAP_NO_SUCH_OBJECT ) {
303                         goto next;
304                 }
305
306                 assert( tool_next_entry != NULL );
307
308 #ifdef BDB_HIER
309                 /* TODO: needed until BDB_HIER is handled accordingly
310                  * in bdb_tool_entry_get_int() */
311                 if ( tool_base && !dnIsSuffixScope( &tool_next_entry->e_nname, tool_base, tool_scope ) )
312                 {
313                         bdb_entry_release( &op, tool_next_entry, 0 );
314                         tool_next_entry = NULL;
315                         goto next;
316                 }
317 #endif
318
319                 if ( tool_filter && test_filter( NULL, tool_next_entry, tool_filter ) != LDAP_COMPARE_TRUE )
320                 {
321                         bdb_entry_release( &op, tool_next_entry, 0 );
322                         tool_next_entry = NULL;
323                         goto next;
324                 }
325         }
326
327         return id;
328 }
329
330 ID bdb_tool_dn2id_get(
331         Backend *be,
332         struct berval *dn
333 )
334 {
335         Operation op = {0};
336         Opheader ohdr = {0};
337         EntryInfo *ei = NULL;
338         int rc;
339
340         if ( BER_BVISEMPTY(dn) )
341                 return 0;
342
343         op.o_hdr = &ohdr;
344         op.o_bd = be;
345         op.o_tmpmemctx = NULL;
346         op.o_tmpmfuncs = &ch_mfuncs;
347
348         rc = bdb_cache_find_ndn( &op, 0, dn, &ei );
349         if ( ei ) bdb_cache_entryinfo_unlock( ei );
350         if ( rc == DB_NOTFOUND )
351                 return NOID;
352         
353         return ei->bei_id;
354 }
355
356 static int
357 bdb_tool_entry_get_int( BackendDB *be, ID id, Entry **ep )
358 {
359         Entry *e = NULL;
360         char *dptr;
361         int rc, eoff;
362
363         assert( be != NULL );
364         assert( slapMode & SLAP_TOOL_MODE );
365
366         if ( ( tool_filter || tool_base ) && id == previd && tool_next_entry != NULL ) {
367                 *ep = tool_next_entry;
368                 tool_next_entry = NULL;
369                 return LDAP_SUCCESS;
370         }
371
372         if ( id != previd ) {
373                 data.ulen = data.dlen = sizeof( ehbuf );
374                 data.data = ehbuf;
375                 data.flags |= DB_DBT_PARTIAL;
376
377                 BDB_ID2DISK( id, &nid );
378                 rc = cursor->c_get( cursor, &key, &data, DB_SET );
379                 if ( rc ) {
380                         rc = LDAP_OTHER;
381                         goto done;
382                 }
383         }
384
385         /* Get the header */
386         dptr = eh.bv.bv_val;
387         eh.bv.bv_val = ehbuf;
388         eh.bv.bv_len = data.size;
389         rc = entry_header( &eh );
390         eoff = eh.data - eh.bv.bv_val;
391         eh.bv.bv_val = dptr;
392         if ( rc ) {
393                 rc = LDAP_OTHER;
394                 goto done;
395         }
396
397         /* Get the size */
398         data.flags &= ~DB_DBT_PARTIAL;
399         data.ulen = 0;
400         rc = cursor->c_get( cursor, &key, &data, DB_CURRENT );
401         if ( rc != DB_BUFFER_SMALL ) {
402                 rc = LDAP_OTHER;
403                 goto done;
404         }
405
406         /* Allocate a block and retrieve the data */
407         eh.bv.bv_len = eh.nvals * sizeof( struct berval ) + data.size;
408         eh.bv.bv_val = ch_realloc( eh.bv.bv_val, eh.bv.bv_len );
409         eh.data = eh.bv.bv_val + eh.nvals * sizeof( struct berval );
410         data.data = eh.data;
411         data.ulen = data.size;
412
413         /* Skip past already parsed nattr/nvals */
414         eh.data += eoff;
415
416         rc = cursor->c_get( cursor, &key, &data, DB_CURRENT );
417         if ( rc ) {
418                 rc = LDAP_OTHER;
419                 goto done;
420         }
421
422 #ifndef BDB_HIER
423         /* TODO: handle BDB_HIER accordingly */
424         if ( tool_base != NULL ) {
425                 struct berval ndn;
426                 entry_decode_dn( &eh, NULL, &ndn );
427
428                 if ( !dnIsSuffixScope( &ndn, tool_base, tool_scope ) ) {
429                         return LDAP_NO_SUCH_OBJECT;
430                 }
431         }
432 #endif
433
434 #ifdef SLAP_ZONE_ALLOC
435         /* FIXME: will add ctx later */
436         rc = entry_decode( &eh, &e, NULL );
437 #else
438         rc = entry_decode( &eh, &e );
439 #endif
440
441         if( rc == LDAP_SUCCESS ) {
442                 e->e_id = id;
443 #ifdef BDB_HIER
444                 if ( slapMode & SLAP_TOOL_READONLY ) {
445                         struct bdb_info *bdb = (struct bdb_info *) be->be_private;
446                         EntryInfo *ei = NULL;
447                         Operation op = {0};
448                         Opheader ohdr = {0};
449
450                         op.o_hdr = &ohdr;
451                         op.o_bd = be;
452                         op.o_tmpmemctx = NULL;
453                         op.o_tmpmfuncs = &ch_mfuncs;
454
455                         rc = bdb_cache_find_parent( &op, bdb->bi_cache.c_txn, id, &ei );
456                         if ( rc == LDAP_SUCCESS ) {
457                                 bdb_cache_entryinfo_unlock( ei );
458                                 e->e_private = ei;
459                                 ei->bei_e = e;
460                                 bdb_fix_dn( e, 0 );
461                                 ei->bei_e = NULL;
462                                 e->e_private = NULL;
463                         }
464                 }
465 #endif
466         }
467 done:
468         if ( e != NULL ) {
469                 *ep = e;
470         }
471
472         return rc;
473 }
474
475 Entry*
476 bdb_tool_entry_get( BackendDB *be, ID id )
477 {
478         Entry *e = NULL;
479
480         (void)bdb_tool_entry_get_int( be, id, &e );
481         return e;
482 }
483
484 static int bdb_tool_next_id(
485         Operation *op,
486         DB_TXN *tid,
487         Entry *e,
488         struct berval *text,
489         int hole )
490 {
491         struct berval dn = e->e_name;
492         struct berval ndn = e->e_nname;
493         struct berval pdn, npdn;
494         EntryInfo *ei = NULL, eidummy;
495         int rc;
496
497         if (ndn.bv_len == 0) {
498                 e->e_id = 0;
499                 return 0;
500         }
501
502         rc = bdb_cache_find_ndn( op, tid, &ndn, &ei );
503         if ( ei ) bdb_cache_entryinfo_unlock( ei );
504         if ( rc == DB_NOTFOUND ) {
505                 if ( !be_issuffix( op->o_bd, &ndn ) ) {
506                         ID eid = e->e_id;
507                         dnParent( &dn, &pdn );
508                         dnParent( &ndn, &npdn );
509                         e->e_name = pdn;
510                         e->e_nname = npdn;
511                         rc = bdb_tool_next_id( op, tid, e, text, 1 );
512                         e->e_name = dn;
513                         e->e_nname = ndn;
514                         if ( rc ) {
515                                 return rc;
516                         }
517                         /* If parent didn't exist, it was created just now
518                          * and its ID is now in e->e_id. Make sure the current
519                          * entry gets added under the new parent ID.
520                          */
521                         if ( eid != e->e_id ) {
522                                 eidummy.bei_id = e->e_id;
523                                 ei = &eidummy;
524                         }
525                 }
526                 rc = bdb_next_id( op->o_bd, &e->e_id );
527                 if ( rc ) {
528                         snprintf( text->bv_val, text->bv_len,
529                                 "next_id failed: %s (%d)",
530                                 db_strerror(rc), rc );
531                 Debug( LDAP_DEBUG_ANY,
532                         "=> bdb_tool_next_id: %s\n", text->bv_val, 0, 0 );
533                         return rc;
534                 }
535                 rc = bdb_dn2id_add( op, tid, ei, e );
536                 if ( rc ) {
537                         snprintf( text->bv_val, text->bv_len, 
538                                 "dn2id_add failed: %s (%d)",
539                                 db_strerror(rc), rc );
540                 Debug( LDAP_DEBUG_ANY,
541                         "=> bdb_tool_next_id: %s\n", text->bv_val, 0, 0 );
542                 } else if ( hole ) {
543                         if ( nholes == nhmax - 1 ) {
544                                 if ( holes == hbuf ) {
545                                         holes = ch_malloc( nhmax * sizeof(dn_id) * 2 );
546                                         AC_MEMCPY( holes, hbuf, sizeof(hbuf) );
547                                 } else {
548                                         holes = ch_realloc( holes, nhmax * sizeof(dn_id) * 2 );
549                                 }
550                                 nhmax *= 2;
551                         }
552                         ber_dupbv( &holes[nholes].dn, &ndn );
553                         holes[nholes++].id = e->e_id;
554                 }
555         } else if ( !hole ) {
556                 unsigned i, j;
557
558                 e->e_id = ei->bei_id;
559
560                 for ( i=0; i<nholes; i++) {
561                         if ( holes[i].id == e->e_id ) {
562                                 free(holes[i].dn.bv_val);
563                                 for (j=i;j<nholes;j++) holes[j] = holes[j+1];
564                                 holes[j].id = 0;
565                                 nholes--;
566                                 break;
567                         } else if ( holes[i].id > e->e_id ) {
568                                 break;
569                         }
570                 }
571         }
572         return rc;
573 }
574
575 static int
576 bdb_tool_index_add(
577         Operation *op,
578         DB_TXN *txn,
579         Entry *e )
580 {
581         struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
582
583         if ( !bdb->bi_nattrs )
584                 return 0;
585
586         if ( slapMode & SLAP_TOOL_QUICK ) {
587                 IndexRec *ir;
588                 int i, rc;
589                 Attribute *a;
590                 
591                 ir = bdb_tool_index_rec;
592                 memset(ir, 0, bdb->bi_nattrs * sizeof( IndexRec ));
593
594                 for ( a = e->e_attrs; a != NULL; a = a->a_next ) {
595                         rc = bdb_index_recset( bdb, a, a->a_desc->ad_type, 
596                                 &a->a_desc->ad_tags, ir );
597                         if ( rc )
598                                 return rc;
599                 }
600                 bdb_tool_ix_id = e->e_id;
601                 bdb_tool_ix_op = op;
602                 ldap_pvt_thread_mutex_lock( &bdb_tool_index_mutex );
603                 /* Wait for all threads to be ready */
604                 while ( bdb_tool_index_tcount ) {
605                         ldap_pvt_thread_cond_wait( &bdb_tool_index_cond_main, 
606                                 &bdb_tool_index_mutex );
607                 }
608                 for ( i=1; i<bdb_tool_threads; i++ )
609                         bdb_tool_index_threads[i] = LDAP_BUSY;
610                 bdb_tool_index_tcount = bdb_tool_threads - 1;
611                 ldap_pvt_thread_cond_broadcast( &bdb_tool_index_cond_work );
612                 ldap_pvt_thread_mutex_unlock( &bdb_tool_index_mutex );
613                 rc = bdb_index_recrun( op, bdb, ir, e->e_id, 0 );
614                 if ( rc )
615                         return rc;
616                 ldap_pvt_thread_mutex_lock( &bdb_tool_index_mutex );
617                 for ( i=1; i<bdb_tool_threads; i++ ) {
618                         if ( bdb_tool_index_threads[i] == LDAP_BUSY ) {
619                                 ldap_pvt_thread_cond_wait( &bdb_tool_index_cond_main, 
620                                         &bdb_tool_index_mutex );
621                                 i--;
622                                 continue;
623                         }
624                         if ( bdb_tool_index_threads[i] ) {
625                                 rc = bdb_tool_index_threads[i];
626                                 break;
627                         }
628                 }
629                 ldap_pvt_thread_mutex_unlock( &bdb_tool_index_mutex );
630                 return rc;
631         } else {
632                 return bdb_index_entry_add( op, txn, e );
633         }
634 }
635
636 ID bdb_tool_entry_put(
637         BackendDB *be,
638         Entry *e,
639         struct berval *text )
640 {
641         int rc;
642         struct bdb_info *bdb;
643         DB_TXN *tid = NULL;
644         Operation op = {0};
645         Opheader ohdr = {0};
646
647         assert( be != NULL );
648         assert( slapMode & SLAP_TOOL_MODE );
649
650         assert( text != NULL );
651         assert( text->bv_val != NULL );
652         assert( text->bv_val[0] == '\0' );      /* overconservative? */
653
654         Debug( LDAP_DEBUG_TRACE, "=> " LDAP_XSTRING(bdb_tool_entry_put)
655                 "( %ld, \"%s\" )\n", (long) e->e_id, e->e_dn, 0 );
656
657         bdb = (struct bdb_info *) be->be_private;
658
659         if (! (slapMode & SLAP_TOOL_QUICK)) {
660         rc = TXN_BEGIN( bdb->bi_dbenv, NULL, &tid, 
661                 bdb->bi_db_opflags );
662         if( rc != 0 ) {
663                 snprintf( text->bv_val, text->bv_len,
664                         "txn_begin failed: %s (%d)",
665                         db_strerror(rc), rc );
666                 Debug( LDAP_DEBUG_ANY,
667                         "=> " LDAP_XSTRING(bdb_tool_entry_put) ": %s\n",
668                          text->bv_val, 0, 0 );
669                 return NOID;
670         }
671         }
672
673         op.o_hdr = &ohdr;
674         op.o_bd = be;
675         op.o_tmpmemctx = NULL;
676         op.o_tmpmfuncs = &ch_mfuncs;
677
678         /* add dn2id indices */
679         rc = bdb_tool_next_id( &op, tid, e, text, 0 );
680         if( rc != 0 ) {
681                 goto done;
682         }
683
684 #ifdef USE_TRICKLE
685         if (( slapMode & SLAP_TOOL_QUICK ) && (( e->e_id & 0xfff ) == 0xfff )) {
686                 ldap_pvt_thread_cond_signal( &bdb_tool_trickle_cond );
687         }
688 #endif
689
690         if ( !bdb->bi_linear_index )
691                 rc = bdb_tool_index_add( &op, tid, e );
692         if( rc != 0 ) {
693                 snprintf( text->bv_val, text->bv_len,
694                                 "index_entry_add failed: %s (%d)",
695                                 rc == LDAP_OTHER ? "Internal error" :
696                                 db_strerror(rc), rc );
697                 Debug( LDAP_DEBUG_ANY,
698                         "=> " LDAP_XSTRING(bdb_tool_entry_put) ": %s\n",
699                         text->bv_val, 0, 0 );
700                 goto done;
701         }
702
703         /* id2entry index */
704         rc = bdb_id2entry_add( be, tid, e );
705         if( rc != 0 ) {
706                 snprintf( text->bv_val, text->bv_len,
707                                 "id2entry_add failed: %s (%d)",
708                                 db_strerror(rc), rc );
709                 Debug( LDAP_DEBUG_ANY,
710                         "=> " LDAP_XSTRING(bdb_tool_entry_put) ": %s\n",
711                         text->bv_val, 0, 0 );
712                 goto done;
713         }
714
715 done:
716         if( rc == 0 ) {
717                 if ( !( slapMode & SLAP_TOOL_QUICK )) {
718                 rc = TXN_COMMIT( tid, 0 );
719                 if( rc != 0 ) {
720                         snprintf( text->bv_val, text->bv_len,
721                                         "txn_commit failed: %s (%d)",
722                                         db_strerror(rc), rc );
723                         Debug( LDAP_DEBUG_ANY,
724                                 "=> " LDAP_XSTRING(bdb_tool_entry_put) ": %s\n",
725                                 text->bv_val, 0, 0 );
726                         e->e_id = NOID;
727                 }
728                 }
729
730         } else {
731                 if ( !( slapMode & SLAP_TOOL_QUICK )) {
732                 TXN_ABORT( tid );
733                 snprintf( text->bv_val, text->bv_len,
734                         "txn_aborted! %s (%d)",
735                         rc == LDAP_OTHER ? "Internal error" :
736                         db_strerror(rc), rc );
737                 Debug( LDAP_DEBUG_ANY,
738                         "=> " LDAP_XSTRING(bdb_tool_entry_put) ": %s\n",
739                         text->bv_val, 0, 0 );
740                 }
741                 e->e_id = NOID;
742         }
743
744         return e->e_id;
745 }
746
747 int bdb_tool_entry_reindex(
748         BackendDB *be,
749         ID id,
750         AttributeDescription **adv )
751 {
752         struct bdb_info *bi = (struct bdb_info *) be->be_private;
753         int rc;
754         Entry *e;
755         DB_TXN *tid = NULL;
756         Operation op = {0};
757         Opheader ohdr = {0};
758
759         Debug( LDAP_DEBUG_ARGS,
760                 "=> " LDAP_XSTRING(bdb_tool_entry_reindex) "( %ld )\n",
761                 (long) id, 0, 0 );
762         assert( tool_base == NULL );
763         assert( tool_filter == NULL );
764
765         /* No indexes configured, nothing to do. Could return an
766          * error here to shortcut things.
767          */
768         if (!bi->bi_attrs) {
769                 return 0;
770         }
771
772         /* Check for explicit list of attrs to index */
773         if ( adv ) {
774                 int i, j, n;
775
776                 if ( bi->bi_attrs[0]->ai_desc != adv[0] ) {
777                         /* count */
778                         for ( n = 0; adv[n]; n++ ) ;
779
780                         /* insertion sort */
781                         for ( i = 0; i < n; i++ ) {
782                                 AttributeDescription *ad = adv[i];
783                                 for ( j = i-1; j>=0; j--) {
784                                         if ( SLAP_PTRCMP( adv[j], ad ) <= 0 ) break;
785                                         adv[j+1] = adv[j];
786                                 }
787                                 adv[j+1] = ad;
788                         }
789                 }
790
791                 for ( i = 0; adv[i]; i++ ) {
792                         if ( bi->bi_attrs[i]->ai_desc != adv[i] ) {
793                                 for ( j = i+1; j < bi->bi_nattrs; j++ ) {
794                                         if ( bi->bi_attrs[j]->ai_desc == adv[i] ) {
795                                                 AttrInfo *ai = bi->bi_attrs[i];
796                                                 bi->bi_attrs[i] = bi->bi_attrs[j];
797                                                 bi->bi_attrs[j] = ai;
798                                                 break;
799                                         }
800                                 }
801                                 if ( j == bi->bi_nattrs ) {
802                                         Debug( LDAP_DEBUG_ANY,
803                                                 LDAP_XSTRING(bdb_tool_entry_reindex)
804                                                 ": no index configured for %s\n",
805                                                 adv[i]->ad_cname.bv_val, 0, 0 );
806                                         return -1;
807                                 }
808                         }
809                 }
810                 bi->bi_nattrs = i;
811         }
812
813         /* Get the first attribute to index */
814         if (bi->bi_linear_index && !index_nattrs) {
815                 index_nattrs = bi->bi_nattrs - 1;
816                 bi->bi_nattrs = 1;
817         }
818
819         e = bdb_tool_entry_get( be, id );
820
821         if( e == NULL ) {
822                 Debug( LDAP_DEBUG_ANY,
823                         LDAP_XSTRING(bdb_tool_entry_reindex)
824                         ": could not locate id=%ld\n",
825                         (long) id, 0, 0 );
826                 return -1;
827         }
828
829         if (! (slapMode & SLAP_TOOL_QUICK)) {
830         rc = TXN_BEGIN( bi->bi_dbenv, NULL, &tid, bi->bi_db_opflags );
831         if( rc != 0 ) {
832                 Debug( LDAP_DEBUG_ANY,
833                         "=> " LDAP_XSTRING(bdb_tool_entry_reindex) ": "
834                         "txn_begin failed: %s (%d)\n",
835                         db_strerror(rc), rc, 0 );
836                 goto done;
837         }
838         }
839         
840         /*
841          * just (re)add them for now
842          * assume that some other routine (not yet implemented)
843          * will zap index databases
844          *
845          */
846
847         Debug( LDAP_DEBUG_TRACE,
848                 "=> " LDAP_XSTRING(bdb_tool_entry_reindex) "( %ld, \"%s\" )\n",
849                 (long) id, e->e_dn, 0 );
850
851         op.o_hdr = &ohdr;
852         op.o_bd = be;
853         op.o_tmpmemctx = NULL;
854         op.o_tmpmfuncs = &ch_mfuncs;
855
856         rc = bdb_tool_index_add( &op, tid, e );
857
858 done:
859         if( rc == 0 ) {
860                 if (! (slapMode & SLAP_TOOL_QUICK)) {
861                 rc = TXN_COMMIT( tid, 0 );
862                 if( rc != 0 ) {
863                         Debug( LDAP_DEBUG_ANY,
864                                 "=> " LDAP_XSTRING(bdb_tool_entry_reindex)
865                                 ": txn_commit failed: %s (%d)\n",
866                                 db_strerror(rc), rc, 0 );
867                         e->e_id = NOID;
868                 }
869                 }
870
871         } else {
872                 if (! (slapMode & SLAP_TOOL_QUICK)) {
873                 TXN_ABORT( tid );
874                 Debug( LDAP_DEBUG_ANY,
875                         "=> " LDAP_XSTRING(bdb_tool_entry_reindex)
876                         ": txn_aborted! %s (%d)\n",
877                         db_strerror(rc), rc, 0 );
878                 }
879                 e->e_id = NOID;
880         }
881         bdb_entry_release( &op, e, 0 );
882
883         return rc;
884 }
885
886 ID bdb_tool_entry_modify(
887         BackendDB *be,
888         Entry *e,
889         struct berval *text )
890 {
891         int rc;
892         struct bdb_info *bdb;
893         DB_TXN *tid = NULL;
894         Operation op = {0};
895         Opheader ohdr = {0};
896
897         assert( be != NULL );
898         assert( slapMode & SLAP_TOOL_MODE );
899
900         assert( text != NULL );
901         assert( text->bv_val != NULL );
902         assert( text->bv_val[0] == '\0' );      /* overconservative? */
903
904         assert ( e->e_id != NOID );
905
906         Debug( LDAP_DEBUG_TRACE,
907                 "=> " LDAP_XSTRING(bdb_tool_entry_modify) "( %ld, \"%s\" )\n",
908                 (long) e->e_id, e->e_dn, 0 );
909
910         bdb = (struct bdb_info *) be->be_private;
911
912         if (! (slapMode & SLAP_TOOL_QUICK)) {
913                 if( cursor ) {
914                         cursor->c_close( cursor );
915                         cursor = NULL;
916                 }
917                 rc = TXN_BEGIN( bdb->bi_dbenv, NULL, &tid, 
918                         bdb->bi_db_opflags );
919                 if( rc != 0 ) {
920                         snprintf( text->bv_val, text->bv_len,
921                                 "txn_begin failed: %s (%d)",
922                                 db_strerror(rc), rc );
923                         Debug( LDAP_DEBUG_ANY,
924                                 "=> " LDAP_XSTRING(bdb_tool_entry_modify) ": %s\n",
925                                  text->bv_val, 0, 0 );
926                         return NOID;
927                 }
928         }
929
930         op.o_hdr = &ohdr;
931         op.o_bd = be;
932         op.o_tmpmemctx = NULL;
933         op.o_tmpmfuncs = &ch_mfuncs;
934
935         /* id2entry index */
936         rc = bdb_id2entry_update( be, tid, e );
937         if( rc != 0 ) {
938                 snprintf( text->bv_val, text->bv_len,
939                                 "id2entry_add failed: %s (%d)",
940                                 db_strerror(rc), rc );
941                 Debug( LDAP_DEBUG_ANY,
942                         "=> " LDAP_XSTRING(bdb_tool_entry_modify) ": %s\n",
943                         text->bv_val, 0, 0 );
944                 goto done;
945         }
946
947 done:
948         if( rc == 0 ) {
949                 if (! (slapMode & SLAP_TOOL_QUICK)) {
950                 rc = TXN_COMMIT( tid, 0 );
951                 if( rc != 0 ) {
952                         snprintf( text->bv_val, text->bv_len,
953                                         "txn_commit failed: %s (%d)",
954                                         db_strerror(rc), rc );
955                         Debug( LDAP_DEBUG_ANY,
956                                 "=> " LDAP_XSTRING(bdb_tool_entry_modify) ": "
957                                 "%s\n", text->bv_val, 0, 0 );
958                         e->e_id = NOID;
959                 }
960                 }
961
962         } else {
963                 if (! (slapMode & SLAP_TOOL_QUICK)) {
964                 TXN_ABORT( tid );
965                 snprintf( text->bv_val, text->bv_len,
966                         "txn_aborted! %s (%d)",
967                         db_strerror(rc), rc );
968                 Debug( LDAP_DEBUG_ANY,
969                         "=> " LDAP_XSTRING(bdb_tool_entry_modify) ": %s\n",
970                         text->bv_val, 0, 0 );
971                 }
972                 e->e_id = NOID;
973         }
974
975         return e->e_id;
976 }
977
978 #ifdef BDB_TOOL_IDL_CACHING
979 static int
980 bdb_tool_idl_cmp( const void *v1, const void *v2 )
981 {
982         const bdb_tool_idl_cache *c1 = v1, *c2 = v2;
983         int rc;
984
985         if (( rc = c1->kstr.bv_len - c2->kstr.bv_len )) return rc;
986         return memcmp( c1->kstr.bv_val, c2->kstr.bv_val, c1->kstr.bv_len );
987 }
988
989 static int
990 bdb_tool_idl_flush_one( void *v1, void *arg )
991 {
992         bdb_tool_idl_cache *ic = v1;
993         DB *db = arg;
994         struct bdb_info *bdb = bdb_tool_info;
995         bdb_tool_idl_cache_entry *ice;
996         DBC *curs;
997         DBT key, data;
998         int i, rc;
999         ID id, nid;
1000
1001         /* Freshly allocated, ignore it */
1002         if ( !ic->head && ic->count <= BDB_IDL_DB_SIZE ) {
1003                 return 0;
1004         }
1005
1006         rc = db->cursor( db, NULL, &curs, 0 );
1007         if ( rc )
1008                 return -1;
1009
1010         DBTzero( &key );
1011         DBTzero( &data );
1012
1013         bv2DBT( &ic->kstr, &key );
1014
1015         data.size = data.ulen = sizeof( ID );
1016         data.flags = DB_DBT_USERMEM;
1017         data.data = &nid;
1018
1019         rc = curs->c_get( curs, &key, &data, DB_SET );
1020         /* If key already exists and we're writing a range... */
1021         if ( rc == 0 && ic->count > BDB_IDL_DB_SIZE ) {
1022                 /* If it's not currently a range, must delete old info */
1023                 if ( nid ) {
1024                         /* Skip lo */
1025                         while ( curs->c_get( curs, &key, &data, DB_NEXT_DUP ) == 0 )
1026                                 curs->c_del( curs, 0 );
1027
1028                         nid = 0;
1029                         /* Store range marker */
1030                         curs->c_put( curs, &key, &data, DB_KEYFIRST );
1031                 } else {
1032                         
1033                         /* Skip lo */
1034                         rc = curs->c_get( curs, &key, &data, DB_NEXT_DUP );
1035
1036                         /* Get hi */
1037                         rc = curs->c_get( curs, &key, &data, DB_NEXT_DUP );
1038
1039                         /* Delete hi */
1040                         curs->c_del( curs, 0 );
1041                 }
1042                 BDB_ID2DISK( ic->last, &nid );
1043                 curs->c_put( curs, &key, &data, DB_KEYLAST );
1044                 rc = 0;
1045         } else if ( rc && rc != DB_NOTFOUND ) {
1046                 rc = -1;
1047         } else if ( ic->count > BDB_IDL_DB_SIZE ) {
1048                 /* range, didn't exist before */
1049                 nid = 0;
1050                 rc = curs->c_put( curs, &key, &data, DB_KEYLAST );
1051                 if ( rc == 0 ) {
1052                         BDB_ID2DISK( ic->first, &nid );
1053                         rc = curs->c_put( curs, &key, &data, DB_KEYLAST );
1054                         if ( rc == 0 ) {
1055                                 BDB_ID2DISK( ic->last, &nid );
1056                                 rc = curs->c_put( curs, &key, &data, DB_KEYLAST );
1057                         }
1058                 }
1059                 if ( rc ) {
1060                         rc = -1;
1061                 }
1062         } else {
1063                 int n;
1064
1065                 /* Just a normal write */
1066                 rc = 0;
1067                 for ( ice = ic->head, n=0; ice; ice = ice->next, n++ ) {
1068                         int end;
1069                         if ( ice->next ) {
1070                                 end = IDBLOCK;
1071                         } else {
1072                                 end = ic->count & (IDBLOCK-1);
1073                                 if ( !end )
1074                                         end = IDBLOCK;
1075                         }
1076                         for ( i=0; i<end; i++ ) {
1077                                 if ( !ice->ids[i] ) continue;
1078                                 BDB_ID2DISK( ice->ids[i], &nid );
1079                                 rc = curs->c_put( curs, &key, &data, DB_NODUPDATA );
1080                                 if ( rc ) {
1081                                         if ( rc == DB_KEYEXIST ) {
1082                                                 rc = 0;
1083                                                 continue;
1084                                         }
1085                                         rc = -1;
1086                                         break;
1087                                 }
1088                         }
1089                         if ( rc ) {
1090                                 rc = -1;
1091                                 break;
1092                         }
1093                 }
1094                 if ( ic->head ) {
1095                         ldap_pvt_thread_mutex_lock( &bdb->bi_idl_tree_lrulock );
1096                         ic->tail->next = bdb_tool_idl_free_list;
1097                         bdb_tool_idl_free_list = ic->head;
1098                         bdb->bi_idl_cache_size -= n;
1099                         ldap_pvt_thread_mutex_unlock( &bdb->bi_idl_tree_lrulock );
1100                 }
1101         }
1102         if ( ic != db->app_private ) {
1103                 ch_free( ic );
1104         } else {
1105                 ic->head = ic->tail = NULL;
1106         }
1107         curs->c_close( curs );
1108         return rc;
1109 }
1110
1111 static int
1112 bdb_tool_idl_flush_db( DB *db, bdb_tool_idl_cache *ic )
1113 {
1114         Avlnode *root = db->app_private;
1115         int rc;
1116
1117         db->app_private = ic;
1118         rc = avl_apply( root, bdb_tool_idl_flush_one, db, -1, AVL_INORDER );
1119         avl_free( root, NULL );
1120         db->app_private = NULL;
1121         if ( rc != -1 )
1122                 rc = 0;
1123         return rc;
1124 }
1125
1126 static int
1127 bdb_tool_idl_flush( BackendDB *be )
1128 {
1129         struct bdb_info *bdb = (struct bdb_info *) be->be_private;
1130         DB *db;
1131         Avlnode *root;
1132         int i, rc = 0;
1133
1134         for ( i=BDB_NDB; i < bdb->bi_ndatabases; i++ ) {
1135                 db = bdb->bi_databases[i]->bdi_db;
1136                 if ( !db->app_private ) continue;
1137                 rc = bdb_tool_idl_flush_db( db, NULL );
1138                 if ( rc )
1139                         break;
1140         }
1141         if ( !rc ) {
1142                 bdb->bi_idl_cache_size = 0;
1143         }
1144         return rc;
1145 }
1146
1147 int bdb_tool_idl_add(
1148         BackendDB *be,
1149         DB *db,
1150         DB_TXN *txn,
1151         DBT *key,
1152         ID id )
1153 {
1154         struct bdb_info *bdb = (struct bdb_info *) be->be_private;
1155         bdb_tool_idl_cache *ic, itmp;
1156         bdb_tool_idl_cache_entry *ice;
1157         int rc;
1158
1159         if ( !bdb->bi_idl_cache_max_size )
1160                 return bdb_idl_insert_key( be, db, txn, key, id );
1161
1162         DBT2bv( key, &itmp.kstr );
1163
1164         ic = avl_find( (Avlnode *)db->app_private, &itmp, bdb_tool_idl_cmp );
1165
1166         /* No entry yet, create one */
1167         if ( !ic ) {
1168                 DBC *curs;
1169                 DBT data;
1170                 ID nid;
1171                 int rc;
1172
1173                 ic = ch_malloc( sizeof( bdb_tool_idl_cache ) + itmp.kstr.bv_len );
1174                 ic->kstr.bv_len = itmp.kstr.bv_len;
1175                 ic->kstr.bv_val = (char *)(ic+1);
1176                 AC_MEMCPY( ic->kstr.bv_val, itmp.kstr.bv_val, ic->kstr.bv_len );
1177                 ic->head = ic->tail = NULL;
1178                 ic->last = 0;
1179                 ic->count = 0;
1180                 avl_insert( (Avlnode **)&db->app_private, ic, bdb_tool_idl_cmp,
1181                         avl_dup_error );
1182
1183                 /* load existing key count here */
1184                 rc = db->cursor( db, NULL, &curs, 0 );
1185                 if ( rc ) return rc;
1186
1187                 data.ulen = sizeof( ID );
1188                 data.flags = DB_DBT_USERMEM;
1189                 data.data = &nid;
1190                 rc = curs->c_get( curs, key, &data, DB_SET );
1191                 if ( rc == 0 ) {
1192                         if ( nid == 0 ) {
1193                                 ic->count = BDB_IDL_DB_SIZE+1;
1194                         } else {
1195                                 db_recno_t count;
1196
1197                                 curs->c_count( curs, &count, 0 );
1198                                 ic->count = count;
1199                                 BDB_DISK2ID( &nid, &ic->first );
1200                         }
1201                 }
1202                 curs->c_close( curs );
1203         }
1204         /* are we a range already? */
1205         if ( ic->count > BDB_IDL_DB_SIZE ) {
1206                 ic->last = id;
1207                 return 0;
1208         /* Are we at the limit, and converting to a range? */
1209         } else if ( ic->count == BDB_IDL_DB_SIZE ) {
1210                 int n;
1211                 for ( ice = ic->head, n=0; ice; ice = ice->next, n++ )
1212                         /* counting */ ;
1213                 if ( n ) {
1214                         ldap_pvt_thread_mutex_lock( &bdb->bi_idl_tree_lrulock );
1215                         ic->tail->next = bdb_tool_idl_free_list;
1216                         bdb_tool_idl_free_list = ic->head;
1217                         bdb->bi_idl_cache_size -= n;
1218                         ldap_pvt_thread_mutex_unlock( &bdb->bi_idl_tree_lrulock );
1219                 }
1220                 ic->head = ic->tail = NULL;
1221                 ic->last = id;
1222                 ic->count++;
1223                 return 0;
1224         }
1225         /* No free block, create that too */
1226         if ( !ic->tail || ( ic->count & (IDBLOCK-1)) == 0) {
1227                 ice = NULL;
1228                 ldap_pvt_thread_mutex_lock( &bdb->bi_idl_tree_lrulock );
1229                 if ( bdb->bi_idl_cache_size >= bdb->bi_idl_cache_max_size ) {
1230                         ldap_pvt_thread_mutex_unlock( &bdb->bi_idl_tree_lrulock );
1231                         rc = bdb_tool_idl_flush_db( db, ic );
1232                         if ( rc )
1233                                 return rc;
1234                         avl_insert( (Avlnode **)&db->app_private, ic, bdb_tool_idl_cmp,
1235                                 avl_dup_error );
1236                         ldap_pvt_thread_mutex_lock( &bdb->bi_idl_tree_lrulock );
1237                 }
1238                 bdb->bi_idl_cache_size++;
1239                 if ( bdb_tool_idl_free_list ) {
1240                         ice = bdb_tool_idl_free_list;
1241                         bdb_tool_idl_free_list = ice->next;
1242                 }
1243                 ldap_pvt_thread_mutex_unlock( &bdb->bi_idl_tree_lrulock );
1244                 if ( !ice ) {
1245                         ice = ch_malloc( sizeof( bdb_tool_idl_cache_entry ));
1246                 }
1247                 memset( ice, 0, sizeof( *ice ));
1248                 if ( !ic->head ) {
1249                         ic->head = ice;
1250                 } else {
1251                         ic->tail->next = ice;
1252                 }
1253                 ic->tail = ice;
1254                 if ( !ic->count )
1255                         ic->first = id;
1256         }
1257         ice = ic->tail;
1258         ice->ids[ ic->count & (IDBLOCK-1) ] = id;
1259         ic->count++;
1260
1261         return 0;
1262 }
1263 #endif
1264
1265 #ifdef USE_TRICKLE
1266 static void *
1267 bdb_tool_trickle_task( void *ctx, void *ptr )
1268 {
1269         DB_ENV *env = ptr;
1270         int wrote;
1271
1272         ldap_pvt_thread_mutex_lock( &bdb_tool_trickle_mutex );
1273         bdb_tool_trickle_active = 1;
1274         ldap_pvt_thread_cond_signal( &bdb_tool_trickle_cond_end );
1275         while ( 1 ) {
1276                 ldap_pvt_thread_cond_wait( &bdb_tool_trickle_cond,
1277                         &bdb_tool_trickle_mutex );
1278                 if ( slapd_shutdown )
1279                         break;
1280                 env->memp_trickle( env, 30, &wrote );
1281         }
1282         bdb_tool_trickle_active = 0;
1283         ldap_pvt_thread_cond_signal( &bdb_tool_trickle_cond_end );
1284         ldap_pvt_thread_mutex_unlock( &bdb_tool_trickle_mutex );
1285
1286         return NULL;
1287 }
1288 #endif
1289
1290 static void *
1291 bdb_tool_index_task( void *ctx, void *ptr )
1292 {
1293         int base = *(int *)ptr;
1294
1295         free( ptr );
1296         while ( 1 ) {
1297                 ldap_pvt_thread_mutex_lock( &bdb_tool_index_mutex );
1298                 bdb_tool_index_tcount--;
1299                 if ( !bdb_tool_index_tcount )
1300                         ldap_pvt_thread_cond_signal( &bdb_tool_index_cond_main );
1301                 ldap_pvt_thread_cond_wait( &bdb_tool_index_cond_work,
1302                         &bdb_tool_index_mutex );
1303                 if ( slapd_shutdown ) {
1304                         bdb_tool_index_tcount--;
1305                         if ( !bdb_tool_index_tcount )
1306                                 ldap_pvt_thread_cond_signal( &bdb_tool_index_cond_main );
1307                         ldap_pvt_thread_mutex_unlock( &bdb_tool_index_mutex );
1308                         break;
1309                 }
1310                 ldap_pvt_thread_mutex_unlock( &bdb_tool_index_mutex );
1311
1312                 bdb_tool_index_threads[base] = bdb_index_recrun( bdb_tool_ix_op,
1313                         bdb_tool_info, bdb_tool_index_rec, bdb_tool_ix_id, base );
1314         }
1315
1316         return NULL;
1317 }