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