]> git.sur5r.net Git - openldap/blob - servers/slapd/back-mdb/tools.c
Plug slapindex memleak
[openldap] / servers / slapd / back-mdb / 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 2011-2012 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-mdb.h"
25 #include "idl.h"
26
27 #ifdef MDB_TOOL_IDL_CACHING
28 static int mdb_tool_idl_flush( BackendDB *be, MDB_txn *txn );
29
30 #define IDBLOCK 1024
31
32 typedef struct mdb_tool_idl_cache_entry {
33         struct mdb_tool_idl_cache_entry *next;
34         ID ids[IDBLOCK];
35 } mdb_tool_idl_cache_entry;
36
37 typedef struct mdb_tool_idl_cache {
38         struct berval kstr;
39         mdb_tool_idl_cache_entry *head, *tail;
40         ID first, last;
41         int count;
42         short offset;
43         short flags;
44 } mdb_tool_idl_cache;
45 #define WAS_FOUND       0x01
46 #define WAS_RANGE       0x02
47
48 #define MDB_TOOL_IDL_FLUSH(be, txn)     mdb_tool_idl_flush(be, txn)
49 #else
50 #define MDB_TOOL_IDL_FLUSH(be, txn)
51 #endif /* MDB_TOOL_IDL_CACHING */
52
53 static MDB_txn *txn = NULL, *txi = NULL;
54 static MDB_cursor *cursor = NULL, *idcursor = NULL;
55 static MDB_cursor *mcp = NULL, *mcd = NULL;
56 static MDB_val key, data;
57 static ID previd = NOID;
58
59 typedef struct dn_id {
60         ID id;
61         struct berval dn;
62 } dn_id;
63
64 #define HOLE_SIZE       4096
65 static dn_id hbuf[HOLE_SIZE], *holes = hbuf;
66 static unsigned nhmax = HOLE_SIZE;
67 static unsigned nholes;
68
69 static struct berval    *tool_base;
70 static int              tool_scope;
71 static Filter           *tool_filter;
72 static Entry            *tool_next_entry;
73
74 static ID mdb_tool_ix_id;
75 static Operation *mdb_tool_ix_op;
76 static MDB_txn *mdb_tool_ix_txn;
77 static int mdb_tool_index_tcount, mdb_tool_threads;
78 static IndexRec *mdb_tool_index_rec;
79 static struct mdb_info *mdb_tool_info;
80 static ldap_pvt_thread_mutex_t mdb_tool_index_mutex;
81 static ldap_pvt_thread_cond_t mdb_tool_index_cond_main;
82 static ldap_pvt_thread_cond_t mdb_tool_index_cond_work;
83 static void * mdb_tool_index_task( void *ctx, void *ptr );
84
85 static int      mdb_writes, mdb_writes_per_commit;
86
87 static int
88 mdb_tool_entry_get_int( BackendDB *be, ID id, Entry **ep );
89
90 int mdb_tool_entry_open(
91         BackendDB *be, int mode )
92 {
93         /* In Quick mode, commit once per 1000 entries */
94         mdb_writes = 0;
95         if ( slapMode & SLAP_TOOL_QUICK )
96                 mdb_writes_per_commit = 1000;
97         else
98                 mdb_writes_per_commit = 1;
99
100         /* Set up for threaded slapindex */
101         if (( slapMode & (SLAP_TOOL_QUICK|SLAP_TOOL_READONLY)) == SLAP_TOOL_QUICK ) {
102                 if ( !mdb_tool_info ) {
103                         struct mdb_info *mdb = (struct mdb_info *) be->be_private;
104                         ldap_pvt_thread_mutex_init( &mdb_tool_index_mutex );
105                         ldap_pvt_thread_cond_init( &mdb_tool_index_cond_main );
106                         ldap_pvt_thread_cond_init( &mdb_tool_index_cond_work );
107                         if ( mdb->mi_nattrs ) {
108                                 int i;
109                                 mdb_tool_threads = slap_tool_thread_max - 1;
110                                 if ( mdb_tool_threads > 1 ) {
111                                         mdb_tool_index_rec = ch_calloc( mdb->mi_nattrs, sizeof( IndexRec ));
112                                         mdb_tool_index_tcount = mdb_tool_threads - 1;
113                                         for (i=1; i<mdb_tool_threads; i++) {
114                                                 int *ptr = ch_malloc( sizeof( int ));
115                                                 *ptr = i;
116                                                 ldap_pvt_thread_pool_submit( &connection_pool,
117                                                         mdb_tool_index_task, ptr );
118                                         }
119                                         mdb_tool_info = mdb;
120                                 }
121                         }
122                 }
123         }
124
125         return 0;
126 }
127
128 int mdb_tool_entry_close(
129         BackendDB *be )
130 {
131         if ( mdb_tool_info ) {
132                 slapd_shutdown = 1;
133                 ldap_pvt_thread_mutex_lock( &mdb_tool_index_mutex );
134
135                 /* There might still be some threads starting */
136                 while ( mdb_tool_index_tcount > 0 ) {
137                         ldap_pvt_thread_cond_wait( &mdb_tool_index_cond_main,
138                                         &mdb_tool_index_mutex );
139                 }
140
141                 mdb_tool_index_tcount = mdb_tool_threads - 1;
142                 ldap_pvt_thread_cond_broadcast( &mdb_tool_index_cond_work );
143
144                 /* Make sure all threads are stopped */
145                 while ( mdb_tool_index_tcount > 0 ) {
146                         ldap_pvt_thread_cond_wait( &mdb_tool_index_cond_main,
147                                 &mdb_tool_index_mutex );
148                 }
149                 ldap_pvt_thread_mutex_unlock( &mdb_tool_index_mutex );
150
151                 mdb_tool_info = NULL;
152                 slapd_shutdown = 0;
153                 ch_free( mdb_tool_index_rec );
154                 mdb_tool_index_tcount = mdb_tool_threads - 1;
155         }
156
157         if( idcursor ) {
158                 mdb_cursor_close( idcursor );
159                 idcursor = NULL;
160         }
161         if( cursor ) {
162                 mdb_cursor_close( cursor );
163                 cursor = NULL;
164         }
165         if( txn ) {
166                 MDB_TOOL_IDL_FLUSH( be, txn );
167                 if ( mdb_txn_commit( txn ))
168                         return -1;
169                 txn = NULL;
170         }
171
172         if( nholes ) {
173                 unsigned i;
174                 fprintf( stderr, "Error, entries missing!\n");
175                 for (i=0; i<nholes; i++) {
176                         fprintf(stderr, "  entry %ld: %s\n",
177                                 holes[i].id, holes[i].dn.bv_val);
178                 }
179                 nholes = 0;
180                 return -1;
181         }
182
183         return 0;
184 }
185
186 ID
187 mdb_tool_entry_first_x(
188         BackendDB *be,
189         struct berval *base,
190         int scope,
191         Filter *f )
192 {
193         tool_base = base;
194         tool_scope = scope;
195         tool_filter = f;
196
197         return mdb_tool_entry_next( be );
198 }
199
200 ID mdb_tool_entry_next(
201         BackendDB *be )
202 {
203         int rc;
204         ID id;
205         struct mdb_info *mdb;
206
207         assert( be != NULL );
208         assert( slapMode & SLAP_TOOL_MODE );
209
210         mdb = (struct mdb_info *) be->be_private;
211         assert( mdb != NULL );
212
213         if ( !txn ) {
214                 rc = mdb_txn_begin( mdb->mi_dbenv, NULL, MDB_RDONLY, &txn );
215                 if ( rc )
216                         return NOID;
217                 rc = mdb_cursor_open( txn, mdb->mi_id2entry, &cursor );
218                 if ( rc ) {
219                         mdb_txn_abort( txn );
220                         return NOID;
221                 }
222         }
223
224 next:;
225         rc = mdb_cursor_get( cursor, &key, &data, MDB_NEXT );
226
227         if( rc ) {
228                 return NOID;
229         }
230
231         previd = *(ID *)key.mv_data;
232         id = previd;
233
234         if ( tool_filter || tool_base ) {
235                 static Operation op = {0};
236                 static Opheader ohdr = {0};
237
238                 op.o_hdr = &ohdr;
239                 op.o_bd = be;
240                 op.o_tmpmemctx = NULL;
241                 op.o_tmpmfuncs = &ch_mfuncs;
242
243                 if ( tool_next_entry ) {
244                         mdb_entry_release( &op, tool_next_entry, 0 );
245                         tool_next_entry = NULL;
246                 }
247
248                 rc = mdb_tool_entry_get_int( be, id, &tool_next_entry );
249                 if ( rc == LDAP_NO_SUCH_OBJECT ) {
250                         goto next;
251                 }
252
253                 assert( tool_next_entry != NULL );
254
255                 if ( tool_filter && test_filter( NULL, tool_next_entry, tool_filter ) != LDAP_COMPARE_TRUE )
256                 {
257                         mdb_entry_release( &op, tool_next_entry, 0 );
258                         tool_next_entry = NULL;
259                         goto next;
260                 }
261         }
262
263         return id;
264 }
265
266 ID mdb_tool_dn2id_get(
267         Backend *be,
268         struct berval *dn
269 )
270 {
271         struct mdb_info *mdb;
272         Operation op = {0};
273         Opheader ohdr = {0};
274         ID id;
275         int rc;
276
277         if ( BER_BVISEMPTY(dn) )
278                 return 0;
279
280         mdb = (struct mdb_info *) be->be_private;
281
282         if ( !txn ) {
283                 rc = mdb_txn_begin( mdb->mi_dbenv, NULL, (slapMode & SLAP_TOOL_READONLY) != 0 ?
284                         MDB_RDONLY : 0, &txn );
285                 if ( rc )
286                         return NOID;
287         }
288
289         op.o_hdr = &ohdr;
290         op.o_bd = be;
291         op.o_tmpmemctx = NULL;
292         op.o_tmpmfuncs = &ch_mfuncs;
293
294         rc = mdb_dn2id( &op, txn, NULL, dn, &id, NULL, NULL );
295         if ( rc == MDB_NOTFOUND )
296                 return NOID;
297
298         return id;
299 }
300
301 static int
302 mdb_tool_entry_get_int( BackendDB *be, ID id, Entry **ep )
303 {
304         Operation op = {0};
305         Opheader ohdr = {0};
306
307         Entry *e = NULL;
308         struct berval dn = BER_BVNULL, ndn = BER_BVNULL;
309         int rc;
310
311         assert( be != NULL );
312         assert( slapMode & SLAP_TOOL_MODE );
313
314         if ( ( tool_filter || tool_base ) && id == previd && tool_next_entry != NULL ) {
315                 *ep = tool_next_entry;
316                 tool_next_entry = NULL;
317                 return LDAP_SUCCESS;
318         }
319
320         if ( id != previd ) {
321                 key.mv_size = sizeof(ID);
322                 key.mv_data = &id;
323                 rc = mdb_cursor_get( cursor, &key, &data, MDB_SET );
324                 if ( rc ) {
325                         rc = LDAP_OTHER;
326                         goto done;
327                 }
328         }
329
330         op.o_hdr = &ohdr;
331         op.o_bd = be;
332         op.o_tmpmemctx = NULL;
333         op.o_tmpmfuncs = &ch_mfuncs;
334         if ( slapMode & SLAP_TOOL_READONLY ) {
335                 rc = mdb_id2name( &op, txn, &idcursor, id, &dn, &ndn );
336                 if ( rc  ) {
337                         rc = LDAP_OTHER;
338                         if ( e ) {
339                                 mdb_entry_return( &op, e );
340                                 e = NULL;
341                         }
342                         goto done;
343                 }
344                 if ( tool_base != NULL ) {
345                         if ( !dnIsSuffixScope( &ndn, tool_base, tool_scope ) ) {
346                                 ch_free( dn.bv_val );
347                                 ch_free( ndn.bv_val );
348                                 rc = LDAP_NO_SUCH_OBJECT;
349                         }
350                 }
351         }
352         rc = mdb_entry_decode( &op, &data, &e );
353         e->e_id = id;
354         if ( !BER_BVISNULL( &dn )) {
355                 e->e_name = dn;
356                 e->e_nname = ndn;
357         } else {
358                 e->e_name.bv_val = NULL;
359                 e->e_nname.bv_val = NULL;
360         }
361
362 done:
363         if ( e != NULL ) {
364                 *ep = e;
365         }
366
367         return rc;
368 }
369
370 Entry*
371 mdb_tool_entry_get( BackendDB *be, ID id )
372 {
373         Entry *e = NULL;
374         int rc;
375
376         if ( !txn ) {
377                 struct mdb_info *mdb = (struct mdb_info *) be->be_private;
378                 rc = mdb_txn_begin( mdb->mi_dbenv, NULL,
379                         (slapMode & SLAP_TOOL_READONLY) ? MDB_RDONLY : 0, &txn );
380                 if ( rc )
381                         return NULL;
382         }
383         if ( !cursor ) {
384                 struct mdb_info *mdb = (struct mdb_info *) be->be_private;
385                 rc = mdb_cursor_open( txn, mdb->mi_id2entry, &cursor );
386                 if ( rc ) {
387                         mdb_txn_abort( txn );
388                         txn = NULL;
389                         return NULL;
390                 }
391         }
392         (void)mdb_tool_entry_get_int( be, id, &e );
393         return e;
394 }
395
396 static int mdb_tool_next_id(
397         Operation *op,
398         MDB_txn *tid,
399         Entry *e,
400         struct berval *text,
401         int hole )
402 {
403         struct mdb_info *mdb = (struct mdb_info *) op->o_bd->be_private;
404         struct berval dn = e->e_name;
405         struct berval ndn = e->e_nname;
406         struct berval pdn, npdn, nmatched;
407         ID id, pid = 0;
408         int rc;
409
410         if (ndn.bv_len == 0) {
411                 e->e_id = 0;
412                 return 0;
413         }
414
415         rc = mdb_dn2id( op, tid, mcp, &ndn, &id, NULL, &nmatched );
416         if ( rc == MDB_NOTFOUND ) {
417                 if ( !be_issuffix( op->o_bd, &ndn ) ) {
418                         ID eid = e->e_id;
419                         dnParent( &ndn, &npdn );
420                         if ( nmatched.bv_len != npdn.bv_len ) {
421                                 dnParent( &dn, &pdn );
422                                 e->e_name = pdn;
423                                 e->e_nname = npdn;
424                                 rc = mdb_tool_next_id( op, tid, e, text, 1 );
425                                 e->e_name = dn;
426                                 e->e_nname = ndn;
427                                 if ( rc ) {
428                                         return rc;
429                                 }
430                                 /* If parent didn't exist, it was created just now
431                                  * and its ID is now in e->e_id. Make sure the current
432                                  * entry gets added under the new parent ID.
433                                  */
434                                 if ( eid != e->e_id ) {
435                                         pid = e->e_id;
436                                 }
437                         } else {
438                                 pid = id;
439                         }
440                 }
441                 rc = mdb_next_id( op->o_bd, idcursor, &e->e_id );
442                 if ( rc ) {
443                         snprintf( text->bv_val, text->bv_len,
444                                 "next_id failed: %s (%d)",
445                                 mdb_strerror(rc), rc );
446                 Debug( LDAP_DEBUG_ANY,
447                         "=> mdb_tool_next_id: %s\n", text->bv_val, 0, 0 );
448                         return rc;
449                 }
450                 rc = mdb_dn2id_add( op, mcp, mcd, pid, e );
451                 if ( rc ) {
452                         snprintf( text->bv_val, text->bv_len,
453                                 "dn2id_add failed: %s (%d)",
454                                 mdb_strerror(rc), rc );
455                         Debug( LDAP_DEBUG_ANY,
456                                 "=> mdb_tool_next_id: %s\n", text->bv_val, 0, 0 );
457                 } else if ( hole ) {
458                         MDB_val key, data;
459                         if ( nholes == nhmax - 1 ) {
460                                 if ( holes == hbuf ) {
461                                         holes = ch_malloc( nhmax * sizeof(dn_id) * 2 );
462                                         AC_MEMCPY( holes, hbuf, sizeof(hbuf) );
463                                 } else {
464                                         holes = ch_realloc( holes, nhmax * sizeof(dn_id) * 2 );
465                                 }
466                                 nhmax *= 2;
467                         }
468                         ber_dupbv( &holes[nholes].dn, &ndn );
469                         holes[nholes++].id = e->e_id;
470                         key.mv_size = sizeof(ID);
471                         key.mv_data = &e->e_id;
472                         data.mv_size = 0;
473                         data.mv_data = NULL;
474                         rc = mdb_cursor_put( idcursor, &key, &data, MDB_NOOVERWRITE );
475                         if ( rc == MDB_KEYEXIST )
476                                 rc = 0;
477                         if ( rc ) {
478                                 snprintf( text->bv_val, text->bv_len,
479                                         "dummy id2entry add failed: %s (%d)",
480                                         mdb_strerror(rc), rc );
481                                 Debug( LDAP_DEBUG_ANY,
482                                         "=> mdb_tool_next_id: %s\n", text->bv_val, 0, 0 );
483                         }
484                 }
485         } else if ( !hole ) {
486                 unsigned i, j;
487
488                 e->e_id = id;
489
490                 for ( i=0; i<nholes; i++) {
491                         if ( holes[i].id == e->e_id ) {
492                                 free(holes[i].dn.bv_val);
493                                 for (j=i;j<nholes;j++) holes[j] = holes[j+1];
494                                 holes[j].id = 0;
495                                 nholes--;
496                                 break;
497                         } else if ( holes[i].id > e->e_id ) {
498                                 break;
499                         }
500                 }
501         }
502         return rc;
503 }
504
505 static int
506 mdb_tool_index_add(
507         Operation *op,
508         MDB_txn *txn,
509         Entry *e )
510 {
511         struct mdb_info *mdb = (struct mdb_info *) op->o_bd->be_private;
512
513         if ( !mdb->mi_nattrs )
514                 return 0;
515
516         if ( mdb_tool_threads > 1 ) {
517                 IndexRec *ir;
518                 int i, rc;
519                 Attribute *a;
520
521                 ir = mdb_tool_index_rec;
522                 for (i=0; i<mdb->mi_nattrs; i++)
523                         ir[i].ir_attrs = NULL;
524
525                 for ( a = e->e_attrs; a != NULL; a = a->a_next ) {
526                         rc = mdb_index_recset( mdb, a, a->a_desc->ad_type,
527                                 &a->a_desc->ad_tags, ir );
528                         if ( rc )
529                                 return rc;
530                 }
531                 for (i=0; i<mdb->mi_nattrs; i++) {
532                         if ( !ir[i].ir_ai )
533                                 break;
534                         rc = mdb_cursor_open( txn, ir[i].ir_ai->ai_dbi,
535                                  &ir[i].ir_ai->ai_cursor );
536                         if ( rc )
537                                 return rc;
538                 }
539                 mdb_tool_ix_id = e->e_id;
540                 mdb_tool_ix_op = op;
541                 mdb_tool_ix_txn = txn;
542                 ldap_pvt_thread_mutex_lock( &mdb_tool_index_mutex );
543                 /* Wait for all threads to be ready */
544                 while ( mdb_tool_index_tcount ) {
545                         ldap_pvt_thread_cond_wait( &mdb_tool_index_cond_main,
546                                 &mdb_tool_index_mutex );
547                 }
548
549                 for ( i=1; i<mdb_tool_threads; i++ )
550                         mdb_tool_index_rec[i].ir_i = LDAP_BUSY;
551                 mdb_tool_index_tcount = mdb_tool_threads - 1;
552                 ldap_pvt_thread_mutex_unlock( &mdb_tool_index_mutex );
553                 ldap_pvt_thread_cond_broadcast( &mdb_tool_index_cond_work );
554
555                 rc = mdb_index_recrun( op, txn, mdb, ir, e->e_id, 0 );
556                 if ( rc )
557                         return rc;
558                 ldap_pvt_thread_mutex_lock( &mdb_tool_index_mutex );
559                 for ( i=1; i<mdb_tool_threads; i++ ) {
560                         if ( mdb_tool_index_rec[i].ir_i == LDAP_BUSY ) {
561                                 ldap_pvt_thread_cond_wait( &mdb_tool_index_cond_main,
562                                         &mdb_tool_index_mutex );
563                                 i--;
564                                 continue;
565                         }
566                         if ( mdb_tool_index_rec[i].ir_i ) {
567                                 rc = mdb_tool_index_rec[i].ir_i;
568                                 break;
569                         }
570                 }
571                 ldap_pvt_thread_mutex_unlock( &mdb_tool_index_mutex );
572                 return rc;
573         } else
574         {
575                 return mdb_index_entry_add( op, txn, e );
576         }
577 }
578
579 ID mdb_tool_entry_put(
580         BackendDB *be,
581         Entry *e,
582         struct berval *text )
583 {
584         int rc;
585         struct mdb_info *mdb;
586         Operation op = {0};
587         Opheader ohdr = {0};
588
589         assert( be != NULL );
590         assert( slapMode & SLAP_TOOL_MODE );
591
592         assert( text != NULL );
593         assert( text->bv_val != NULL );
594         assert( text->bv_val[0] == '\0' );      /* overconservative? */
595
596         Debug( LDAP_DEBUG_TRACE, "=> " LDAP_XSTRING(mdb_tool_entry_put)
597                 "( %ld, \"%s\" )\n", (long) e->e_id, e->e_dn, 0 );
598
599         mdb = (struct mdb_info *) be->be_private;
600
601         if ( !txn ) {
602                 rc = mdb_txn_begin( mdb->mi_dbenv, NULL, 0, &txn );
603                 if( rc != 0 ) {
604                         snprintf( text->bv_val, text->bv_len,
605                                 "txn_begin failed: %s (%d)",
606                                 mdb_strerror(rc), rc );
607                         Debug( LDAP_DEBUG_ANY,
608                                 "=> " LDAP_XSTRING(mdb_tool_entry_put) ": %s\n",
609                                  text->bv_val, 0, 0 );
610                         return NOID;
611                 }
612                 rc = mdb_cursor_open( txn, mdb->mi_id2entry, &idcursor );
613                 if( rc != 0 ) {
614                         snprintf( text->bv_val, text->bv_len,
615                                 "cursor_open failed: %s (%d)",
616                                 mdb_strerror(rc), rc );
617                         Debug( LDAP_DEBUG_ANY,
618                                 "=> " LDAP_XSTRING(mdb_tool_entry_put) ": %s\n",
619                                  text->bv_val, 0, 0 );
620                         return NOID;
621                 }
622                 if ( !mdb->mi_nextid ) {
623                         ID dummy;
624                         mdb_next_id( be, idcursor, &dummy );
625                 }
626                 rc = mdb_cursor_open( txn, mdb->mi_dn2id, &mcp );
627                 if( rc != 0 ) {
628                         snprintf( text->bv_val, text->bv_len,
629                                 "cursor_open failed: %s (%d)",
630                                 mdb_strerror(rc), rc );
631                         Debug( LDAP_DEBUG_ANY,
632                                 "=> " LDAP_XSTRING(mdb_tool_entry_put) ": %s\n",
633                                  text->bv_val, 0, 0 );
634                         return NOID;
635                 }
636                 rc = mdb_cursor_open( txn, mdb->mi_dn2id, &mcd );
637                 if( rc != 0 ) {
638                         snprintf( text->bv_val, text->bv_len,
639                                 "cursor_open failed: %s (%d)",
640                                 mdb_strerror(rc), rc );
641                         Debug( LDAP_DEBUG_ANY,
642                                 "=> " LDAP_XSTRING(mdb_tool_entry_put) ": %s\n",
643                                  text->bv_val, 0, 0 );
644                         return NOID;
645                 }
646         }
647
648         op.o_hdr = &ohdr;
649         op.o_bd = be;
650         op.o_tmpmemctx = NULL;
651         op.o_tmpmfuncs = &ch_mfuncs;
652
653         /* add dn2id indices */
654         rc = mdb_tool_next_id( &op, txn, e, text, 0 );
655         if( rc != 0 ) {
656                 goto done;
657         }
658
659         rc = mdb_tool_index_add( &op, txn, e );
660         if( rc != 0 ) {
661                 snprintf( text->bv_val, text->bv_len,
662                                 "index_entry_add failed: err=%d", rc );
663                 Debug( LDAP_DEBUG_ANY,
664                         "=> " LDAP_XSTRING(mdb_tool_entry_put) ": %s\n",
665                         text->bv_val, 0, 0 );
666                 goto done;
667         }
668
669
670         /* id2entry index */
671         rc = mdb_id2entry_add( &op, txn, idcursor, e );
672         if( rc != 0 ) {
673                 snprintf( text->bv_val, text->bv_len,
674                                 "id2entry_add failed: err=%d", rc );
675                 Debug( LDAP_DEBUG_ANY,
676                         "=> " LDAP_XSTRING(mdb_tool_entry_put) ": %s\n",
677                         text->bv_val, 0, 0 );
678                 goto done;
679         }
680
681 done:
682         if( rc == 0 ) {
683                 mdb_writes++;
684                 if ( mdb_writes >= mdb_writes_per_commit ) {
685                         unsigned i;
686                         MDB_TOOL_IDL_FLUSH( be, txn );
687                         rc = mdb_txn_commit( txn );
688                         for ( i=0; i<mdb->mi_nattrs; i++ )
689                                 mdb->mi_attrs[i]->ai_cursor = NULL;
690                         mdb_writes = 0;
691                         txn = NULL;
692                         idcursor = NULL;
693                         if( rc != 0 ) {
694                                 snprintf( text->bv_val, text->bv_len,
695                                                 "txn_commit failed: %s (%d)",
696                                                 mdb_strerror(rc), rc );
697                                 Debug( LDAP_DEBUG_ANY,
698                                         "=> " LDAP_XSTRING(mdb_tool_entry_put) ": %s\n",
699                                         text->bv_val, 0, 0 );
700                                 e->e_id = NOID;
701                         }
702                 }
703
704         } else {
705                 unsigned i;
706                 mdb_txn_abort( txn );
707                 txn = NULL;
708                 idcursor = NULL;
709                 for ( i=0; i<mdb->mi_nattrs; i++ )
710                         mdb->mi_attrs[i]->ai_cursor = NULL;
711                 mdb_writes = 0;
712                 snprintf( text->bv_val, text->bv_len,
713                         "txn_aborted! %s (%d)",
714                         rc == LDAP_OTHER ? "Internal error" :
715                         mdb_strerror(rc), rc );
716                 Debug( LDAP_DEBUG_ANY,
717                         "=> " LDAP_XSTRING(mdb_tool_entry_put) ": %s\n",
718                         text->bv_val, 0, 0 );
719                 e->e_id = NOID;
720         }
721
722         return e->e_id;
723 }
724
725 int mdb_tool_entry_reindex(
726         BackendDB *be,
727         ID id,
728         AttributeDescription **adv )
729 {
730         struct mdb_info *mi = (struct mdb_info *) be->be_private;
731         int rc;
732         Entry *e;
733         Operation op = {0};
734         Opheader ohdr = {0};
735
736         Debug( LDAP_DEBUG_ARGS,
737                 "=> " LDAP_XSTRING(mdb_tool_entry_reindex) "( %ld )\n",
738                 (long) id, 0, 0 );
739         assert( tool_base == NULL );
740         assert( tool_filter == NULL );
741
742         /* No indexes configured, nothing to do. Could return an
743          * error here to shortcut things.
744          */
745         if (!mi->mi_attrs) {
746                 return 0;
747         }
748
749         /* Check for explicit list of attrs to index */
750         if ( adv ) {
751                 int i, j, n;
752
753                 if ( mi->mi_attrs[0]->ai_desc != adv[0] ) {
754                         /* count */
755                         for ( n = 0; adv[n]; n++ ) ;
756
757                         /* insertion sort */
758                         for ( i = 0; i < n; i++ ) {
759                                 AttributeDescription *ad = adv[i];
760                                 for ( j = i-1; j>=0; j--) {
761                                         if ( SLAP_PTRCMP( adv[j], ad ) <= 0 ) break;
762                                         adv[j+1] = adv[j];
763                                 }
764                                 adv[j+1] = ad;
765                         }
766                 }
767
768                 for ( i = 0; adv[i]; i++ ) {
769                         if ( mi->mi_attrs[i]->ai_desc != adv[i] ) {
770                                 for ( j = i+1; j < mi->mi_nattrs; j++ ) {
771                                         if ( mi->mi_attrs[j]->ai_desc == adv[i] ) {
772                                                 AttrInfo *ai = mi->mi_attrs[i];
773                                                 mi->mi_attrs[i] = mi->mi_attrs[j];
774                                                 mi->mi_attrs[j] = ai;
775                                                 break;
776                                         }
777                                 }
778                                 if ( j == mi->mi_nattrs ) {
779                                         Debug( LDAP_DEBUG_ANY,
780                                                 LDAP_XSTRING(mdb_tool_entry_reindex)
781                                                 ": no index configured for %s\n",
782                                                 adv[i]->ad_cname.bv_val, 0, 0 );
783                                         return -1;
784                                 }
785                         }
786                 }
787                 mi->mi_nattrs = i;
788         }
789
790         e = mdb_tool_entry_get( be, id );
791
792         if( e == NULL ) {
793                 Debug( LDAP_DEBUG_ANY,
794                         LDAP_XSTRING(mdb_tool_entry_reindex)
795                         ": could not locate id=%ld\n",
796                         (long) id, 0, 0 );
797                 return -1;
798         }
799
800         if ( !txi ) {
801                 rc = mdb_txn_begin( mi->mi_dbenv, NULL, 0, &txi );
802                 if( rc != 0 ) {
803                         Debug( LDAP_DEBUG_ANY,
804                                 "=> " LDAP_XSTRING(mdb_tool_entry_reindex) ": "
805                                 "txn_begin failed: %s (%d)\n",
806                                 mdb_strerror(rc), rc, 0 );
807                         goto done;
808                 }
809         }
810
811         if ( slapMode & SLAP_TRUNCATE_MODE ) {
812                 int i;
813                 for ( i=0; i < mi->mi_nattrs; i++ ) {
814                         rc = mdb_drop( txi, mi->mi_attrs[i]->ai_dbi, 0 );
815                         if ( rc ) {
816                                 Debug( LDAP_DEBUG_ANY,
817                                         LDAP_XSTRING(mdb_tool_entry_reindex)
818                                         ": (Truncate) mdb_drop(%s) failed: %s (%d)\n",
819                                         mi->mi_attrs[i]->ai_desc->ad_type->sat_cname.bv_val,
820                                         mdb_strerror(rc), rc );
821                                 return -1;
822                         }
823                 }
824                 slapMode ^= SLAP_TRUNCATE_MODE;
825         }
826
827         /*
828          * just (re)add them for now
829          * Use truncate mode to empty/reset index databases
830          */
831
832         Debug( LDAP_DEBUG_TRACE,
833                 "=> " LDAP_XSTRING(mdb_tool_entry_reindex) "( %ld )\n",
834                 (long) id, 0, 0 );
835
836         op.o_hdr = &ohdr;
837         op.o_bd = be;
838         op.o_tmpmemctx = NULL;
839         op.o_tmpmfuncs = &ch_mfuncs;
840
841         rc = mdb_tool_index_add( &op, txi, e );
842
843 done:
844         if( rc == 0 ) {
845                 mdb_writes++;
846                 if ( mdb_writes >= mdb_writes_per_commit ) {
847                         MDB_val key;
848                         unsigned i;
849                         MDB_TOOL_IDL_FLUSH( be, txi );
850                         rc = mdb_txn_commit( txi );
851                         mdb_writes = 0;
852                         for ( i=0; i<mi->mi_nattrs; i++ )
853                                 mi->mi_attrs[i]->ai_cursor = NULL;
854                         if( rc != 0 ) {
855                                 Debug( LDAP_DEBUG_ANY,
856                                         "=> " LDAP_XSTRING(mdb_tool_entry_reindex)
857                                         ": txn_commit failed: %s (%d)\n",
858                                         mdb_strerror(rc), rc, 0 );
859                                 e->e_id = NOID;
860                         }
861                         mdb_cursor_close( cursor );
862                         txi = NULL;
863                         /* Must close the read txn to allow old pages to be reclaimed. */
864                         mdb_txn_abort( txn );
865                         /* and then reopen it so that tool_entry_next still works. */
866                         mdb_txn_begin( mi->mi_dbenv, NULL, MDB_RDONLY, &txn );
867                         mdb_cursor_open( txn, mi->mi_id2entry, &cursor );
868                         key.mv_data = &id;
869                         key.mv_size = sizeof(ID);
870                         mdb_cursor_get( cursor, &key, NULL, MDB_SET );
871                 }
872
873         } else {
874                 unsigned i;
875                 mdb_writes = 0;
876                 mdb_cursor_close( cursor );
877                 cursor = NULL;
878                 mdb_txn_abort( txi );
879                 for ( i=0; i<mi->mi_nattrs; i++ )
880                         mi->mi_attrs[i]->ai_cursor = NULL;
881                 Debug( LDAP_DEBUG_ANY,
882                         "=> " LDAP_XSTRING(mdb_tool_entry_reindex)
883                         ": txn_aborted! err=%d\n",
884                         rc, 0, 0 );
885                 e->e_id = NOID;
886                 txi = NULL;
887         }
888         mdb_entry_release( &op, e, 0 );
889
890         return rc;
891 }
892
893 ID mdb_tool_entry_modify(
894         BackendDB *be,
895         Entry *e,
896         struct berval *text )
897 {
898         int rc;
899         struct mdb_info *mdb;
900         Operation op = {0};
901         Opheader ohdr = {0};
902
903         assert( be != NULL );
904         assert( slapMode & SLAP_TOOL_MODE );
905
906         assert( text != NULL );
907         assert( text->bv_val != NULL );
908         assert( text->bv_val[0] == '\0' );      /* overconservative? */
909
910         assert ( e->e_id != NOID );
911
912         Debug( LDAP_DEBUG_TRACE,
913                 "=> " LDAP_XSTRING(mdb_tool_entry_modify) "( %ld, \"%s\" )\n",
914                 (long) e->e_id, e->e_dn, 0 );
915
916         mdb = (struct mdb_info *) be->be_private;
917
918         if( cursor ) {
919                 mdb_cursor_close( cursor );
920                 cursor = NULL;
921         }
922         if ( !txn ) {
923                 rc = mdb_txn_begin( mdb->mi_dbenv, NULL, 0, &txn );
924                 if( rc != 0 ) {
925                         snprintf( text->bv_val, text->bv_len,
926                                 "txn_begin failed: %s (%d)",
927                                 mdb_strerror(rc), rc );
928                         Debug( LDAP_DEBUG_ANY,
929                                 "=> " LDAP_XSTRING(mdb_tool_entry_modify) ": %s\n",
930                                  text->bv_val, 0, 0 );
931                         return NOID;
932                 }
933         }
934
935         op.o_hdr = &ohdr;
936         op.o_bd = be;
937         op.o_tmpmemctx = NULL;
938         op.o_tmpmfuncs = &ch_mfuncs;
939
940         /* id2entry index */
941         rc = mdb_id2entry_update( &op, txn, NULL, e );
942         if( rc != 0 ) {
943                 snprintf( text->bv_val, text->bv_len,
944                                 "id2entry_update failed: err=%d", rc );
945                 Debug( LDAP_DEBUG_ANY,
946                         "=> " LDAP_XSTRING(mdb_tool_entry_modify) ": %s\n",
947                         text->bv_val, 0, 0 );
948                 goto done;
949         }
950
951 done:
952         if( rc == 0 ) {
953                 rc = mdb_txn_commit( txn );
954                 if( rc != 0 ) {
955                         snprintf( text->bv_val, text->bv_len,
956                                         "txn_commit failed: %s (%d)",
957                                         mdb_strerror(rc), rc );
958                         Debug( LDAP_DEBUG_ANY,
959                                 "=> " LDAP_XSTRING(mdb_tool_entry_modify) ": "
960                                 "%s\n", text->bv_val, 0, 0 );
961                         e->e_id = NOID;
962                 }
963
964         } else {
965                 mdb_txn_abort( txn );
966                 snprintf( text->bv_val, text->bv_len,
967                         "txn_aborted! %s (%d)",
968                         mdb_strerror(rc), rc );
969                 Debug( LDAP_DEBUG_ANY,
970                         "=> " LDAP_XSTRING(mdb_tool_entry_modify) ": %s\n",
971                         text->bv_val, 0, 0 );
972                 e->e_id = NOID;
973         }
974         txn = NULL;
975         idcursor = NULL;
976
977         return e->e_id;
978 }
979
980 static void *
981 mdb_tool_index_task( void *ctx, void *ptr )
982 {
983         int base = *(int *)ptr;
984
985         free( ptr );
986         while ( 1 ) {
987                 ldap_pvt_thread_mutex_lock( &mdb_tool_index_mutex );
988                 mdb_tool_index_tcount--;
989                 if ( !mdb_tool_index_tcount )
990                         ldap_pvt_thread_cond_signal( &mdb_tool_index_cond_main );
991                 ldap_pvt_thread_cond_wait( &mdb_tool_index_cond_work,
992                         &mdb_tool_index_mutex );
993                 if ( slapd_shutdown ) {
994                         mdb_tool_index_tcount--;
995                         if ( !mdb_tool_index_tcount )
996                                 ldap_pvt_thread_cond_signal( &mdb_tool_index_cond_main );
997                         ldap_pvt_thread_mutex_unlock( &mdb_tool_index_mutex );
998                         break;
999                 }
1000                 ldap_pvt_thread_mutex_unlock( &mdb_tool_index_mutex );
1001                 mdb_tool_index_rec[base].ir_i = mdb_index_recrun( mdb_tool_ix_op,
1002                         mdb_tool_ix_txn,
1003                         mdb_tool_info, mdb_tool_index_rec, mdb_tool_ix_id, base );
1004         }
1005
1006         return NULL;
1007 }
1008
1009 #ifdef MDB_TOOL_IDL_CACHING
1010 static int
1011 mdb_tool_idl_cmp( const void *v1, const void *v2 )
1012 {
1013         const mdb_tool_idl_cache *c1 = v1, *c2 = v2;
1014         int rc;
1015
1016         if (( rc = c1->kstr.bv_len - c2->kstr.bv_len )) return rc;
1017         return memcmp( c1->kstr.bv_val, c2->kstr.bv_val, c1->kstr.bv_len );
1018 }
1019
1020 static int
1021 mdb_tool_idl_flush_one( MDB_cursor *mc, AttrInfo *ai, mdb_tool_idl_cache *ic )
1022 {
1023         mdb_tool_idl_cache_entry *ice;
1024         MDB_val key, data[2];
1025         int i, rc;
1026         ID id, nid;
1027
1028         /* Freshly allocated, ignore it */
1029         if ( !ic->head && ic->count <= MDB_IDL_DB_SIZE ) {
1030                 return 0;
1031         }
1032
1033         key.mv_data = ic->kstr.bv_val;
1034         key.mv_size = ic->kstr.bv_len;
1035
1036         if ( ic->count > MDB_IDL_DB_SIZE ) {
1037                 while ( ic->flags & WAS_FOUND ) {
1038                         rc = mdb_cursor_get( mc, &key, data, MDB_SET );
1039                         if ( rc ) {
1040                                 /* FIXME: find out why this happens */
1041                                 ic->flags = 0;
1042                                 break;
1043                         }
1044                         if ( ic->flags & WAS_RANGE ) {
1045                                 /* Skip lo */
1046                                 rc = mdb_cursor_get( mc, &key, data, MDB_NEXT_DUP );
1047
1048                                 /* Get hi */
1049                                 rc = mdb_cursor_get( mc, &key, data, MDB_NEXT_DUP );
1050
1051                                 /* Store range hi */
1052                                 data[0].mv_data = &ic->last;
1053                                 rc = mdb_cursor_put( mc, &key, data, MDB_CURRENT );
1054                         } else {
1055                                 /* Delete old data, replace with range */
1056                                 ic->first = *(ID *)data[0].mv_data;
1057                                 mdb_cursor_del( mc, MDB_NODUPDATA );
1058                         }
1059                         break;
1060                 }
1061                 if ( !(ic->flags & WAS_RANGE)) {
1062                         /* range, didn't exist before */
1063                         nid = 0;
1064                         data[0].mv_size = sizeof(ID);
1065                         data[0].mv_data = &nid;
1066                         rc = mdb_cursor_put( mc, &key, data, 0 );
1067                         if ( rc == 0 ) {
1068                                 data[0].mv_data = &ic->first;
1069                                 rc = mdb_cursor_put( mc, &key, data, 0 );
1070                                 if ( rc == 0 ) {
1071                                         data[0].mv_data = &ic->last;
1072                                         rc = mdb_cursor_put( mc, &key, data, 0 );
1073                                 }
1074                         }
1075                         if ( rc ) {
1076                                 rc = -1;
1077                         }
1078                 }
1079         } else {
1080                 /* Normal write */
1081                 int n;
1082
1083                 data[0].mv_size = sizeof(ID);
1084                 rc = 0;
1085                 i = ic->offset;
1086                 for ( ice = ic->head, n=0; ice; ice = ice->next, n++ ) {
1087                         int end;
1088                         if ( ice->next ) {
1089                                 end = IDBLOCK;
1090                         } else {
1091                                 end = ic->count & (IDBLOCK-1);
1092                                 if ( !end )
1093                                         end = IDBLOCK;
1094                         }
1095                         data[1].mv_size = end - i;
1096                         data[0].mv_data = &ice->ids[i];
1097                         i = 0;
1098                         rc = mdb_cursor_put( mc, &key, data, MDB_NODUPDATA|MDB_APPEND|MDB_MULTIPLE );
1099                         if ( rc ) {
1100                                 if ( rc == MDB_KEYEXIST ) {
1101                                         rc = 0;
1102                                         continue;
1103                                 }
1104                                 rc = -1;
1105                                 break;
1106                         }
1107                 }
1108                 if ( ic->head ) {
1109                         ic->tail->next = ai->ai_flist;
1110                         ai->ai_flist = ic->head;
1111                 }
1112         }
1113         ic->head = ai->ai_clist;
1114         ai->ai_clist = ic;
1115         return rc;
1116 }
1117
1118 static int
1119 mdb_tool_idl_flush_db( MDB_txn *txn, AttrInfo *ai )
1120 {
1121         MDB_cursor *mc;
1122         Avlnode *root;
1123         int rc;
1124
1125         mdb_cursor_open( txn, ai->ai_dbi, &mc );
1126         root = tavl_end( ai->ai_root, TAVL_DIR_LEFT );
1127         do {
1128                 rc = mdb_tool_idl_flush_one( mc, ai, root->avl_data );
1129                 if ( rc != -1 )
1130                         rc = 0;
1131         } while ((root = tavl_next(root, TAVL_DIR_RIGHT)));
1132         mdb_cursor_close( mc );
1133
1134         return rc;
1135 }
1136
1137 static int
1138 mdb_tool_idl_flush( BackendDB *be, MDB_txn *txn )
1139 {
1140         struct mdb_info *mdb = (struct mdb_info *) be->be_private;
1141         int rc = 0;
1142         unsigned int i, dbi;
1143
1144         for ( i=0; i < mdb->mi_nattrs; i++ ) {
1145                 if ( !mdb->mi_attrs[i]->ai_root ) continue;
1146                 rc = mdb_tool_idl_flush_db( txn, mdb->mi_attrs[i] );
1147                 tavl_free(mdb->mi_attrs[i]->ai_root, NULL);
1148                 mdb->mi_attrs[i]->ai_root = NULL;
1149                 if ( rc )
1150                         break;
1151         }
1152         return rc;
1153 }
1154
1155 int mdb_tool_idl_add(
1156         BackendDB *be,
1157         MDB_cursor *mc,
1158         struct berval *keys,
1159         ID id )
1160 {
1161         MDB_dbi dbi;
1162         mdb_tool_idl_cache *ic, itmp;
1163         mdb_tool_idl_cache_entry *ice;
1164         int i, rc, lcount;
1165         AttrInfo *ai = (AttrInfo *)mc;
1166         mc = ai->ai_cursor;
1167
1168         dbi = ai->ai_dbi;
1169         for (i=0; keys[i].bv_val; i++) {
1170         itmp.kstr = keys[i];
1171         ic = tavl_find( (Avlnode *)ai->ai_root, &itmp, mdb_tool_idl_cmp );
1172
1173         /* No entry yet, create one */
1174         if ( !ic ) {
1175                 MDB_val key, data;
1176                 ID nid;
1177                 int rc;
1178
1179                 if ( ai->ai_clist ) {
1180                         ic = ai->ai_clist;
1181                         ai->ai_clist = ic->head;
1182                 } else {
1183                         ic = ch_malloc( sizeof( mdb_tool_idl_cache ) + itmp.kstr.bv_len + 4 );
1184                 }
1185                 ic->kstr.bv_len = itmp.kstr.bv_len;
1186                 ic->kstr.bv_val = (char *)(ic+1);
1187                 memcpy( ic->kstr.bv_val, itmp.kstr.bv_val, ic->kstr.bv_len );
1188                 ic->head = ic->tail = NULL;
1189                 ic->last = 0;
1190                 ic->count = 0;
1191                 ic->offset = 0;
1192                 ic->flags = 0;
1193                 tavl_insert( (Avlnode **)&ai->ai_root, ic, mdb_tool_idl_cmp,
1194                         avl_dup_error );
1195
1196                 /* load existing key count here */
1197                 key.mv_size = keys[i].bv_len;
1198                 key.mv_data = keys[i].bv_val;
1199                 rc = mdb_cursor_get( mc, &key, &data, MDB_SET );
1200                 if ( rc == 0 ) {
1201                         ic->flags |= WAS_FOUND;
1202                         nid = *(ID *)data.mv_data;
1203                         if ( nid == 0 ) {
1204                                 ic->count = MDB_IDL_DB_SIZE+1;
1205                                 ic->flags |= WAS_RANGE;
1206                         } else {
1207                                 size_t count;
1208
1209                                 mdb_cursor_count( mc, &count );
1210                                 ic->count = count;
1211                                 ic->first = nid;
1212                                 ic->offset = count & (IDBLOCK-1);
1213                         }
1214                 }
1215         }
1216         /* are we a range already? */
1217         if ( ic->count > MDB_IDL_DB_SIZE ) {
1218                 ic->last = id;
1219                 continue;
1220         /* Are we at the limit, and converting to a range? */
1221         } else if ( ic->count == MDB_IDL_DB_SIZE ) {
1222                 if ( ic->head ) {
1223                         ic->tail->next = ai->ai_flist;
1224                         ai->ai_flist = ic->head;
1225                 }
1226                 ic->head = ic->tail = NULL;
1227                 ic->last = id;
1228                 ic->count++;
1229                 continue;
1230         }
1231         /* No free block, create that too */
1232         lcount = ic->count & (IDBLOCK-1);
1233         if ( !ic->tail || lcount == 0) {
1234                 if ( ai->ai_flist ) {
1235                         ice = ai->ai_flist;
1236                         ai->ai_flist = ice->next;
1237                 } else {
1238                         ice = ch_malloc( sizeof( mdb_tool_idl_cache_entry ));
1239                 }
1240                 ice->next = NULL;
1241                 if ( !ic->head ) {
1242                         ic->head = ice;
1243                 } else {
1244                         ic->tail->next = ice;
1245                 }
1246                 ic->tail = ice;
1247                 if ( lcount )
1248                         ice->ids[lcount-1] = 0;
1249                 if ( !ic->count )
1250                         ic->first = id;
1251         }
1252         ice = ic->tail;
1253         if (!lcount || ice->ids[lcount-1] != id)
1254                 ice->ids[lcount] = id;
1255         ic->count++;
1256         }
1257
1258         return 0;
1259 }
1260 #endif /* MDB_TOOL_IDL_CACHING */