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