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