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