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