]> git.sur5r.net Git - openldap/blob - servers/slapd/back-bdb/tools.c
Update copyright for next release
[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-2009 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, j;
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                                 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_cond_signal( &bdb_tool_trickle_cond );
526         }
527
528         if ( !bdb->bi_linear_index )
529                 rc = bdb_tool_index_add( &op, tid, e );
530         if( rc != 0 ) {
531                 snprintf( text->bv_val, text->bv_len,
532                                 "index_entry_add failed: %s (%d)",
533                                 rc == LDAP_OTHER ? "Internal error" :
534                                 db_strerror(rc), rc );
535                 Debug( LDAP_DEBUG_ANY,
536                         "=> " LDAP_XSTRING(bdb_tool_entry_put) ": %s\n",
537                         text->bv_val, 0, 0 );
538                 goto done;
539         }
540
541         /* id2entry index */
542         rc = bdb_id2entry_add( be, tid, e );
543         if( rc != 0 ) {
544                 snprintf( text->bv_val, text->bv_len,
545                                 "id2entry_add failed: %s (%d)",
546                                 db_strerror(rc), rc );
547                 Debug( LDAP_DEBUG_ANY,
548                         "=> " LDAP_XSTRING(bdb_tool_entry_put) ": %s\n",
549                         text->bv_val, 0, 0 );
550                 goto done;
551         }
552
553 done:
554         if( rc == 0 ) {
555                 if ( !( slapMode & SLAP_TOOL_QUICK )) {
556                 rc = TXN_COMMIT( tid, 0 );
557                 if( rc != 0 ) {
558                         snprintf( text->bv_val, text->bv_len,
559                                         "txn_commit failed: %s (%d)",
560                                         db_strerror(rc), rc );
561                         Debug( LDAP_DEBUG_ANY,
562                                 "=> " LDAP_XSTRING(bdb_tool_entry_put) ": %s\n",
563                                 text->bv_val, 0, 0 );
564                         e->e_id = NOID;
565                 }
566                 }
567
568         } else {
569                 if ( !( slapMode & SLAP_TOOL_QUICK )) {
570                 TXN_ABORT( tid );
571                 snprintf( text->bv_val, text->bv_len,
572                         "txn_aborted! %s (%d)",
573                         rc == LDAP_OTHER ? "Internal error" :
574                         db_strerror(rc), rc );
575                 Debug( LDAP_DEBUG_ANY,
576                         "=> " LDAP_XSTRING(bdb_tool_entry_put) ": %s\n",
577                         text->bv_val, 0, 0 );
578                 }
579                 e->e_id = NOID;
580         }
581
582         return e->e_id;
583 }
584
585 int bdb_tool_entry_reindex(
586         BackendDB *be,
587         ID id,
588         AttributeDescription **adv )
589 {
590         struct bdb_info *bi = (struct bdb_info *) be->be_private;
591         int rc;
592         Entry *e;
593         DB_TXN *tid = NULL;
594         Operation op = {0};
595         Opheader ohdr = {0};
596
597         Debug( LDAP_DEBUG_ARGS,
598                 "=> " LDAP_XSTRING(bdb_tool_entry_reindex) "( %ld )\n",
599                 (long) id, 0, 0 );
600
601         /* No indexes configured, nothing to do. Could return an
602          * error here to shortcut things.
603          */
604         if (!bi->bi_attrs) {
605                 return 0;
606         }
607
608         /* Check for explicit list of attrs to index */
609         if ( adv ) {
610                 int i, j, n;
611
612                 if ( bi->bi_attrs[0]->ai_desc != adv[0] ) {
613                         /* count */
614                         for ( n = 0; adv[n]; n++ ) ;
615
616                         /* insertion sort */
617                         for ( i = 0; i < n; i++ ) {
618                                 AttributeDescription *ad = adv[i];
619                                 for ( j = i-1; j>=0; j--) {
620                                         if ( SLAP_PTRCMP( adv[j], ad ) <= 0 ) break;
621                                         adv[j+1] = adv[j];
622                                 }
623                                 adv[j+1] = ad;
624                         }
625                 }
626
627                 for ( i = 0; adv[i]; i++ ) {
628                         if ( bi->bi_attrs[i]->ai_desc != adv[i] ) {
629                                 for ( j = i+1; j < bi->bi_nattrs; j++ ) {
630                                         if ( bi->bi_attrs[j]->ai_desc == adv[i] ) {
631                                                 AttrInfo *ai = bi->bi_attrs[i];
632                                                 bi->bi_attrs[i] = bi->bi_attrs[j];
633                                                 bi->bi_attrs[j] = ai;
634                                                 break;
635                                         }
636                                 }
637                                 if ( j == bi->bi_nattrs ) {
638                                         Debug( LDAP_DEBUG_ANY,
639                                                 LDAP_XSTRING(bdb_tool_entry_reindex)
640                                                 ": no index configured for %s\n",
641                                                 adv[i]->ad_cname.bv_val, 0, 0 );
642                                         return -1;
643                                 }
644                         }
645                 }
646                 bi->bi_nattrs = i;
647         }
648
649         /* Get the first attribute to index */
650         if (bi->bi_linear_index && !index_nattrs) {
651                 index_nattrs = bi->bi_nattrs - 1;
652                 bi->bi_nattrs = 1;
653         }
654
655         e = bdb_tool_entry_get( be, id );
656
657         if( e == NULL ) {
658                 Debug( LDAP_DEBUG_ANY,
659                         LDAP_XSTRING(bdb_tool_entry_reindex)
660                         ": could not locate id=%ld\n",
661                         (long) id, 0, 0 );
662                 return -1;
663         }
664
665         if (! (slapMode & SLAP_TOOL_QUICK)) {
666         rc = TXN_BEGIN( bi->bi_dbenv, NULL, &tid, bi->bi_db_opflags );
667         if( rc != 0 ) {
668                 Debug( LDAP_DEBUG_ANY,
669                         "=> " LDAP_XSTRING(bdb_tool_entry_reindex) ": "
670                         "txn_begin failed: %s (%d)\n",
671                         db_strerror(rc), rc, 0 );
672                 goto done;
673         }
674         }
675         
676         /*
677          * just (re)add them for now
678          * assume that some other routine (not yet implemented)
679          * will zap index databases
680          *
681          */
682
683         Debug( LDAP_DEBUG_TRACE,
684                 "=> " LDAP_XSTRING(bdb_tool_entry_reindex) "( %ld, \"%s\" )\n",
685                 (long) id, e->e_dn, 0 );
686
687         op.o_hdr = &ohdr;
688         op.o_bd = be;
689         op.o_tmpmemctx = NULL;
690         op.o_tmpmfuncs = &ch_mfuncs;
691
692         rc = bdb_tool_index_add( &op, tid, e );
693
694 done:
695         if( rc == 0 ) {
696                 if (! (slapMode & SLAP_TOOL_QUICK)) {
697                 rc = TXN_COMMIT( tid, 0 );
698                 if( rc != 0 ) {
699                         Debug( LDAP_DEBUG_ANY,
700                                 "=> " LDAP_XSTRING(bdb_tool_entry_reindex)
701                                 ": txn_commit failed: %s (%d)\n",
702                                 db_strerror(rc), rc, 0 );
703                         e->e_id = NOID;
704                 }
705                 }
706
707         } else {
708                 if (! (slapMode & SLAP_TOOL_QUICK)) {
709                 TXN_ABORT( tid );
710                 Debug( LDAP_DEBUG_ANY,
711                         "=> " LDAP_XSTRING(bdb_tool_entry_reindex)
712                         ": txn_aborted! %s (%d)\n",
713                         db_strerror(rc), rc, 0 );
714                 }
715                 e->e_id = NOID;
716         }
717         bdb_entry_release( &op, e, 0 );
718
719         return rc;
720 }
721
722 ID bdb_tool_entry_modify(
723         BackendDB *be,
724         Entry *e,
725         struct berval *text )
726 {
727         int rc;
728         struct bdb_info *bdb = (struct bdb_info *) be->be_private;
729         DB_TXN *tid = NULL;
730         Operation op = {0};
731         Opheader ohdr = {0};
732
733         assert( be != NULL );
734         assert( slapMode & SLAP_TOOL_MODE );
735
736         assert( text != NULL );
737         assert( text->bv_val != NULL );
738         assert( text->bv_val[0] == '\0' );      /* overconservative? */
739
740         assert ( e->e_id != NOID );
741
742         Debug( LDAP_DEBUG_TRACE,
743                 "=> " LDAP_XSTRING(bdb_tool_entry_modify) "( %ld, \"%s\" )\n",
744                 (long) e->e_id, e->e_dn, 0 );
745
746         if (! (slapMode & SLAP_TOOL_QUICK)) {
747                 if( cursor ) {
748                         cursor->c_close( cursor );
749                         cursor = NULL;
750                 }
751                 rc = TXN_BEGIN( bdb->bi_dbenv, NULL, &tid, 
752                         bdb->bi_db_opflags );
753                 if( rc != 0 ) {
754                         snprintf( text->bv_val, text->bv_len,
755                                 "txn_begin failed: %s (%d)",
756                                 db_strerror(rc), rc );
757                         Debug( LDAP_DEBUG_ANY,
758                                 "=> " LDAP_XSTRING(bdb_tool_entry_modify) ": %s\n",
759                                  text->bv_val, 0, 0 );
760                         return NOID;
761                 }
762         }
763
764         op.o_hdr = &ohdr;
765         op.o_bd = be;
766         op.o_tmpmemctx = NULL;
767         op.o_tmpmfuncs = &ch_mfuncs;
768
769         /* id2entry index */
770         rc = bdb_id2entry_update( be, tid, e );
771         if( rc != 0 ) {
772                 snprintf( text->bv_val, text->bv_len,
773                                 "id2entry_add failed: %s (%d)",
774                                 db_strerror(rc), rc );
775                 Debug( LDAP_DEBUG_ANY,
776                         "=> " LDAP_XSTRING(bdb_tool_entry_modify) ": %s\n",
777                         text->bv_val, 0, 0 );
778                 goto done;
779         }
780
781 done:
782         if( rc == 0 ) {
783                 if (! (slapMode & SLAP_TOOL_QUICK)) {
784                 rc = TXN_COMMIT( tid, 0 );
785                 if( rc != 0 ) {
786                         snprintf( text->bv_val, text->bv_len,
787                                         "txn_commit failed: %s (%d)",
788                                         db_strerror(rc), rc );
789                         Debug( LDAP_DEBUG_ANY,
790                                 "=> " LDAP_XSTRING(bdb_tool_entry_modify) ": "
791                                 "%s\n", text->bv_val, 0, 0 );
792                         e->e_id = NOID;
793                 }
794                 }
795
796         } else {
797                 if (! (slapMode & SLAP_TOOL_QUICK)) {
798                 TXN_ABORT( tid );
799                 snprintf( text->bv_val, text->bv_len,
800                         "txn_aborted! %s (%d)",
801                         db_strerror(rc), rc );
802                 Debug( LDAP_DEBUG_ANY,
803                         "=> " LDAP_XSTRING(bdb_tool_entry_modify) ": %s\n",
804                         text->bv_val, 0, 0 );
805                 }
806                 e->e_id = NOID;
807         }
808
809         return e->e_id;
810 }
811
812 #ifdef BDB_TOOL_IDL_CACHING
813 static int
814 bdb_tool_idl_cmp( const void *v1, const void *v2 )
815 {
816         const bdb_tool_idl_cache *c1 = v1, *c2 = v2;
817         int rc;
818
819         if (( rc = c1->kstr.bv_len - c2->kstr.bv_len )) return rc;
820         return memcmp( c1->kstr.bv_val, c2->kstr.bv_val, c1->kstr.bv_len );
821 }
822
823 static int
824 bdb_tool_idl_flush_one( void *v1, void *arg )
825 {
826         bdb_tool_idl_cache *ic = v1;
827         DB *db = arg;
828         struct bdb_info *bdb = bdb_tool_info;
829         bdb_tool_idl_cache_entry *ice;
830         DBC *curs;
831         DBT key, data;
832         int i, rc;
833         ID id, nid;
834
835         /* Freshly allocated, ignore it */
836         if ( !ic->head && ic->count <= BDB_IDL_DB_SIZE ) {
837                 return 0;
838         }
839
840         rc = db->cursor( db, NULL, &curs, 0 );
841         if ( rc )
842                 return -1;
843
844         DBTzero( &key );
845         DBTzero( &data );
846
847         bv2DBT( &ic->kstr, &key );
848
849         data.size = data.ulen = sizeof( ID );
850         data.flags = DB_DBT_USERMEM;
851         data.data = &nid;
852
853         rc = curs->c_get( curs, &key, &data, DB_SET );
854         /* If key already exists and we're writing a range... */
855         if ( rc == 0 && ic->count > BDB_IDL_DB_SIZE ) {
856                 /* If it's not currently a range, must delete old info */
857                 if ( nid ) {
858                         /* Skip lo */
859                         while ( curs->c_get( curs, &key, &data, DB_NEXT_DUP ) == 0 )
860                                 curs->c_del( curs, 0 );
861
862                         nid = 0;
863                         /* Store range marker */
864                         curs->c_put( curs, &key, &data, DB_KEYFIRST );
865                 } else {
866                         
867                         /* Skip lo */
868                         rc = curs->c_get( curs, &key, &data, DB_NEXT_DUP );
869
870                         /* Get hi */
871                         rc = curs->c_get( curs, &key, &data, DB_NEXT_DUP );
872
873                         /* Delete hi */
874                         curs->c_del( curs, 0 );
875                 }
876                 BDB_ID2DISK( ic->last, &nid );
877                 curs->c_put( curs, &key, &data, DB_KEYLAST );
878                 rc = 0;
879         } else if ( rc && rc != DB_NOTFOUND ) {
880                 rc = -1;
881         } else if ( ic->count > BDB_IDL_DB_SIZE ) {
882                 /* range, didn't exist before */
883                 nid = 0;
884                 rc = curs->c_put( curs, &key, &data, DB_KEYLAST );
885                 if ( rc == 0 ) {
886                         BDB_ID2DISK( ic->first, &nid );
887                         rc = curs->c_put( curs, &key, &data, DB_KEYLAST );
888                         if ( rc == 0 ) {
889                                 BDB_ID2DISK( ic->last, &nid );
890                                 rc = curs->c_put( curs, &key, &data, DB_KEYLAST );
891                         }
892                 }
893                 if ( rc ) {
894                         rc = -1;
895                 }
896         } else {
897                 int n;
898
899                 /* Just a normal write */
900                 rc = 0;
901                 for ( ice = ic->head, n=0; ice; ice = ice->next, n++ ) {
902                         int end;
903                         if ( ice->next ) {
904                                 end = IDBLOCK;
905                         } else {
906                                 end = ic->count & (IDBLOCK-1);
907                                 if ( !end )
908                                         end = IDBLOCK;
909                         }
910                         for ( i=0; i<end; i++ ) {
911                                 if ( !ice->ids[i] ) continue;
912                                 BDB_ID2DISK( ice->ids[i], &nid );
913                                 rc = curs->c_put( curs, &key, &data, DB_NODUPDATA );
914                                 if ( rc ) {
915                                         if ( rc == DB_KEYEXIST ) {
916                                                 rc = 0;
917                                                 continue;
918                                         }
919                                         rc = -1;
920                                         break;
921                                 }
922                         }
923                         if ( rc ) {
924                                 rc = -1;
925                                 break;
926                         }
927                 }
928                 if ( ic->head ) {
929                         ldap_pvt_thread_mutex_lock( &bdb->bi_idl_tree_lrulock );
930                         ic->tail->next = bdb_tool_idl_free_list;
931                         bdb_tool_idl_free_list = ic->head;
932                         bdb->bi_idl_cache_size -= n;
933                         ldap_pvt_thread_mutex_unlock( &bdb->bi_idl_tree_lrulock );
934                 }
935         }
936         if ( ic != db->app_private ) {
937                 ch_free( ic );
938         } else {
939                 ic->head = ic->tail = NULL;
940         }
941         curs->c_close( curs );
942         return rc;
943 }
944
945 static int
946 bdb_tool_idl_flush_db( DB *db, bdb_tool_idl_cache *ic )
947 {
948         Avlnode *root = db->app_private;
949         int rc;
950
951         db->app_private = ic;
952         rc = avl_apply( root, bdb_tool_idl_flush_one, db, -1, AVL_INORDER );
953         avl_free( root, NULL );
954         db->app_private = NULL;
955         if ( rc != -1 )
956                 rc = 0;
957         return rc;
958 }
959
960 static int
961 bdb_tool_idl_flush( BackendDB *be )
962 {
963         struct bdb_info *bdb = (struct bdb_info *) be->be_private;
964         DB *db;
965         Avlnode *root;
966         int i, rc = 0;
967
968         for ( i=BDB_NDB; i < bdb->bi_ndatabases; i++ ) {
969                 db = bdb->bi_databases[i]->bdi_db;
970                 if ( !db->app_private ) continue;
971                 rc = bdb_tool_idl_flush_db( db, NULL );
972                 if ( rc )
973                         break;
974         }
975         if ( !rc ) {
976                 bdb->bi_idl_cache_size = 0;
977         }
978         return rc;
979 }
980
981 int bdb_tool_idl_add(
982         BackendDB *be,
983         DB *db,
984         DB_TXN *txn,
985         DBT *key,
986         ID id )
987 {
988         struct bdb_info *bdb = (struct bdb_info *) be->be_private;
989         bdb_tool_idl_cache *ic, itmp;
990         bdb_tool_idl_cache_entry *ice;
991         int rc;
992
993         if ( !bdb->bi_idl_cache_max_size )
994                 return bdb_idl_insert_key( be, db, txn, key, id );
995
996         DBT2bv( key, &itmp.kstr );
997
998         ic = avl_find( (Avlnode *)db->app_private, &itmp, bdb_tool_idl_cmp );
999
1000         /* No entry yet, create one */
1001         if ( !ic ) {
1002                 DBC *curs;
1003                 DBT data;
1004                 ID nid;
1005                 int rc;
1006
1007                 ic = ch_malloc( sizeof( bdb_tool_idl_cache ) + itmp.kstr.bv_len );
1008                 ic->kstr.bv_len = itmp.kstr.bv_len;
1009                 ic->kstr.bv_val = (char *)(ic+1);
1010                 AC_MEMCPY( ic->kstr.bv_val, itmp.kstr.bv_val, ic->kstr.bv_len );
1011                 ic->head = ic->tail = NULL;
1012                 ic->last = 0;
1013                 ic->count = 0;
1014                 avl_insert( (Avlnode **)&db->app_private, ic, bdb_tool_idl_cmp,
1015                         avl_dup_error );
1016
1017                 /* load existing key count here */
1018                 rc = db->cursor( db, NULL, &curs, 0 );
1019                 if ( rc ) return rc;
1020
1021                 data.ulen = sizeof( ID );
1022                 data.flags = DB_DBT_USERMEM;
1023                 data.data = &nid;
1024                 rc = curs->c_get( curs, key, &data, DB_SET );
1025                 if ( rc == 0 ) {
1026                         if ( nid == 0 ) {
1027                                 ic->count = BDB_IDL_DB_SIZE+1;
1028                         } else {
1029                                 db_recno_t count;
1030
1031                                 curs->c_count( curs, &count, 0 );
1032                                 ic->count = count;
1033                                 BDB_DISK2ID( &nid, &ic->first );
1034                         }
1035                 }
1036                 curs->c_close( curs );
1037         }
1038         /* are we a range already? */
1039         if ( ic->count > BDB_IDL_DB_SIZE ) {
1040                 ic->last = id;
1041                 return 0;
1042         /* Are we at the limit, and converting to a range? */
1043         } else if ( ic->count == BDB_IDL_DB_SIZE ) {
1044                 int n;
1045                 for ( ice = ic->head, n=0; ice; ice = ice->next, n++ )
1046                         /* counting */ ;
1047                 if ( n ) {
1048                         ldap_pvt_thread_mutex_lock( &bdb->bi_idl_tree_lrulock );
1049                         ic->tail->next = bdb_tool_idl_free_list;
1050                         bdb_tool_idl_free_list = ic->head;
1051                         bdb->bi_idl_cache_size -= n;
1052                         ldap_pvt_thread_mutex_unlock( &bdb->bi_idl_tree_lrulock );
1053                 }
1054                 ic->head = ic->tail = NULL;
1055                 ic->last = id;
1056                 ic->count++;
1057                 return 0;
1058         }
1059         /* No free block, create that too */
1060         if ( !ic->tail || ( ic->count & (IDBLOCK-1)) == 0) {
1061                 ice = NULL;
1062                 ldap_pvt_thread_mutex_lock( &bdb->bi_idl_tree_lrulock );
1063                 if ( bdb->bi_idl_cache_size >= bdb->bi_idl_cache_max_size ) {
1064                         ldap_pvt_thread_mutex_unlock( &bdb->bi_idl_tree_lrulock );
1065                         rc = bdb_tool_idl_flush_db( db, ic );
1066                         if ( rc )
1067                                 return rc;
1068                         avl_insert( (Avlnode **)&db->app_private, ic, bdb_tool_idl_cmp,
1069                                 avl_dup_error );
1070                         ldap_pvt_thread_mutex_lock( &bdb->bi_idl_tree_lrulock );
1071                 }
1072                 bdb->bi_idl_cache_size++;
1073                 if ( bdb_tool_idl_free_list ) {
1074                         ice = bdb_tool_idl_free_list;
1075                         bdb_tool_idl_free_list = ice->next;
1076                 }
1077                 ldap_pvt_thread_mutex_unlock( &bdb->bi_idl_tree_lrulock );
1078                 if ( !ice ) {
1079                         ice = ch_malloc( sizeof( bdb_tool_idl_cache_entry ));
1080                 }
1081                 memset( ice, 0, sizeof( *ice ));
1082                 if ( !ic->head ) {
1083                         ic->head = ice;
1084                 } else {
1085                         ic->tail->next = ice;
1086                 }
1087                 ic->tail = ice;
1088                 if ( !ic->count )
1089                         ic->first = id;
1090         }
1091         ice = ic->tail;
1092         ice->ids[ ic->count & (IDBLOCK-1) ] = id;
1093         ic->count++;
1094
1095         return 0;
1096 }
1097 #endif
1098
1099 static void *
1100 bdb_tool_trickle_task( void *ctx, void *ptr )
1101 {
1102         DB_ENV *env = ptr;
1103         int wrote;
1104
1105         ldap_pvt_thread_mutex_lock( &bdb_tool_trickle_mutex );
1106         while ( 1 ) {
1107                 ldap_pvt_thread_cond_wait( &bdb_tool_trickle_cond,
1108                         &bdb_tool_trickle_mutex );
1109                 if ( slapd_shutdown )
1110                         break;
1111                 env->memp_trickle( env, 30, &wrote );
1112         }
1113         ldap_pvt_thread_mutex_unlock( &bdb_tool_trickle_mutex );
1114
1115         return NULL;
1116 }
1117
1118 static void *
1119 bdb_tool_index_task( void *ctx, void *ptr )
1120 {
1121         int base = *(int *)ptr;
1122
1123         free( ptr );
1124         while ( 1 ) {
1125                 ldap_pvt_thread_mutex_lock( &bdb_tool_index_mutex );
1126                 bdb_tool_index_tcount--;
1127                 if ( !bdb_tool_index_tcount )
1128                         ldap_pvt_thread_cond_signal( &bdb_tool_index_cond_main );
1129                 ldap_pvt_thread_cond_wait( &bdb_tool_index_cond_work,
1130                         &bdb_tool_index_mutex );
1131                 ldap_pvt_thread_mutex_unlock( &bdb_tool_index_mutex );
1132                 if ( slapd_shutdown )
1133                         break;
1134
1135                 bdb_tool_index_threads[base] = bdb_index_recrun( bdb_tool_ix_op,
1136                         bdb_tool_info, bdb_tool_index_rec, bdb_tool_ix_id, base );
1137         }
1138
1139         return NULL;
1140 }