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