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