]> git.sur5r.net Git - openldap/blob - servers/slapd/back-bdb/tools.c
ITS#5189 revert index.c 1.67, just catch LDAP_OTHER
[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 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, NULL, 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, NULL, 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, &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         rc = TXN_BEGIN( bdb->bi_dbenv, NULL, &tid, 
750                 bdb->bi_db_opflags );
751         if( rc != 0 ) {
752                 snprintf( text->bv_val, text->bv_len,
753                         "txn_begin failed: %s (%d)",
754                         db_strerror(rc), rc );
755                 Debug( LDAP_DEBUG_ANY,
756                         "=> " LDAP_XSTRING(bdb_tool_entry_modify) ": %s\n",
757                          text->bv_val, 0, 0 );
758                 return NOID;
759         }
760         }
761
762         op.o_hdr = &ohdr;
763         op.o_bd = be;
764         op.o_tmpmemctx = NULL;
765         op.o_tmpmfuncs = &ch_mfuncs;
766
767         /* id2entry index */
768         rc = bdb_id2entry_update( be, tid, e );
769         if( rc != 0 ) {
770                 snprintf( text->bv_val, text->bv_len,
771                                 "id2entry_add failed: %s (%d)",
772                                 db_strerror(rc), rc );
773                 Debug( LDAP_DEBUG_ANY,
774                         "=> " LDAP_XSTRING(bdb_tool_entry_modify) ": %s\n",
775                         text->bv_val, 0, 0 );
776                 goto done;
777         }
778
779 done:
780         if( rc == 0 ) {
781                 if (! (slapMode & SLAP_TOOL_QUICK)) {
782                 rc = TXN_COMMIT( tid, 0 );
783                 if( rc != 0 ) {
784                         snprintf( text->bv_val, text->bv_len,
785                                         "txn_commit failed: %s (%d)",
786                                         db_strerror(rc), rc );
787                         Debug( LDAP_DEBUG_ANY,
788                                 "=> " LDAP_XSTRING(bdb_tool_entry_modify) ": "
789                                 "%s\n", text->bv_val, 0, 0 );
790                         e->e_id = NOID;
791                 }
792                 }
793
794         } else {
795                 if (! (slapMode & SLAP_TOOL_QUICK)) {
796                 TXN_ABORT( tid );
797                 snprintf( text->bv_val, text->bv_len,
798                         "txn_aborted! %s (%d)",
799                         db_strerror(rc), rc );
800                 Debug( LDAP_DEBUG_ANY,
801                         "=> " LDAP_XSTRING(bdb_tool_entry_modify) ": %s\n",
802                         text->bv_val, 0, 0 );
803                 }
804                 e->e_id = NOID;
805         }
806
807         return e->e_id;
808 }
809
810 #ifdef BDB_TOOL_IDL_CACHING
811 static int
812 bdb_tool_idl_cmp( const void *v1, const void *v2 )
813 {
814         const bdb_tool_idl_cache *c1 = v1, *c2 = v2;
815         int rc;
816
817         if (( rc = c1->kstr.bv_len - c2->kstr.bv_len )) return rc;
818         return memcmp( c1->kstr.bv_val, c2->kstr.bv_val, c1->kstr.bv_len );
819 }
820
821 static int
822 bdb_tool_idl_flush_one( void *v1, void *arg )
823 {
824         bdb_tool_idl_cache *ic = v1;
825         DB *db = arg;
826         struct bdb_info *bdb = bdb_tool_info;
827         bdb_tool_idl_cache_entry *ice;
828         DBC *curs;
829         DBT key, data;
830         int i, rc;
831         ID id, nid;
832
833         /* Freshly allocated, ignore it */
834         if ( !ic->head && ic->count <= BDB_IDL_DB_SIZE ) {
835                 return 0;
836         }
837
838         rc = db->cursor( db, NULL, &curs, 0 );
839         if ( rc )
840                 return -1;
841
842         DBTzero( &key );
843         DBTzero( &data );
844
845         bv2DBT( &ic->kstr, &key );
846
847         data.size = data.ulen = sizeof( ID );
848         data.flags = DB_DBT_USERMEM;
849         data.data = &nid;
850
851         rc = curs->c_get( curs, &key, &data, DB_SET );
852         /* If key already exists and we're writing a range... */
853         if ( rc == 0 && ic->count > BDB_IDL_DB_SIZE ) {
854                 /* If it's not currently a range, must delete old info */
855                 if ( nid ) {
856                         /* Skip lo */
857                         while ( curs->c_get( curs, &key, &data, DB_NEXT_DUP ) == 0 )
858                                 curs->c_del( curs, 0 );
859
860                         nid = 0;
861                         /* Store range marker */
862                         curs->c_put( curs, &key, &data, DB_KEYFIRST );
863                 } else {
864                         
865                         /* Skip lo */
866                         rc = curs->c_get( curs, &key, &data, DB_NEXT_DUP );
867
868                         /* Get hi */
869                         rc = curs->c_get( curs, &key, &data, DB_NEXT_DUP );
870
871                         /* Delete hi */
872                         curs->c_del( curs, 0 );
873                 }
874                 BDB_ID2DISK( ic->last, &nid );
875                 curs->c_put( curs, &key, &data, DB_KEYLAST );
876                 rc = 0;
877         } else if ( rc && rc != DB_NOTFOUND ) {
878                 rc = -1;
879         } else if ( ic->count > BDB_IDL_DB_SIZE ) {
880                 /* range, didn't exist before */
881                 nid = 0;
882                 rc = curs->c_put( curs, &key, &data, DB_KEYLAST );
883                 if ( rc == 0 ) {
884                         BDB_ID2DISK( ic->first, &nid );
885                         rc = curs->c_put( curs, &key, &data, DB_KEYLAST );
886                         if ( rc == 0 ) {
887                                 BDB_ID2DISK( ic->last, &nid );
888                                 rc = curs->c_put( curs, &key, &data, DB_KEYLAST );
889                         }
890                 }
891                 if ( rc ) {
892                         rc = -1;
893                 }
894         } else {
895                 int n;
896
897                 /* Just a normal write */
898                 rc = 0;
899                 for ( ice = ic->head, n=0; ice; ice = ice->next, n++ ) {
900                         int end;
901                         if ( ice->next ) {
902                                 end = IDBLOCK;
903                         } else {
904                                 end = ic->count & (IDBLOCK-1);
905                                 if ( !end )
906                                         end = IDBLOCK;
907                         }
908                         for ( i=0; i<end; i++ ) {
909                                 if ( !ice->ids[i] ) continue;
910                                 BDB_ID2DISK( ice->ids[i], &nid );
911                                 rc = curs->c_put( curs, &key, &data, DB_NODUPDATA );
912                                 if ( rc ) {
913                                         if ( rc == DB_KEYEXIST ) {
914                                                 rc = 0;
915                                                 continue;
916                                         }
917                                         rc = -1;
918                                         break;
919                                 }
920                         }
921                         if ( rc ) {
922                                 rc = -1;
923                                 break;
924                         }
925                 }
926                 if ( ic->head ) {
927                         ldap_pvt_thread_mutex_lock( &bdb->bi_idl_tree_lrulock );
928                         ic->tail->next = bdb_tool_idl_free_list;
929                         bdb_tool_idl_free_list = ic->head;
930                         bdb->bi_idl_cache_size -= n;
931                         ldap_pvt_thread_mutex_unlock( &bdb->bi_idl_tree_lrulock );
932                 }
933         }
934         if ( ic != db->app_private ) {
935                 ch_free( ic );
936         } else {
937                 ic->head = ic->tail = NULL;
938         }
939         curs->c_close( curs );
940         return rc;
941 }
942
943 static int
944 bdb_tool_idl_flush_db( DB *db, bdb_tool_idl_cache *ic )
945 {
946         Avlnode *root = db->app_private;
947         int rc;
948
949         db->app_private = ic;
950         rc = avl_apply( root, bdb_tool_idl_flush_one, db, -1, AVL_INORDER );
951         avl_free( root, NULL );
952         db->app_private = NULL;
953         if ( rc != -1 )
954                 rc = 0;
955         return rc;
956 }
957
958 static int
959 bdb_tool_idl_flush( BackendDB *be )
960 {
961         struct bdb_info *bdb = (struct bdb_info *) be->be_private;
962         DB *db;
963         Avlnode *root;
964         int i, rc = 0;
965
966         for ( i=BDB_NDB; i < bdb->bi_ndatabases; i++ ) {
967                 db = bdb->bi_databases[i]->bdi_db;
968                 if ( !db->app_private ) continue;
969                 rc = bdb_tool_idl_flush_db( db, NULL );
970                 if ( rc )
971                         break;
972         }
973         if ( !rc ) {
974                 bdb->bi_idl_cache_size = 0;
975         }
976         return rc;
977 }
978
979 int bdb_tool_idl_add(
980         BackendDB *be,
981         DB *db,
982         DB_TXN *txn,
983         DBT *key,
984         ID id )
985 {
986         struct bdb_info *bdb = (struct bdb_info *) be->be_private;
987         bdb_tool_idl_cache *ic, itmp;
988         bdb_tool_idl_cache_entry *ice;
989         int rc;
990
991         if ( !bdb->bi_idl_cache_max_size )
992                 return bdb_idl_insert_key( be, db, txn, key, id );
993
994         DBT2bv( key, &itmp.kstr );
995
996         ic = avl_find( (Avlnode *)db->app_private, &itmp, bdb_tool_idl_cmp );
997
998         /* No entry yet, create one */
999         if ( !ic ) {
1000                 DBC *curs;
1001                 DBT data;
1002                 ID nid;
1003                 int rc;
1004
1005                 ic = ch_malloc( sizeof( bdb_tool_idl_cache ) + itmp.kstr.bv_len );
1006                 ic->kstr.bv_len = itmp.kstr.bv_len;
1007                 ic->kstr.bv_val = (char *)(ic+1);
1008                 AC_MEMCPY( ic->kstr.bv_val, itmp.kstr.bv_val, ic->kstr.bv_len );
1009                 ic->head = ic->tail = NULL;
1010                 ic->last = 0;
1011                 ic->count = 0;
1012                 avl_insert( (Avlnode **)&db->app_private, ic, bdb_tool_idl_cmp,
1013                         avl_dup_error );
1014
1015                 /* load existing key count here */
1016                 rc = db->cursor( db, NULL, &curs, 0 );
1017                 if ( rc ) return rc;
1018
1019                 data.ulen = sizeof( ID );
1020                 data.flags = DB_DBT_USERMEM;
1021                 data.data = &nid;
1022                 rc = curs->c_get( curs, key, &data, DB_SET );
1023                 if ( rc == 0 ) {
1024                         if ( nid == 0 ) {
1025                                 ic->count = BDB_IDL_DB_SIZE+1;
1026                         } else {
1027                                 db_recno_t count;
1028
1029                                 curs->c_count( curs, &count, 0 );
1030                                 ic->count = count;
1031                                 BDB_DISK2ID( &nid, &ic->first );
1032                         }
1033                 }
1034                 curs->c_close( curs );
1035         }
1036         /* are we a range already? */
1037         if ( ic->count > BDB_IDL_DB_SIZE ) {
1038                 ic->last = id;
1039                 return 0;
1040         /* Are we at the limit, and converting to a range? */
1041         } else if ( ic->count == BDB_IDL_DB_SIZE ) {
1042                 int n;
1043                 for ( ice = ic->head, n=0; ice; ice = ice->next, n++ )
1044                         /* counting */ ;
1045                 if ( n ) {
1046                         ldap_pvt_thread_mutex_lock( &bdb->bi_idl_tree_lrulock );
1047                         ic->tail->next = bdb_tool_idl_free_list;
1048                         bdb_tool_idl_free_list = ic->head;
1049                         bdb->bi_idl_cache_size -= n;
1050                         ldap_pvt_thread_mutex_unlock( &bdb->bi_idl_tree_lrulock );
1051                 }
1052                 ic->head = ic->tail = NULL;
1053                 ic->last = id;
1054                 ic->count++;
1055                 return 0;
1056         }
1057         /* No free block, create that too */
1058         if ( !ic->tail || ( ic->count & (IDBLOCK-1)) == 0) {
1059                 ice = NULL;
1060                 ldap_pvt_thread_mutex_lock( &bdb->bi_idl_tree_lrulock );
1061                 if ( bdb->bi_idl_cache_size >= bdb->bi_idl_cache_max_size ) {
1062                         ldap_pvt_thread_mutex_unlock( &bdb->bi_idl_tree_lrulock );
1063                         rc = bdb_tool_idl_flush_db( db, ic );
1064                         if ( rc )
1065                                 return rc;
1066                         avl_insert( (Avlnode **)&db->app_private, ic, bdb_tool_idl_cmp,
1067                                 avl_dup_error );
1068                         ldap_pvt_thread_mutex_lock( &bdb->bi_idl_tree_lrulock );
1069                 }
1070                 bdb->bi_idl_cache_size++;
1071                 if ( bdb_tool_idl_free_list ) {
1072                         ice = bdb_tool_idl_free_list;
1073                         bdb_tool_idl_free_list = ice->next;
1074                 }
1075                 ldap_pvt_thread_mutex_unlock( &bdb->bi_idl_tree_lrulock );
1076                 if ( !ice ) {
1077                         ice = ch_malloc( sizeof( bdb_tool_idl_cache_entry ));
1078                 }
1079                 memset( ice, 0, sizeof( *ice ));
1080                 if ( !ic->head ) {
1081                         ic->head = ice;
1082                 } else {
1083                         ic->tail->next = ice;
1084                 }
1085                 ic->tail = ice;
1086                 if ( !ic->count )
1087                         ic->first = id;
1088         }
1089         ice = ic->tail;
1090         ice->ids[ ic->count & (IDBLOCK-1) ] = id;
1091         ic->count++;
1092
1093         return 0;
1094 }
1095 #endif
1096
1097 static void *
1098 bdb_tool_trickle_task( void *ctx, void *ptr )
1099 {
1100         DB_ENV *env = ptr;
1101         int wrote;
1102
1103         ldap_pvt_thread_mutex_lock( &bdb_tool_trickle_mutex );
1104         while ( 1 ) {
1105                 ldap_pvt_thread_cond_wait( &bdb_tool_trickle_cond,
1106                         &bdb_tool_trickle_mutex );
1107                 if ( slapd_shutdown )
1108                         break;
1109                 env->memp_trickle( env, 30, &wrote );
1110         }
1111         ldap_pvt_thread_mutex_unlock( &bdb_tool_trickle_mutex );
1112
1113         return NULL;
1114 }
1115
1116 static void *
1117 bdb_tool_index_task( void *ctx, void *ptr )
1118 {
1119         int base = *(int *)ptr;
1120
1121         free( ptr );
1122         while ( 1 ) {
1123                 ldap_pvt_thread_mutex_lock( &bdb_tool_index_mutex );
1124                 bdb_tool_index_tcount--;
1125                 if ( !bdb_tool_index_tcount )
1126                         ldap_pvt_thread_cond_signal( &bdb_tool_index_cond_main );
1127                 ldap_pvt_thread_cond_wait( &bdb_tool_index_cond_work,
1128                         &bdb_tool_index_mutex );
1129                 ldap_pvt_thread_mutex_unlock( &bdb_tool_index_mutex );
1130                 if ( slapd_shutdown )
1131                         break;
1132
1133                 bdb_tool_index_threads[base] = bdb_index_recrun( bdb_tool_ix_op,
1134                         bdb_tool_info, bdb_tool_index_rec, bdb_tool_ix_id, base );
1135         }
1136
1137         return NULL;
1138 }