]> git.sur5r.net Git - openldap/blob - servers/slapd/back-bdb/tools.c
fix typo
[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-2008 The OpenLDAP Foundation.
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted only as authorized by the OpenLDAP
10  * Public License.
11  *
12  * A copy of this license is available in the file LICENSE in the
13  * top-level directory of the distribution or, alternatively, at
14  * <http://www.OpenLDAP.org/license.html>.
15  */
16
17 #include "portable.h"
18
19 #include <stdio.h>
20 #include <ac/string.h>
21 #include <ac/errno.h>
22
23 #define AVL_INTERNAL
24 #include "back-bdb.h"
25 #include "idl.h"
26
27 static DBC *cursor = NULL;
28 static DBT key, data;
29 static EntryHeader eh;
30 static ID nid, previd = NOID;
31 static char ehbuf[16];
32
33 typedef struct dn_id {
34         ID id;
35         struct berval dn;
36 } dn_id;
37
38 #define HOLE_SIZE       4096
39 static dn_id hbuf[HOLE_SIZE], *holes = hbuf;
40 static unsigned nhmax = HOLE_SIZE;
41 static unsigned nholes;
42
43 static int index_nattrs;
44
45 #ifdef BDB_TOOL_IDL_CACHING
46 #define bdb_tool_idl_cmp                BDB_SYMBOL(tool_idl_cmp)
47 #define bdb_tool_idl_flush_one          BDB_SYMBOL(tool_idl_flush_one)
48 #define bdb_tool_idl_flush              BDB_SYMBOL(tool_idl_flush)
49
50 static int bdb_tool_idl_flush( BackendDB *be );
51
52 #define IDBLOCK 1024
53
54 typedef struct bdb_tool_idl_cache_entry {
55         struct bdb_tool_idl_cache_entry *next;
56         ID ids[IDBLOCK];
57 } bdb_tool_idl_cache_entry;
58  
59 typedef struct bdb_tool_idl_cache {
60         struct berval kstr;
61         bdb_tool_idl_cache_entry *head, *tail;
62         ID first, last;
63         int count;
64 } bdb_tool_idl_cache;
65
66 static bdb_tool_idl_cache_entry *bdb_tool_idl_free_list;
67 #endif  /* BDB_TOOL_IDL_CACHING */
68
69 static ID bdb_tool_ix_id;
70 static Operation *bdb_tool_ix_op;
71 static int *bdb_tool_index_threads, bdb_tool_index_tcount;
72 static void *bdb_tool_index_rec;
73 static struct bdb_info *bdb_tool_info;
74 static ldap_pvt_thread_mutex_t bdb_tool_index_mutex;
75 static ldap_pvt_thread_cond_t bdb_tool_index_cond_main;
76 static ldap_pvt_thread_cond_t bdb_tool_index_cond_work;
77
78 static ldap_pvt_thread_mutex_t bdb_tool_trickle_mutex;
79 static ldap_pvt_thread_cond_t bdb_tool_trickle_cond;
80
81 static void * bdb_tool_index_task( void *ctx, void *ptr );
82 static void * bdb_tool_trickle_task( void *ctx, void *ptr );
83
84 int bdb_tool_entry_open(
85         BackendDB *be, int mode )
86 {
87         struct bdb_info *bdb = (struct bdb_info *) be->be_private;
88
89         /* initialize key and data thangs */
90         DBTzero( &key );
91         DBTzero( &data );
92         key.flags = DB_DBT_USERMEM;
93         key.data = &nid;
94         key.size = key.ulen = sizeof( nid );
95         data.flags = DB_DBT_USERMEM;
96
97         if (cursor == NULL) {
98                 int rc = bdb->bi_id2entry->bdi_db->cursor(
99                         bdb->bi_id2entry->bdi_db, bdb->bi_cache.c_txn, &cursor,
100                         bdb->bi_db_opflags );
101                 if( rc != 0 ) {
102                         return -1;
103                 }
104         }
105
106         /* Set up for threaded slapindex */
107         if (( slapMode & (SLAP_TOOL_QUICK|SLAP_TOOL_READONLY)) == SLAP_TOOL_QUICK ) {
108                 if ( !bdb_tool_info ) {
109                         ldap_pvt_thread_mutex_init( &bdb_tool_trickle_mutex );
110                         ldap_pvt_thread_cond_init( &bdb_tool_trickle_cond );
111                         ldap_pvt_thread_pool_submit( &connection_pool, bdb_tool_trickle_task, bdb->bi_dbenv );
112
113                         ldap_pvt_thread_mutex_init( &bdb_tool_index_mutex );
114                         ldap_pvt_thread_cond_init( &bdb_tool_index_cond_main );
115                         ldap_pvt_thread_cond_init( &bdb_tool_index_cond_work );
116                         if ( bdb->bi_nattrs ) {
117                                 int i;
118                                 bdb_tool_index_threads = ch_malloc( slap_tool_thread_max * sizeof( int ));
119                                 bdb_tool_index_rec = ch_malloc( bdb->bi_nattrs * sizeof( IndexRec ));
120                                 bdb_tool_index_tcount = slap_tool_thread_max - 1;
121                                 for (i=1; i<slap_tool_thread_max; i++) {
122                                         int *ptr = ch_malloc( sizeof( int ));
123                                         *ptr = i;
124                                         ldap_pvt_thread_pool_submit( &connection_pool,
125                                                 bdb_tool_index_task, ptr );
126                                 }
127                         }
128                         bdb_tool_info = bdb;
129                 }
130         }
131
132         return 0;
133 }
134
135 int bdb_tool_entry_close(
136         BackendDB *be )
137 {
138         if ( bdb_tool_info ) {
139                 slapd_shutdown = 1;
140                 ldap_pvt_thread_mutex_lock( &bdb_tool_trickle_mutex );
141                 ldap_pvt_thread_cond_signal( &bdb_tool_trickle_cond );
142                 ldap_pvt_thread_mutex_unlock( &bdb_tool_trickle_mutex );
143                 ldap_pvt_thread_mutex_lock( &bdb_tool_index_mutex );
144                 bdb_tool_index_tcount = slap_tool_thread_max - 1;
145                 ldap_pvt_thread_cond_broadcast( &bdb_tool_index_cond_work );
146                 ldap_pvt_thread_mutex_unlock( &bdb_tool_index_mutex );
147         }
148
149         if( eh.bv.bv_val ) {
150                 ch_free( eh.bv.bv_val );
151                 eh.bv.bv_val = NULL;
152         }
153
154         if( cursor ) {
155                 cursor->c_close( cursor );
156                 cursor = NULL;
157         }
158
159 #ifdef BDB_TOOL_IDL_CACHING
160         bdb_tool_idl_flush( be );
161 #endif
162
163         if( nholes ) {
164                 unsigned i;
165                 fprintf( stderr, "Error, entries missing!\n");
166                 for (i=0; i<nholes; i++) {
167                         fprintf(stderr, "  entry %ld: %s\n",
168                                 holes[i].id, holes[i].dn.bv_val);
169                 }
170                 return -1;
171         }
172                         
173         return 0;
174 }
175
176 ID bdb_tool_entry_next(
177         BackendDB *be )
178 {
179         int rc;
180         ID id;
181         struct bdb_info *bdb = (struct bdb_info *) be->be_private;
182
183         assert( be != NULL );
184         assert( slapMode & SLAP_TOOL_MODE );
185         assert( bdb != NULL );
186
187         /* Get the header */
188         data.ulen = data.dlen = sizeof( ehbuf );
189         data.data = ehbuf;
190         data.flags |= DB_DBT_PARTIAL;
191         rc = cursor->c_get( cursor, &key, &data, DB_NEXT );
192
193         if( rc ) {
194                 /* If we're doing linear indexing and there are more attrs to
195                  * index, and we're at the end of the database, start over.
196                  */
197                 if ( index_nattrs && rc == DB_NOTFOUND ) {
198                         /* optional - do a checkpoint here? */
199                         bdb_attr_info_free( bdb->bi_attrs[0] );
200                         bdb->bi_attrs[0] = bdb->bi_attrs[index_nattrs];
201                         index_nattrs--;
202                         rc = cursor->c_get( cursor, &key, &data, DB_FIRST );
203                         if ( rc ) {
204                                 return NOID;
205                         }
206                 } else {
207                         return NOID;
208                 }
209         }
210
211         BDB_DISK2ID( key.data, &id );
212         previd = id;
213         return id;
214 }
215
216 ID bdb_tool_dn2id_get(
217         Backend *be,
218         struct berval *dn
219 )
220 {
221         Operation op = {0};
222         Opheader ohdr = {0};
223         EntryInfo *ei = NULL;
224         int rc;
225
226         if ( BER_BVISEMPTY(dn) )
227                 return 0;
228
229         op.o_hdr = &ohdr;
230         op.o_bd = be;
231         op.o_tmpmemctx = NULL;
232         op.o_tmpmfuncs = &ch_mfuncs;
233
234         rc = bdb_cache_find_ndn( &op, 0, dn, &ei );
235         if ( ei ) bdb_cache_entryinfo_unlock( ei );
236         if ( rc == DB_NOTFOUND )
237                 return NOID;
238         
239         return ei->bei_id;
240 }
241
242 Entry* bdb_tool_entry_get( BackendDB *be, ID id )
243 {
244         Entry *e = NULL;
245         char *dptr;
246         int rc, eoff;
247
248         assert( be != NULL );
249         assert( slapMode & SLAP_TOOL_MODE );
250
251         if ( id != previd ) {
252                 data.ulen = data.dlen = sizeof( ehbuf );
253                 data.data = ehbuf;
254                 data.flags |= DB_DBT_PARTIAL;
255
256                 BDB_ID2DISK( id, &nid );
257                 rc = cursor->c_get( cursor, &key, &data, DB_SET );
258                 if ( rc ) goto done;
259         }
260
261         /* Get the header */
262         dptr = eh.bv.bv_val;
263         eh.bv.bv_val = ehbuf;
264         eh.bv.bv_len = data.size;
265         rc = entry_header( &eh );
266         eoff = eh.data - eh.bv.bv_val;
267         eh.bv.bv_val = dptr;
268         if ( rc ) goto done;
269
270         /* Get the size */
271         data.flags &= ~DB_DBT_PARTIAL;
272         data.ulen = 0;
273     rc = cursor->c_get( cursor, &key, &data, DB_CURRENT );
274         if ( rc != DB_BUFFER_SMALL ) goto done;
275
276         /* Allocate a block and retrieve the data */
277         eh.bv.bv_len = eh.nvals * sizeof( struct berval ) + data.size;
278         eh.bv.bv_val = ch_realloc( eh.bv.bv_val, eh.bv.bv_len );
279         eh.data = eh.bv.bv_val + eh.nvals * sizeof( struct berval );
280         data.data = eh.data;
281         data.ulen = data.size;
282
283         /* Skip past already parsed nattr/nvals */
284         eh.data += eoff;
285
286     rc = cursor->c_get( cursor, &key, &data, DB_CURRENT );
287         if ( rc ) goto done;
288
289 #ifdef SLAP_ZONE_ALLOC
290         /* FIXME: will add ctx later */
291         rc = entry_decode( &eh, &e, NULL );
292 #else
293         rc = entry_decode( &eh, &e );
294 #endif
295
296         if( rc == LDAP_SUCCESS ) {
297                 e->e_id = id;
298 #ifdef BDB_HIER
299                 if ( slapMode & SLAP_TOOL_READONLY ) {
300                         struct bdb_info *bdb = (struct bdb_info *) be->be_private;
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, bdb->bi_cache.c_txn, 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 done:
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, &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 (( slapMode & SLAP_TOOL_QUICK ) && (( e->e_id & 0xfff ) == 0xfff )) {
526                 ldap_pvt_thread_mutex_lock( &bdb_tool_trickle_mutex );
527                 ldap_pvt_thread_cond_signal( &bdb_tool_trickle_cond );
528                 ldap_pvt_thread_mutex_unlock( &bdb_tool_trickle_mutex );
529         }
530
531         if ( !bdb->bi_linear_index )
532                 rc = bdb_tool_index_add( &op, tid, e );
533         if( rc != 0 ) {
534                 snprintf( text->bv_val, text->bv_len,
535                                 "index_entry_add failed: %s (%d)",
536                                 rc == LDAP_OTHER ? "Internal error" :
537                                 db_strerror(rc), rc );
538                 Debug( LDAP_DEBUG_ANY,
539                         "=> " LDAP_XSTRING(bdb_tool_entry_put) ": %s\n",
540                         text->bv_val, 0, 0 );
541                 goto done;
542         }
543
544         /* id2entry index */
545         rc = bdb_id2entry_add( be, tid, e );
546         if( rc != 0 ) {
547                 snprintf( text->bv_val, text->bv_len,
548                                 "id2entry_add failed: %s (%d)",
549                                 db_strerror(rc), rc );
550                 Debug( LDAP_DEBUG_ANY,
551                         "=> " LDAP_XSTRING(bdb_tool_entry_put) ": %s\n",
552                         text->bv_val, 0, 0 );
553                 goto done;
554         }
555
556 done:
557         if( rc == 0 ) {
558                 if ( !( slapMode & SLAP_TOOL_QUICK )) {
559                 rc = TXN_COMMIT( tid, 0 );
560                 if( rc != 0 ) {
561                         snprintf( text->bv_val, text->bv_len,
562                                         "txn_commit failed: %s (%d)",
563                                         db_strerror(rc), rc );
564                         Debug( LDAP_DEBUG_ANY,
565                                 "=> " LDAP_XSTRING(bdb_tool_entry_put) ": %s\n",
566                                 text->bv_val, 0, 0 );
567                         e->e_id = NOID;
568                 }
569                 }
570
571         } else {
572                 if ( !( slapMode & SLAP_TOOL_QUICK )) {
573                 TXN_ABORT( tid );
574                 snprintf( text->bv_val, text->bv_len,
575                         "txn_aborted! %s (%d)",
576                         rc == LDAP_OTHER ? "Internal error" :
577                         db_strerror(rc), rc );
578                 Debug( LDAP_DEBUG_ANY,
579                         "=> " LDAP_XSTRING(bdb_tool_entry_put) ": %s\n",
580                         text->bv_val, 0, 0 );
581                 }
582                 e->e_id = NOID;
583         }
584
585         return e->e_id;
586 }
587
588 int bdb_tool_entry_reindex(
589         BackendDB *be,
590         ID id,
591         AttributeDescription **adv )
592 {
593         struct bdb_info *bi = (struct bdb_info *) be->be_private;
594         int rc;
595         Entry *e;
596         DB_TXN *tid = NULL;
597         Operation op = {0};
598         Opheader ohdr = {0};
599
600         Debug( LDAP_DEBUG_ARGS,
601                 "=> " LDAP_XSTRING(bdb_tool_entry_reindex) "( %ld )\n",
602                 (long) id, 0, 0 );
603
604         /* No indexes configured, nothing to do. Could return an
605          * error here to shortcut things.
606          */
607         if (!bi->bi_attrs) {
608                 return 0;
609         }
610
611         /* Check for explicit list of attrs to index */
612         if ( adv ) {
613                 int i, j, n;
614
615                 if ( bi->bi_attrs[0]->ai_desc != adv[0] ) {
616                         /* count */
617                         for ( n = 0; adv[n]; n++ ) ;
618
619                         /* insertion sort */
620                         for ( i = 0; i < n; i++ ) {
621                                 AttributeDescription *ad = adv[i];
622                                 for ( j = i-1; j>=0; j--) {
623                                         if ( SLAP_PTRCMP( adv[j], ad ) <= 0 ) break;
624                                         adv[j+1] = adv[j];
625                                 }
626                                 adv[j+1] = ad;
627                         }
628                 }
629
630                 for ( i = 0; adv[i]; i++ ) {
631                         if ( bi->bi_attrs[i]->ai_desc != adv[i] ) {
632                                 for ( j = i+1; j < bi->bi_nattrs; j++ ) {
633                                         if ( bi->bi_attrs[j]->ai_desc == adv[i] ) {
634                                                 AttrInfo *ai = bi->bi_attrs[i];
635                                                 bi->bi_attrs[i] = bi->bi_attrs[j];
636                                                 bi->bi_attrs[j] = ai;
637                                                 break;
638                                         }
639                                 }
640                                 if ( j == bi->bi_nattrs ) {
641                                         Debug( LDAP_DEBUG_ANY,
642                                                 LDAP_XSTRING(bdb_tool_entry_reindex)
643                                                 ": no index configured for %s\n",
644                                                 adv[i]->ad_cname.bv_val, 0, 0 );
645                                         return -1;
646                                 }
647                         }
648                 }
649                 bi->bi_nattrs = i;
650         }
651
652         /* Get the first attribute to index */
653         if (bi->bi_linear_index && !index_nattrs) {
654                 index_nattrs = bi->bi_nattrs - 1;
655                 bi->bi_nattrs = 1;
656         }
657
658         e = bdb_tool_entry_get( be, id );
659
660         if( e == NULL ) {
661                 Debug( LDAP_DEBUG_ANY,
662                         LDAP_XSTRING(bdb_tool_entry_reindex)
663                         ": could not locate id=%ld\n",
664                         (long) id, 0, 0 );
665                 return -1;
666         }
667
668         if (! (slapMode & SLAP_TOOL_QUICK)) {
669         rc = TXN_BEGIN( bi->bi_dbenv, NULL, &tid, bi->bi_db_opflags );
670         if( rc != 0 ) {
671                 Debug( LDAP_DEBUG_ANY,
672                         "=> " LDAP_XSTRING(bdb_tool_entry_reindex) ": "
673                         "txn_begin failed: %s (%d)\n",
674                         db_strerror(rc), rc, 0 );
675                 goto done;
676         }
677         }
678         
679         /*
680          * just (re)add them for now
681          * assume that some other routine (not yet implemented)
682          * will zap index databases
683          *
684          */
685
686         Debug( LDAP_DEBUG_TRACE,
687                 "=> " LDAP_XSTRING(bdb_tool_entry_reindex) "( %ld, \"%s\" )\n",
688                 (long) id, e->e_dn, 0 );
689
690         op.o_hdr = &ohdr;
691         op.o_bd = be;
692         op.o_tmpmemctx = NULL;
693         op.o_tmpmfuncs = &ch_mfuncs;
694
695         rc = bdb_tool_index_add( &op, tid, e );
696
697 done:
698         if( rc == 0 ) {
699                 if (! (slapMode & SLAP_TOOL_QUICK)) {
700                 rc = TXN_COMMIT( tid, 0 );
701                 if( rc != 0 ) {
702                         Debug( LDAP_DEBUG_ANY,
703                                 "=> " LDAP_XSTRING(bdb_tool_entry_reindex)
704                                 ": txn_commit failed: %s (%d)\n",
705                                 db_strerror(rc), rc, 0 );
706                         e->e_id = NOID;
707                 }
708                 }
709
710         } else {
711                 if (! (slapMode & SLAP_TOOL_QUICK)) {
712                 TXN_ABORT( tid );
713                 Debug( LDAP_DEBUG_ANY,
714                         "=> " LDAP_XSTRING(bdb_tool_entry_reindex)
715                         ": txn_aborted! %s (%d)\n",
716                         db_strerror(rc), rc, 0 );
717                 }
718                 e->e_id = NOID;
719         }
720         bdb_entry_release( &op, e, 0 );
721
722         return rc;
723 }
724
725 ID bdb_tool_entry_modify(
726         BackendDB *be,
727         Entry *e,
728         struct berval *text )
729 {
730         int rc;
731         struct bdb_info *bdb = (struct bdb_info *) be->be_private;
732         DB_TXN *tid = NULL;
733         Operation op = {0};
734         Opheader ohdr = {0};
735
736         assert( be != NULL );
737         assert( slapMode & SLAP_TOOL_MODE );
738
739         assert( text != NULL );
740         assert( text->bv_val != NULL );
741         assert( text->bv_val[0] == '\0' );      /* overconservative? */
742
743         assert ( e->e_id != NOID );
744
745         Debug( LDAP_DEBUG_TRACE,
746                 "=> " LDAP_XSTRING(bdb_tool_entry_modify) "( %ld, \"%s\" )\n",
747                 (long) e->e_id, e->e_dn, 0 );
748
749         if (! (slapMode & SLAP_TOOL_QUICK)) {
750                 if( cursor ) {
751                         cursor->c_close( cursor );
752                         cursor = NULL;
753                 }
754                 rc = TXN_BEGIN( bdb->bi_dbenv, NULL, &tid, 
755                         bdb->bi_db_opflags );
756                 if( rc != 0 ) {
757                         snprintf( text->bv_val, text->bv_len,
758                                 "txn_begin failed: %s (%d)",
759                                 db_strerror(rc), rc );
760                         Debug( LDAP_DEBUG_ANY,
761                                 "=> " LDAP_XSTRING(bdb_tool_entry_modify) ": %s\n",
762                                  text->bv_val, 0, 0 );
763                         return NOID;
764                 }
765         }
766
767         op.o_hdr = &ohdr;
768         op.o_bd = be;
769         op.o_tmpmemctx = NULL;
770         op.o_tmpmfuncs = &ch_mfuncs;
771
772         /* id2entry index */
773         rc = bdb_id2entry_update( be, tid, e );
774         if( rc != 0 ) {
775                 snprintf( text->bv_val, text->bv_len,
776                                 "id2entry_add failed: %s (%d)",
777                                 db_strerror(rc), rc );
778                 Debug( LDAP_DEBUG_ANY,
779                         "=> " LDAP_XSTRING(bdb_tool_entry_modify) ": %s\n",
780                         text->bv_val, 0, 0 );
781                 goto done;
782         }
783
784 done:
785         if( rc == 0 ) {
786                 if (! (slapMode & SLAP_TOOL_QUICK)) {
787                 rc = TXN_COMMIT( tid, 0 );
788                 if( rc != 0 ) {
789                         snprintf( text->bv_val, text->bv_len,
790                                         "txn_commit failed: %s (%d)",
791                                         db_strerror(rc), rc );
792                         Debug( LDAP_DEBUG_ANY,
793                                 "=> " LDAP_XSTRING(bdb_tool_entry_modify) ": "
794                                 "%s\n", text->bv_val, 0, 0 );
795                         e->e_id = NOID;
796                 }
797                 }
798
799         } else {
800                 if (! (slapMode & SLAP_TOOL_QUICK)) {
801                 TXN_ABORT( tid );
802                 snprintf( text->bv_val, text->bv_len,
803                         "txn_aborted! %s (%d)",
804                         db_strerror(rc), rc );
805                 Debug( LDAP_DEBUG_ANY,
806                         "=> " LDAP_XSTRING(bdb_tool_entry_modify) ": %s\n",
807                         text->bv_val, 0, 0 );
808                 }
809                 e->e_id = NOID;
810         }
811
812         return e->e_id;
813 }
814
815 #ifdef BDB_TOOL_IDL_CACHING
816 static int
817 bdb_tool_idl_cmp( const void *v1, const void *v2 )
818 {
819         const bdb_tool_idl_cache *c1 = v1, *c2 = v2;
820         int rc;
821
822         if (( rc = c1->kstr.bv_len - c2->kstr.bv_len )) return rc;
823         return memcmp( c1->kstr.bv_val, c2->kstr.bv_val, c1->kstr.bv_len );
824 }
825
826 static int
827 bdb_tool_idl_flush_one( void *v1, void *arg )
828 {
829         bdb_tool_idl_cache *ic = v1;
830         DB *db = arg;
831         struct bdb_info *bdb = bdb_tool_info;
832         bdb_tool_idl_cache_entry *ice;
833         DBC *curs;
834         DBT key, data;
835         int i, rc;
836         ID id, nid;
837
838         /* Freshly allocated, ignore it */
839         if ( !ic->head && ic->count <= BDB_IDL_DB_SIZE ) {
840                 return 0;
841         }
842
843         rc = db->cursor( db, NULL, &curs, 0 );
844         if ( rc )
845                 return -1;
846
847         DBTzero( &key );
848         DBTzero( &data );
849
850         bv2DBT( &ic->kstr, &key );
851
852         data.size = data.ulen = sizeof( ID );
853         data.flags = DB_DBT_USERMEM;
854         data.data = &nid;
855
856         rc = curs->c_get( curs, &key, &data, DB_SET );
857         /* If key already exists and we're writing a range... */
858         if ( rc == 0 && ic->count > BDB_IDL_DB_SIZE ) {
859                 /* If it's not currently a range, must delete old info */
860                 if ( nid ) {
861                         /* Skip lo */
862                         while ( curs->c_get( curs, &key, &data, DB_NEXT_DUP ) == 0 )
863                                 curs->c_del( curs, 0 );
864
865                         nid = 0;
866                         /* Store range marker */
867                         curs->c_put( curs, &key, &data, DB_KEYFIRST );
868                 } else {
869                         
870                         /* Skip lo */
871                         rc = curs->c_get( curs, &key, &data, DB_NEXT_DUP );
872
873                         /* Get hi */
874                         rc = curs->c_get( curs, &key, &data, DB_NEXT_DUP );
875
876                         /* Delete hi */
877                         curs->c_del( curs, 0 );
878                 }
879                 BDB_ID2DISK( ic->last, &nid );
880                 curs->c_put( curs, &key, &data, DB_KEYLAST );
881                 rc = 0;
882         } else if ( rc && rc != DB_NOTFOUND ) {
883                 rc = -1;
884         } else if ( ic->count > BDB_IDL_DB_SIZE ) {
885                 /* range, didn't exist before */
886                 nid = 0;
887                 rc = curs->c_put( curs, &key, &data, DB_KEYLAST );
888                 if ( rc == 0 ) {
889                         BDB_ID2DISK( ic->first, &nid );
890                         rc = curs->c_put( curs, &key, &data, DB_KEYLAST );
891                         if ( rc == 0 ) {
892                                 BDB_ID2DISK( ic->last, &nid );
893                                 rc = curs->c_put( curs, &key, &data, DB_KEYLAST );
894                         }
895                 }
896                 if ( rc ) {
897                         rc = -1;
898                 }
899         } else {
900                 int n;
901
902                 /* Just a normal write */
903                 rc = 0;
904                 for ( ice = ic->head, n=0; ice; ice = ice->next, n++ ) {
905                         int end;
906                         if ( ice->next ) {
907                                 end = IDBLOCK;
908                         } else {
909                                 end = ic->count & (IDBLOCK-1);
910                                 if ( !end )
911                                         end = IDBLOCK;
912                         }
913                         for ( i=0; i<end; i++ ) {
914                                 if ( !ice->ids[i] ) continue;
915                                 BDB_ID2DISK( ice->ids[i], &nid );
916                                 rc = curs->c_put( curs, &key, &data, DB_NODUPDATA );
917                                 if ( rc ) {
918                                         if ( rc == DB_KEYEXIST ) {
919                                                 rc = 0;
920                                                 continue;
921                                         }
922                                         rc = -1;
923                                         break;
924                                 }
925                         }
926                         if ( rc ) {
927                                 rc = -1;
928                                 break;
929                         }
930                 }
931                 if ( ic->head ) {
932                         ldap_pvt_thread_mutex_lock( &bdb->bi_idl_tree_lrulock );
933                         ic->tail->next = bdb_tool_idl_free_list;
934                         bdb_tool_idl_free_list = ic->head;
935                         bdb->bi_idl_cache_size -= n;
936                         ldap_pvt_thread_mutex_unlock( &bdb->bi_idl_tree_lrulock );
937                 }
938         }
939         if ( ic != db->app_private ) {
940                 ch_free( ic );
941         } else {
942                 ic->head = ic->tail = NULL;
943         }
944         curs->c_close( curs );
945         return rc;
946 }
947
948 static int
949 bdb_tool_idl_flush_db( DB *db, bdb_tool_idl_cache *ic )
950 {
951         Avlnode *root = db->app_private;
952         int rc;
953
954         db->app_private = ic;
955         rc = avl_apply( root, bdb_tool_idl_flush_one, db, -1, AVL_INORDER );
956         avl_free( root, NULL );
957         db->app_private = NULL;
958         if ( rc != -1 )
959                 rc = 0;
960         return rc;
961 }
962
963 static int
964 bdb_tool_idl_flush( BackendDB *be )
965 {
966         struct bdb_info *bdb = (struct bdb_info *) be->be_private;
967         DB *db;
968         Avlnode *root;
969         int i, rc = 0;
970
971         for ( i=BDB_NDB; i < bdb->bi_ndatabases; i++ ) {
972                 db = bdb->bi_databases[i]->bdi_db;
973                 if ( !db->app_private ) continue;
974                 rc = bdb_tool_idl_flush_db( db, NULL );
975                 if ( rc )
976                         break;
977         }
978         if ( !rc ) {
979                 bdb->bi_idl_cache_size = 0;
980         }
981         return rc;
982 }
983
984 int bdb_tool_idl_add(
985         BackendDB *be,
986         DB *db,
987         DB_TXN *txn,
988         DBT *key,
989         ID id )
990 {
991         struct bdb_info *bdb = (struct bdb_info *) be->be_private;
992         bdb_tool_idl_cache *ic, itmp;
993         bdb_tool_idl_cache_entry *ice;
994         int rc;
995
996         if ( !bdb->bi_idl_cache_max_size )
997                 return bdb_idl_insert_key( be, db, txn, key, id );
998
999         DBT2bv( key, &itmp.kstr );
1000
1001         ic = avl_find( (Avlnode *)db->app_private, &itmp, bdb_tool_idl_cmp );
1002
1003         /* No entry yet, create one */
1004         if ( !ic ) {
1005                 DBC *curs;
1006                 DBT data;
1007                 ID nid;
1008                 int rc;
1009
1010                 ic = ch_malloc( sizeof( bdb_tool_idl_cache ) + itmp.kstr.bv_len );
1011                 ic->kstr.bv_len = itmp.kstr.bv_len;
1012                 ic->kstr.bv_val = (char *)(ic+1);
1013                 AC_MEMCPY( ic->kstr.bv_val, itmp.kstr.bv_val, ic->kstr.bv_len );
1014                 ic->head = ic->tail = NULL;
1015                 ic->last = 0;
1016                 ic->count = 0;
1017                 avl_insert( (Avlnode **)&db->app_private, ic, bdb_tool_idl_cmp,
1018                         avl_dup_error );
1019
1020                 /* load existing key count here */
1021                 rc = db->cursor( db, NULL, &curs, 0 );
1022                 if ( rc ) return rc;
1023
1024                 data.ulen = sizeof( ID );
1025                 data.flags = DB_DBT_USERMEM;
1026                 data.data = &nid;
1027                 rc = curs->c_get( curs, key, &data, DB_SET );
1028                 if ( rc == 0 ) {
1029                         if ( nid == 0 ) {
1030                                 ic->count = BDB_IDL_DB_SIZE+1;
1031                         } else {
1032                                 db_recno_t count;
1033
1034                                 curs->c_count( curs, &count, 0 );
1035                                 ic->count = count;
1036                                 BDB_DISK2ID( &nid, &ic->first );
1037                         }
1038                 }
1039                 curs->c_close( curs );
1040         }
1041         /* are we a range already? */
1042         if ( ic->count > BDB_IDL_DB_SIZE ) {
1043                 ic->last = id;
1044                 return 0;
1045         /* Are we at the limit, and converting to a range? */
1046         } else if ( ic->count == BDB_IDL_DB_SIZE ) {
1047                 int n;
1048                 for ( ice = ic->head, n=0; ice; ice = ice->next, n++ )
1049                         /* counting */ ;
1050                 if ( n ) {
1051                         ldap_pvt_thread_mutex_lock( &bdb->bi_idl_tree_lrulock );
1052                         ic->tail->next = bdb_tool_idl_free_list;
1053                         bdb_tool_idl_free_list = ic->head;
1054                         bdb->bi_idl_cache_size -= n;
1055                         ldap_pvt_thread_mutex_unlock( &bdb->bi_idl_tree_lrulock );
1056                 }
1057                 ic->head = ic->tail = NULL;
1058                 ic->last = id;
1059                 ic->count++;
1060                 return 0;
1061         }
1062         /* No free block, create that too */
1063         if ( !ic->tail || ( ic->count & (IDBLOCK-1)) == 0) {
1064                 ice = NULL;
1065                 ldap_pvt_thread_mutex_lock( &bdb->bi_idl_tree_lrulock );
1066                 if ( bdb->bi_idl_cache_size >= bdb->bi_idl_cache_max_size ) {
1067                         ldap_pvt_thread_mutex_unlock( &bdb->bi_idl_tree_lrulock );
1068                         rc = bdb_tool_idl_flush_db( db, ic );
1069                         if ( rc )
1070                                 return rc;
1071                         avl_insert( (Avlnode **)&db->app_private, ic, bdb_tool_idl_cmp,
1072                                 avl_dup_error );
1073                         ldap_pvt_thread_mutex_lock( &bdb->bi_idl_tree_lrulock );
1074                 }
1075                 bdb->bi_idl_cache_size++;
1076                 if ( bdb_tool_idl_free_list ) {
1077                         ice = bdb_tool_idl_free_list;
1078                         bdb_tool_idl_free_list = ice->next;
1079                 }
1080                 ldap_pvt_thread_mutex_unlock( &bdb->bi_idl_tree_lrulock );
1081                 if ( !ice ) {
1082                         ice = ch_malloc( sizeof( bdb_tool_idl_cache_entry ));
1083                 }
1084                 memset( ice, 0, sizeof( *ice ));
1085                 if ( !ic->head ) {
1086                         ic->head = ice;
1087                 } else {
1088                         ic->tail->next = ice;
1089                 }
1090                 ic->tail = ice;
1091                 if ( !ic->count )
1092                         ic->first = id;
1093         }
1094         ice = ic->tail;
1095         ice->ids[ ic->count & (IDBLOCK-1) ] = id;
1096         ic->count++;
1097
1098         return 0;
1099 }
1100 #endif
1101
1102 static void *
1103 bdb_tool_trickle_task( void *ctx, void *ptr )
1104 {
1105         DB_ENV *env = ptr;
1106         int wrote;
1107
1108         ldap_pvt_thread_mutex_lock( &bdb_tool_trickle_mutex );
1109         while ( 1 ) {
1110                 ldap_pvt_thread_cond_wait( &bdb_tool_trickle_cond,
1111                         &bdb_tool_trickle_mutex );
1112                 if ( slapd_shutdown )
1113                         break;
1114                 env->memp_trickle( env, 30, &wrote );
1115         }
1116         ldap_pvt_thread_mutex_unlock( &bdb_tool_trickle_mutex );
1117
1118         return NULL;
1119 }
1120
1121 static void *
1122 bdb_tool_index_task( void *ctx, void *ptr )
1123 {
1124         int base = *(int *)ptr;
1125
1126         free( ptr );
1127         while ( 1 ) {
1128                 ldap_pvt_thread_mutex_lock( &bdb_tool_index_mutex );
1129                 bdb_tool_index_tcount--;
1130                 if ( !bdb_tool_index_tcount )
1131                         ldap_pvt_thread_cond_signal( &bdb_tool_index_cond_main );
1132                 ldap_pvt_thread_cond_wait( &bdb_tool_index_cond_work,
1133                         &bdb_tool_index_mutex );
1134                 ldap_pvt_thread_mutex_unlock( &bdb_tool_index_mutex );
1135                 if ( slapd_shutdown )
1136                         break;
1137
1138                 bdb_tool_index_threads[base] = bdb_index_recrun( bdb_tool_ix_op,
1139                         bdb_tool_info, bdb_tool_index_rec, bdb_tool_ix_id, base );
1140         }
1141
1142         return NULL;
1143 }