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