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