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