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