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