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