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