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