]> git.sur5r.net Git - openldap/blob - servers/slapd/back-mdb/tools.c
a5408b5bb19fafa6f7f286addc322d5a46082b6b
[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 BackendDB *mdb_tool_ix_be;
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 AttrIxInfo **mdb_tool_axinfo;
80 static struct mdb_info *mdb_tool_info;
81 static ldap_pvt_thread_mutex_t mdb_tool_index_mutex;
82 static ldap_pvt_thread_cond_t mdb_tool_index_cond_main;
83 static ldap_pvt_thread_cond_t mdb_tool_index_cond_work;
84 static void * mdb_tool_index_task( void *ctx, void *ptr );
85
86 static int      mdb_writes, mdb_writes_per_commit;
87
88 /* Number of ops per commit in Quick mode.
89  * Batching speeds writes overall, but too large a
90  * batch will fail with MDB_TXN_FULL.
91  */
92 #ifndef MDB_WRITES_PER_COMMIT
93 #define MDB_WRITES_PER_COMMIT   500
94 #endif
95
96 static int
97 mdb_tool_entry_get_int( BackendDB *be, ID id, Entry **ep );
98
99 int mdb_tool_entry_open(
100         BackendDB *be, int mode )
101 {
102         /* In Quick mode, commit once per 500 entries */
103         mdb_writes = 0;
104         if ( slapMode & SLAP_TOOL_QUICK )
105                 mdb_writes_per_commit = MDB_WRITES_PER_COMMIT;
106         else
107                 mdb_writes_per_commit = 1;
108
109 #ifdef MDB_TOOL_IDL_CACHING                     /* threaded indexing has no performance advantage */
110         /* Set up for threaded slapindex */
111         if (( slapMode & (SLAP_TOOL_QUICK|SLAP_TOOL_READONLY)) == SLAP_TOOL_QUICK ) {
112                 if ( !mdb_tool_info ) {
113                         struct mdb_info *mdb = (struct mdb_info *) be->be_private;
114                         ldap_pvt_thread_mutex_init( &mdb_tool_index_mutex );
115                         ldap_pvt_thread_cond_init( &mdb_tool_index_cond_main );
116                         ldap_pvt_thread_cond_init( &mdb_tool_index_cond_work );
117                         if ( mdb->mi_nattrs ) {
118                                 int i;
119                                 mdb_tool_threads = slap_tool_thread_max - 1;
120                                 if ( mdb_tool_threads > 1 ) {
121                                         mdb_tool_index_rec = ch_calloc( mdb->mi_nattrs, sizeof( IndexRec ));
122                                         mdb_tool_axinfo = ch_calloc( mdb_tool_threads, sizeof( AttrIxInfo* ) +
123                                                 sizeof( AttrIxInfo ));
124                                         mdb_tool_axinfo[0] = (AttrIxInfo *)(mdb_tool_axinfo + mdb_tool_threads);
125                                         for (i=1; i<mdb_tool_threads; i++)
126                                                 mdb_tool_axinfo[i] = mdb_tool_axinfo[i-1]+1;
127                                         mdb_tool_index_tcount = mdb_tool_threads - 1;
128                                         mdb_tool_ix_be = be;
129                                         for (i=1; i<mdb_tool_threads; i++) {
130                                                 int *ptr = ch_malloc( sizeof( int ));
131                                                 *ptr = i;
132                                                 ldap_pvt_thread_pool_submit( &connection_pool,
133                                                         mdb_tool_index_task, ptr );
134                                         }
135                                         mdb_tool_info = mdb;
136                                 }
137                         }
138                 }
139         }
140 #endif
141
142         return 0;
143 }
144
145 int mdb_tool_entry_close(
146         BackendDB *be )
147 {
148 #ifdef MDB_TOOL_IDL_CACHING
149         if ( mdb_tool_info ) {
150                 int i;
151                 slapd_shutdown = 1;
152                 ldap_pvt_thread_mutex_lock( &mdb_tool_index_mutex );
153
154                 /* There might still be some threads starting */
155                 while ( mdb_tool_index_tcount > 0 ) {
156                         ldap_pvt_thread_cond_wait( &mdb_tool_index_cond_main,
157                                         &mdb_tool_index_mutex );
158                 }
159
160                 mdb_tool_index_tcount = mdb_tool_threads - 1;
161                 ldap_pvt_thread_cond_broadcast( &mdb_tool_index_cond_work );
162
163                 /* Make sure all threads are stopped */
164                 while ( mdb_tool_index_tcount > 0 ) {
165                         ldap_pvt_thread_cond_wait( &mdb_tool_index_cond_main,
166                                 &mdb_tool_index_mutex );
167                 }
168                 ldap_pvt_thread_mutex_unlock( &mdb_tool_index_mutex );
169
170                 mdb_tool_info = NULL;
171                 slapd_shutdown = 0;
172                 ch_free( mdb_tool_index_rec );
173                 mdb_tool_index_tcount = mdb_tool_threads - 1;
174                 if (txn)
175                         MDB_TOOL_IDL_FLUSH( be, txn );
176                 for (i=0; i<mdb_tool_threads; i++) {
177                         mdb_tool_idl_cache *ic;
178                         mdb_tool_idl_cache_entry *ice;
179                         while ((ic = mdb_tool_axinfo[i]->ai_clist)) {
180                                 mdb_tool_axinfo[i]->ai_clist = ic->head;
181                                 free(ic);
182                         }
183                         while ((ice = mdb_tool_axinfo[i]->ai_flist)) {
184                                 mdb_tool_axinfo[i]->ai_flist = ice->next;
185                                 free(ice);
186                         }
187                 }
188         }
189 #endif
190
191         if( idcursor ) {
192                 mdb_cursor_close( idcursor );
193                 idcursor = NULL;
194         }
195         if( cursor ) {
196                 mdb_cursor_close( cursor );
197                 cursor = NULL;
198         }
199         if( txn ) {
200                 int rc;
201                 if (( rc = mdb_txn_commit( txn ))) {
202                         Debug( LDAP_DEBUG_ANY,
203                                 LDAP_XSTRING(mdb_tool_entry_close) ": database %s: "
204                                 "txn_commit failed: %s (%d)\n",
205                                 be->be_suffix[0].bv_val, mdb_strerror(rc), rc );
206                         return -1;
207                 }
208                 txn = NULL;
209         }
210
211         if( nholes ) {
212                 unsigned i;
213                 fprintf( stderr, "Error, entries missing!\n");
214                 for (i=0; i<nholes; i++) {
215                         fprintf(stderr, "  entry %ld: %s\n",
216                                 holes[i].id, holes[i].dn.bv_val);
217                 }
218                 nholes = 0;
219                 return -1;
220         }
221
222         return 0;
223 }
224
225 ID
226 mdb_tool_entry_first_x(
227         BackendDB *be,
228         struct berval *base,
229         int scope,
230         Filter *f )
231 {
232         tool_base = base;
233         tool_scope = scope;
234         tool_filter = f;
235
236         return mdb_tool_entry_next( be );
237 }
238
239 ID mdb_tool_entry_next(
240         BackendDB *be )
241 {
242         int rc;
243         ID id;
244         struct mdb_info *mdb;
245
246         assert( be != NULL );
247         assert( slapMode & SLAP_TOOL_MODE );
248
249         mdb = (struct mdb_info *) be->be_private;
250         assert( mdb != NULL );
251
252         if ( !txn ) {
253                 rc = mdb_txn_begin( mdb->mi_dbenv, NULL, MDB_RDONLY, &txn );
254                 if ( rc )
255                         return NOID;
256                 rc = mdb_cursor_open( txn, mdb->mi_id2entry, &cursor );
257                 if ( rc ) {
258                         mdb_txn_abort( txn );
259                         return NOID;
260                 }
261         }
262
263 next:;
264         rc = mdb_cursor_get( cursor, &key, &data, MDB_NEXT );
265
266         if( rc ) {
267                 return NOID;
268         }
269
270         previd = *(ID *)key.mv_data;
271         id = previd;
272
273         if ( !data.mv_size )
274                 goto next;
275
276         if ( tool_filter || tool_base ) {
277                 static Operation op = {0};
278                 static Opheader ohdr = {0};
279
280                 op.o_hdr = &ohdr;
281                 op.o_bd = be;
282                 op.o_tmpmemctx = NULL;
283                 op.o_tmpmfuncs = &ch_mfuncs;
284
285                 if ( tool_next_entry ) {
286                         mdb_entry_release( &op, tool_next_entry, 0 );
287                         tool_next_entry = NULL;
288                 }
289
290                 rc = mdb_tool_entry_get_int( be, id, &tool_next_entry );
291                 if ( rc == LDAP_NO_SUCH_OBJECT ) {
292                         goto next;
293                 }
294
295                 assert( tool_next_entry != NULL );
296
297                 if ( tool_filter && test_filter( NULL, tool_next_entry, tool_filter ) != LDAP_COMPARE_TRUE )
298                 {
299                         mdb_entry_release( &op, tool_next_entry, 0 );
300                         tool_next_entry = NULL;
301                         goto next;
302                 }
303         }
304
305         return id;
306 }
307
308 ID mdb_tool_dn2id_get(
309         Backend *be,
310         struct berval *dn
311 )
312 {
313         struct mdb_info *mdb;
314         Operation op = {0};
315         Opheader ohdr = {0};
316         ID id;
317         int rc;
318
319         if ( BER_BVISEMPTY(dn) )
320                 return 0;
321
322         mdb = (struct mdb_info *) be->be_private;
323
324         if ( !txn ) {
325                 rc = mdb_txn_begin( mdb->mi_dbenv, NULL, (slapMode & SLAP_TOOL_READONLY) != 0 ?
326                         MDB_RDONLY : 0, &txn );
327                 if ( rc )
328                         return NOID;
329         }
330
331         op.o_hdr = &ohdr;
332         op.o_bd = be;
333         op.o_tmpmemctx = NULL;
334         op.o_tmpmfuncs = &ch_mfuncs;
335
336         rc = mdb_dn2id( &op, txn, NULL, dn, &id, NULL, NULL, NULL );
337         if ( rc == MDB_NOTFOUND )
338                 return NOID;
339
340         return id;
341 }
342
343 static int
344 mdb_tool_entry_get_int( BackendDB *be, ID id, Entry **ep )
345 {
346         Operation op = {0};
347         Opheader ohdr = {0};
348
349         Entry *e = NULL;
350         struct berval dn = BER_BVNULL, ndn = BER_BVNULL;
351         int rc;
352
353         assert( be != NULL );
354         assert( slapMode & SLAP_TOOL_MODE );
355
356         if ( ( tool_filter || tool_base ) && id == previd && tool_next_entry != NULL ) {
357                 *ep = tool_next_entry;
358                 tool_next_entry = NULL;
359                 return LDAP_SUCCESS;
360         }
361
362         if ( id != previd ) {
363                 key.mv_size = sizeof(ID);
364                 key.mv_data = &id;
365                 rc = mdb_cursor_get( cursor, &key, &data, MDB_SET );
366                 if ( rc ) {
367                         rc = LDAP_OTHER;
368                         goto done;
369                 }
370         }
371         if ( !data.mv_size ) {
372                 rc = LDAP_NO_SUCH_OBJECT;
373                 goto done;
374         }
375
376         op.o_hdr = &ohdr;
377         op.o_bd = be;
378         op.o_tmpmemctx = NULL;
379         op.o_tmpmfuncs = &ch_mfuncs;
380         if ( slapMode & SLAP_TOOL_READONLY ) {
381                 rc = mdb_id2name( &op, txn, &idcursor, id, &dn, &ndn );
382                 if ( rc  ) {
383                         rc = LDAP_OTHER;
384                         if ( e ) {
385                                 mdb_entry_return( &op, e );
386                                 e = NULL;
387                         }
388                         goto done;
389                 }
390                 if ( tool_base != NULL ) {
391                         if ( !dnIsSuffixScope( &ndn, tool_base, tool_scope ) ) {
392                                 ch_free( dn.bv_val );
393                                 ch_free( ndn.bv_val );
394                                 rc = LDAP_NO_SUCH_OBJECT;
395                                 goto done;
396                         }
397                 }
398         }
399         rc = mdb_entry_decode( &op, &data, &e );
400         e->e_id = id;
401         if ( !BER_BVISNULL( &dn )) {
402                 e->e_name = dn;
403                 e->e_nname = ndn;
404         } else {
405                 e->e_name.bv_val = NULL;
406                 e->e_nname.bv_val = NULL;
407         }
408
409 done:
410         if ( e != NULL ) {
411                 *ep = e;
412         }
413
414         return rc;
415 }
416
417 Entry*
418 mdb_tool_entry_get( BackendDB *be, ID id )
419 {
420         Entry *e = NULL;
421         int rc;
422
423         if ( !txn ) {
424                 struct mdb_info *mdb = (struct mdb_info *) be->be_private;
425                 rc = mdb_txn_begin( mdb->mi_dbenv, NULL,
426                         (slapMode & SLAP_TOOL_READONLY) ? MDB_RDONLY : 0, &txn );
427                 if ( rc )
428                         return NULL;
429         }
430         if ( !cursor ) {
431                 struct mdb_info *mdb = (struct mdb_info *) be->be_private;
432                 rc = mdb_cursor_open( txn, mdb->mi_id2entry, &cursor );
433                 if ( rc ) {
434                         mdb_txn_abort( txn );
435                         txn = NULL;
436                         return NULL;
437                 }
438         }
439         (void)mdb_tool_entry_get_int( be, id, &e );
440         return e;
441 }
442
443 static int mdb_tool_next_id(
444         Operation *op,
445         MDB_txn *tid,
446         Entry *e,
447         struct berval *text,
448         int hole )
449 {
450         struct mdb_info *mdb = (struct mdb_info *) op->o_bd->be_private;
451         struct berval dn = e->e_name;
452         struct berval ndn = e->e_nname;
453         struct berval pdn, npdn, nmatched;
454         ID id, pid = 0;
455         int rc;
456
457         if (ndn.bv_len == 0) {
458                 e->e_id = 0;
459                 return 0;
460         }
461
462         rc = mdb_dn2id( op, tid, mcp, &ndn, &id, NULL, NULL, &nmatched );
463         if ( rc == MDB_NOTFOUND ) {
464                 if ( !be_issuffix( op->o_bd, &ndn ) ) {
465                         ID eid = e->e_id;
466                         dnParent( &ndn, &npdn );
467                         if ( nmatched.bv_len != npdn.bv_len ) {
468                                 dnParent( &dn, &pdn );
469                                 e->e_name = pdn;
470                                 e->e_nname = npdn;
471                                 rc = mdb_tool_next_id( op, tid, e, text, 1 );
472                                 e->e_name = dn;
473                                 e->e_nname = ndn;
474                                 if ( rc ) {
475                                         return rc;
476                                 }
477                                 /* If parent didn't exist, it was created just now
478                                  * and its ID is now in e->e_id. Make sure the current
479                                  * entry gets added under the new parent ID.
480                                  */
481                                 if ( eid != e->e_id ) {
482                                         pid = e->e_id;
483                                 }
484                         } else {
485                                 pid = id;
486                         }
487                 }
488                 rc = mdb_next_id( op->o_bd, idcursor, &e->e_id );
489                 if ( rc ) {
490                         snprintf( text->bv_val, text->bv_len,
491                                 "next_id failed: %s (%d)",
492                                 mdb_strerror(rc), rc );
493                 Debug( LDAP_DEBUG_ANY,
494                         "=> mdb_tool_next_id: %s\n", text->bv_val, 0, 0 );
495                         return rc;
496                 }
497                 rc = mdb_dn2id_add( op, mcp, mcd, pid, 1, 1, e );
498                 if ( rc ) {
499                         snprintf( text->bv_val, text->bv_len,
500                                 "dn2id_add failed: %s (%d)",
501                                 mdb_strerror(rc), rc );
502                         Debug( LDAP_DEBUG_ANY,
503                                 "=> mdb_tool_next_id: %s\n", text->bv_val, 0, 0 );
504                 } else if ( hole ) {
505                         MDB_val key, data;
506                         if ( nholes == nhmax - 1 ) {
507                                 if ( holes == hbuf ) {
508                                         holes = ch_malloc( nhmax * sizeof(dn_id) * 2 );
509                                         AC_MEMCPY( holes, hbuf, sizeof(hbuf) );
510                                 } else {
511                                         holes = ch_realloc( holes, nhmax * sizeof(dn_id) * 2 );
512                                 }
513                                 nhmax *= 2;
514                         }
515                         ber_dupbv( &holes[nholes].dn, &ndn );
516                         holes[nholes++].id = e->e_id;
517                         key.mv_size = sizeof(ID);
518                         key.mv_data = &e->e_id;
519                         data.mv_size = 0;
520                         data.mv_data = NULL;
521                         rc = mdb_cursor_put( idcursor, &key, &data, MDB_NOOVERWRITE );
522                         if ( rc == MDB_KEYEXIST )
523                                 rc = 0;
524                         if ( rc ) {
525                                 snprintf( text->bv_val, text->bv_len,
526                                         "dummy id2entry add failed: %s (%d)",
527                                         mdb_strerror(rc), rc );
528                                 Debug( LDAP_DEBUG_ANY,
529                                         "=> mdb_tool_next_id: %s\n", text->bv_val, 0, 0 );
530                         }
531                 }
532         } else if ( !hole ) {
533                 unsigned i, j;
534
535                 e->e_id = id;
536
537                 for ( i=0; i<nholes; i++) {
538                         if ( holes[i].id == e->e_id ) {
539                                 free(holes[i].dn.bv_val);
540                                 for (j=i;j<nholes;j++) holes[j] = holes[j+1];
541                                 holes[j].id = 0;
542                                 nholes--;
543                                 break;
544                         } else if ( holes[i].id > e->e_id ) {
545                                 break;
546                         }
547                 }
548         }
549         return rc;
550 }
551
552 static int
553 mdb_tool_index_add(
554         Operation *op,
555         MDB_txn *txn,
556         Entry *e )
557 {
558         struct mdb_info *mdb = (struct mdb_info *) op->o_bd->be_private;
559
560         if ( !mdb->mi_nattrs )
561                 return 0;
562
563         if ( mdb_tool_threads > 1 ) {
564                 IndexRec *ir;
565                 int i, rc;
566                 Attribute *a;
567
568                 ir = mdb_tool_index_rec;
569                 for (i=0; i<mdb->mi_nattrs; i++)
570                         ir[i].ir_attrs = NULL;
571
572                 for ( a = e->e_attrs; a != NULL; a = a->a_next ) {
573                         rc = mdb_index_recset( mdb, a, a->a_desc->ad_type,
574                                 &a->a_desc->ad_tags, ir );
575                         if ( rc )
576                                 return rc;
577                 }
578                 for (i=0; i<mdb->mi_nattrs; i++) {
579                         if ( !ir[i].ir_ai )
580                                 break;
581                         rc = mdb_cursor_open( txn, ir[i].ir_ai->ai_dbi,
582                                  &ir[i].ir_ai->ai_cursor );
583                         if ( rc )
584                                 return rc;
585                 }
586                 mdb_tool_ix_id = e->e_id;
587                 mdb_tool_ix_txn = txn;
588                 ldap_pvt_thread_mutex_lock( &mdb_tool_index_mutex );
589                 /* Wait for all threads to be ready */
590                 while ( mdb_tool_index_tcount ) {
591                         ldap_pvt_thread_cond_wait( &mdb_tool_index_cond_main,
592                                 &mdb_tool_index_mutex );
593                 }
594
595                 for ( i=1; i<mdb_tool_threads; i++ )
596                         mdb_tool_index_rec[i].ir_i = LDAP_BUSY;
597                 mdb_tool_index_tcount = mdb_tool_threads - 1;
598                 ldap_pvt_thread_cond_broadcast( &mdb_tool_index_cond_work );
599                 ldap_pvt_thread_mutex_unlock( &mdb_tool_index_mutex );
600
601                 return mdb_index_recrun( op, txn, mdb, ir, e->e_id, 0 );
602         } else
603         {
604                 return mdb_index_entry_add( op, txn, e );
605         }
606 }
607
608 static int
609 mdb_tool_index_finish()
610 {
611         int i, rc;
612         ldap_pvt_thread_mutex_lock( &mdb_tool_index_mutex );
613         for ( i=1; i<mdb_tool_threads; i++ ) {
614                 if ( mdb_tool_index_rec[i].ir_i == LDAP_BUSY ) {
615                         ldap_pvt_thread_cond_wait( &mdb_tool_index_cond_main,
616                                 &mdb_tool_index_mutex );
617                         i--;
618                         continue;
619                 }
620                 if ( mdb_tool_index_rec[i].ir_i ) {
621                         rc = mdb_tool_index_rec[i].ir_i;
622                         break;
623                 }
624         }
625         ldap_pvt_thread_mutex_unlock( &mdb_tool_index_mutex );
626         return rc;
627 }
628
629 ID mdb_tool_entry_put(
630         BackendDB *be,
631         Entry *e,
632         struct berval *text )
633 {
634         int rc;
635         struct mdb_info *mdb;
636         Operation op = {0};
637         Opheader ohdr = {0};
638
639         assert( be != NULL );
640         assert( slapMode & SLAP_TOOL_MODE );
641
642         assert( text != NULL );
643         assert( text->bv_val != NULL );
644         assert( text->bv_val[0] == '\0' );      /* overconservative? */
645
646         Debug( LDAP_DEBUG_TRACE, "=> " LDAP_XSTRING(mdb_tool_entry_put)
647                 "( %ld, \"%s\" )\n", (long) e->e_id, e->e_dn, 0 );
648
649         mdb = (struct mdb_info *) be->be_private;
650
651         if ( !txn ) {
652                 rc = mdb_txn_begin( mdb->mi_dbenv, NULL, 0, &txn );
653                 if( rc != 0 ) {
654                         snprintf( text->bv_val, text->bv_len,
655                                 "txn_begin 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                 rc = mdb_cursor_open( txn, mdb->mi_id2entry, &idcursor );
663                 if( rc != 0 ) {
664                         snprintf( text->bv_val, text->bv_len,
665                                 "cursor_open failed: %s (%d)",
666                                 mdb_strerror(rc), rc );
667                         Debug( LDAP_DEBUG_ANY,
668                                 "=> " LDAP_XSTRING(mdb_tool_entry_put) ": %s\n",
669                                  text->bv_val, 0, 0 );
670                         return NOID;
671                 }
672                 if ( !mdb->mi_nextid ) {
673                         ID dummy;
674                         mdb_next_id( be, idcursor, &dummy );
675                 }
676                 rc = mdb_cursor_open( txn, mdb->mi_dn2id, &mcp );
677                 if( rc != 0 ) {
678                         snprintf( text->bv_val, text->bv_len,
679                                 "cursor_open failed: %s (%d)",
680                                 mdb_strerror(rc), rc );
681                         Debug( LDAP_DEBUG_ANY,
682                                 "=> " LDAP_XSTRING(mdb_tool_entry_put) ": %s\n",
683                                  text->bv_val, 0, 0 );
684                         return NOID;
685                 }
686                 rc = mdb_cursor_open( txn, mdb->mi_dn2id, &mcd );
687                 if( rc != 0 ) {
688                         snprintf( text->bv_val, text->bv_len,
689                                 "cursor_open failed: %s (%d)",
690                                 mdb_strerror(rc), rc );
691                         Debug( LDAP_DEBUG_ANY,
692                                 "=> " LDAP_XSTRING(mdb_tool_entry_put) ": %s\n",
693                                  text->bv_val, 0, 0 );
694                         return NOID;
695                 }
696         }
697
698         op.o_hdr = &ohdr;
699         op.o_bd = be;
700         op.o_tmpmemctx = NULL;
701         op.o_tmpmfuncs = &ch_mfuncs;
702
703         /* add dn2id indices */
704         rc = mdb_tool_next_id( &op, txn, e, text, 0 );
705         if( rc != 0 ) {
706                 goto done;
707         }
708
709         LDAP_SLIST_INSERT_HEAD( &op.o_extra, &mdb_tool_axinfo[0]->ai_oe, oe_next );
710         rc = mdb_tool_index_add( &op, txn, e );
711         if( rc != 0 ) {
712                 snprintf( text->bv_val, text->bv_len,
713                                 "index_entry_add failed: err=%d", rc );
714                 Debug( LDAP_DEBUG_ANY,
715                         "=> " LDAP_XSTRING(mdb_tool_entry_put) ": %s\n",
716                         text->bv_val, 0, 0 );
717                 goto done;
718         }
719
720
721         /* id2entry index */
722         rc = mdb_id2entry_add( &op, txn, idcursor, e );
723         if( rc != 0 ) {
724                 snprintf( text->bv_val, text->bv_len,
725                                 "id2entry_add failed: err=%d", rc );
726                 Debug( LDAP_DEBUG_ANY,
727                         "=> " LDAP_XSTRING(mdb_tool_entry_put) ": %s\n",
728                         text->bv_val, 0, 0 );
729                 goto done;
730         }
731
732         if( mdb->mi_nattrs && mdb_tool_threads > 1 )
733                 rc = mdb_tool_index_finish();
734
735 done:
736         if( rc == 0 ) {
737                 mdb_writes++;
738                 if ( mdb_writes >= mdb_writes_per_commit ) {
739                         unsigned i;
740                         MDB_TOOL_IDL_FLUSH( be, txn );
741                         rc = mdb_txn_commit( txn );
742                         for ( i=0; i<mdb->mi_nattrs; i++ )
743                                 mdb->mi_attrs[i]->ai_cursor = NULL;
744                         mdb_writes = 0;
745                         txn = NULL;
746                         idcursor = NULL;
747                         if( rc != 0 ) {
748                                 snprintf( text->bv_val, text->bv_len,
749                                                 "txn_commit failed: %s (%d)",
750                                                 mdb_strerror(rc), rc );
751                                 Debug( LDAP_DEBUG_ANY,
752                                         "=> " LDAP_XSTRING(mdb_tool_entry_put) ": %s\n",
753                                         text->bv_val, 0, 0 );
754                                 e->e_id = NOID;
755                         }
756                 }
757
758         } else {
759                 unsigned i;
760                 mdb_txn_abort( txn );
761                 txn = NULL;
762                 idcursor = NULL;
763                 for ( i=0; i<mdb->mi_nattrs; i++ )
764                         mdb->mi_attrs[i]->ai_cursor = NULL;
765                 mdb_writes = 0;
766                 snprintf( text->bv_val, text->bv_len,
767                         "txn_aborted! %s (%d)",
768                         rc == LDAP_OTHER ? "Internal error" :
769                         mdb_strerror(rc), rc );
770                 Debug( LDAP_DEBUG_ANY,
771                         "=> " LDAP_XSTRING(mdb_tool_entry_put) ": %s\n",
772                         text->bv_val, 0, 0 );
773                 e->e_id = NOID;
774         }
775
776         return e->e_id;
777 }
778
779 static int mdb_dn2id_upgrade( BackendDB *be );
780
781 int mdb_tool_entry_reindex(
782         BackendDB *be,
783         ID id,
784         AttributeDescription **adv )
785 {
786         struct mdb_info *mi = (struct mdb_info *) be->be_private;
787         int rc;
788         Entry *e;
789         Operation op = {0};
790         Opheader ohdr = {0};
791
792         Debug( LDAP_DEBUG_ARGS,
793                 "=> " LDAP_XSTRING(mdb_tool_entry_reindex) "( %ld )\n",
794                 (long) id, 0, 0 );
795         assert( tool_base == NULL );
796         assert( tool_filter == NULL );
797
798         /* Special: do a dn2id upgrade */
799         if ( adv && adv[0] == slap_schema.si_ad_entryDN ) {
800                 /* short-circuit tool_entry_next() */
801                 mdb_cursor_get( cursor, &key, &data, MDB_LAST );
802                 return mdb_dn2id_upgrade( be );
803         }
804
805         /* No indexes configured, nothing to do. Could return an
806          * error here to shortcut things.
807          */
808         if (!mi->mi_attrs) {
809                 return 0;
810         }
811
812         /* Check for explicit list of attrs to index */
813         if ( adv ) {
814                 int i, j, n;
815
816                 if ( mi->mi_attrs[0]->ai_desc != adv[0] ) {
817                         /* count */
818                         for ( n = 0; adv[n]; n++ ) ;
819
820                         /* insertion sort */
821                         for ( i = 0; i < n; i++ ) {
822                                 AttributeDescription *ad = adv[i];
823                                 for ( j = i-1; j>=0; j--) {
824                                         if ( SLAP_PTRCMP( adv[j], ad ) <= 0 ) break;
825                                         adv[j+1] = adv[j];
826                                 }
827                                 adv[j+1] = ad;
828                         }
829                 }
830
831                 for ( i = 0; adv[i]; i++ ) {
832                         if ( mi->mi_attrs[i]->ai_desc != adv[i] ) {
833                                 for ( j = i+1; j < mi->mi_nattrs; j++ ) {
834                                         if ( mi->mi_attrs[j]->ai_desc == adv[i] ) {
835                                                 AttrInfo *ai = mi->mi_attrs[i];
836                                                 mi->mi_attrs[i] = mi->mi_attrs[j];
837                                                 mi->mi_attrs[j] = ai;
838                                                 break;
839                                         }
840                                 }
841                                 if ( j == mi->mi_nattrs ) {
842                                         Debug( LDAP_DEBUG_ANY,
843                                                 LDAP_XSTRING(mdb_tool_entry_reindex)
844                                                 ": no index configured for %s\n",
845                                                 adv[i]->ad_cname.bv_val, 0, 0 );
846                                         return -1;
847                                 }
848                         }
849                 }
850                 mi->mi_nattrs = i;
851         }
852
853         e = mdb_tool_entry_get( be, id );
854
855         if( e == NULL ) {
856                 Debug( LDAP_DEBUG_ANY,
857                         LDAP_XSTRING(mdb_tool_entry_reindex)
858                         ": could not locate id=%ld\n",
859                         (long) id, 0, 0 );
860                 return -1;
861         }
862
863         if ( !txi ) {
864                 rc = mdb_txn_begin( mi->mi_dbenv, NULL, 0, &txi );
865                 if( rc != 0 ) {
866                         Debug( LDAP_DEBUG_ANY,
867                                 "=> " LDAP_XSTRING(mdb_tool_entry_reindex) ": "
868                                 "txn_begin failed: %s (%d)\n",
869                                 mdb_strerror(rc), rc, 0 );
870                         goto done;
871                 }
872         }
873
874         if ( slapMode & SLAP_TRUNCATE_MODE ) {
875                 int i;
876                 for ( i=0; i < mi->mi_nattrs; i++ ) {
877                         rc = mdb_drop( txi, mi->mi_attrs[i]->ai_dbi, 0 );
878                         if ( rc ) {
879                                 Debug( LDAP_DEBUG_ANY,
880                                         LDAP_XSTRING(mdb_tool_entry_reindex)
881                                         ": (Truncate) mdb_drop(%s) failed: %s (%d)\n",
882                                         mi->mi_attrs[i]->ai_desc->ad_type->sat_cname.bv_val,
883                                         mdb_strerror(rc), rc );
884                                 return -1;
885                         }
886                 }
887                 slapMode ^= SLAP_TRUNCATE_MODE;
888         }
889
890         /*
891          * just (re)add them for now
892          * Use truncate mode to empty/reset index databases
893          */
894
895         Debug( LDAP_DEBUG_TRACE,
896                 "=> " LDAP_XSTRING(mdb_tool_entry_reindex) "( %ld )\n",
897                 (long) id, 0, 0 );
898
899         op.o_hdr = &ohdr;
900         op.o_bd = be;
901         op.o_tmpmemctx = NULL;
902         op.o_tmpmfuncs = &ch_mfuncs;
903
904         rc = mdb_tool_index_add( &op, txi, e );
905
906 done:
907         if( rc == 0 ) {
908                 mdb_writes++;
909                 if ( mdb_writes >= mdb_writes_per_commit ) {
910                         MDB_val key;
911                         unsigned i;
912                         MDB_TOOL_IDL_FLUSH( be, txi );
913                         rc = mdb_txn_commit( txi );
914                         mdb_writes = 0;
915                         for ( i=0; i<mi->mi_nattrs; i++ )
916                                 mi->mi_attrs[i]->ai_cursor = NULL;
917                         if( rc != 0 ) {
918                                 Debug( LDAP_DEBUG_ANY,
919                                         "=> " LDAP_XSTRING(mdb_tool_entry_reindex)
920                                         ": txn_commit failed: %s (%d)\n",
921                                         mdb_strerror(rc), rc, 0 );
922                                 e->e_id = NOID;
923                         }
924                         mdb_cursor_close( cursor );
925                         txi = NULL;
926                         /* Must close the read txn to allow old pages to be reclaimed. */
927                         mdb_txn_abort( txn );
928                         /* and then reopen it so that tool_entry_next still works. */
929                         mdb_txn_begin( mi->mi_dbenv, NULL, MDB_RDONLY, &txn );
930                         mdb_cursor_open( txn, mi->mi_id2entry, &cursor );
931                         key.mv_data = &id;
932                         key.mv_size = sizeof(ID);
933                         mdb_cursor_get( cursor, &key, NULL, MDB_SET );
934                 }
935
936         } else {
937                 unsigned i;
938                 mdb_writes = 0;
939                 mdb_cursor_close( cursor );
940                 cursor = NULL;
941                 mdb_txn_abort( txi );
942                 for ( i=0; i<mi->mi_nattrs; i++ )
943                         mi->mi_attrs[i]->ai_cursor = NULL;
944                 Debug( LDAP_DEBUG_ANY,
945                         "=> " LDAP_XSTRING(mdb_tool_entry_reindex)
946                         ": txn_aborted! err=%d\n",
947                         rc, 0, 0 );
948                 e->e_id = NOID;
949                 txi = NULL;
950         }
951         mdb_entry_release( &op, e, 0 );
952
953         return rc;
954 }
955
956 ID mdb_tool_entry_modify(
957         BackendDB *be,
958         Entry *e,
959         struct berval *text )
960 {
961         int rc;
962         struct mdb_info *mdb;
963         Operation op = {0};
964         Opheader ohdr = {0};
965
966         assert( be != NULL );
967         assert( slapMode & SLAP_TOOL_MODE );
968
969         assert( text != NULL );
970         assert( text->bv_val != NULL );
971         assert( text->bv_val[0] == '\0' );      /* overconservative? */
972
973         assert ( e->e_id != NOID );
974
975         Debug( LDAP_DEBUG_TRACE,
976                 "=> " LDAP_XSTRING(mdb_tool_entry_modify) "( %ld, \"%s\" )\n",
977                 (long) e->e_id, e->e_dn, 0 );
978
979         mdb = (struct mdb_info *) be->be_private;
980
981         if( cursor ) {
982                 mdb_cursor_close( cursor );
983                 cursor = NULL;
984         }
985         if ( !txn ) {
986                 rc = mdb_txn_begin( mdb->mi_dbenv, NULL, 0, &txn );
987                 if( rc != 0 ) {
988                         snprintf( text->bv_val, text->bv_len,
989                                 "txn_begin failed: %s (%d)",
990                                 mdb_strerror(rc), rc );
991                         Debug( LDAP_DEBUG_ANY,
992                                 "=> " LDAP_XSTRING(mdb_tool_entry_modify) ": %s\n",
993                                  text->bv_val, 0, 0 );
994                         return NOID;
995                 }
996         }
997
998         op.o_hdr = &ohdr;
999         op.o_bd = be;
1000         op.o_tmpmemctx = NULL;
1001         op.o_tmpmfuncs = &ch_mfuncs;
1002
1003         /* id2entry index */
1004         rc = mdb_id2entry_update( &op, txn, NULL, e );
1005         if( rc != 0 ) {
1006                 snprintf( text->bv_val, text->bv_len,
1007                                 "id2entry_update failed: err=%d", rc );
1008                 Debug( LDAP_DEBUG_ANY,
1009                         "=> " LDAP_XSTRING(mdb_tool_entry_modify) ": %s\n",
1010                         text->bv_val, 0, 0 );
1011                 goto done;
1012         }
1013
1014 done:
1015         if( rc == 0 ) {
1016                 rc = mdb_txn_commit( txn );
1017                 if( rc != 0 ) {
1018                         snprintf( text->bv_val, text->bv_len,
1019                                         "txn_commit failed: %s (%d)",
1020                                         mdb_strerror(rc), rc );
1021                         Debug( LDAP_DEBUG_ANY,
1022                                 "=> " LDAP_XSTRING(mdb_tool_entry_modify) ": "
1023                                 "%s\n", text->bv_val, 0, 0 );
1024                         e->e_id = NOID;
1025                 }
1026
1027         } else {
1028                 mdb_txn_abort( txn );
1029                 snprintf( text->bv_val, text->bv_len,
1030                         "txn_aborted! %s (%d)",
1031                         mdb_strerror(rc), rc );
1032                 Debug( LDAP_DEBUG_ANY,
1033                         "=> " LDAP_XSTRING(mdb_tool_entry_modify) ": %s\n",
1034                         text->bv_val, 0, 0 );
1035                 e->e_id = NOID;
1036         }
1037         txn = NULL;
1038         idcursor = NULL;
1039
1040         return e->e_id;
1041 }
1042
1043 static void *
1044 mdb_tool_index_task( void *ctx, void *ptr )
1045 {
1046         int base = *(int *)ptr;
1047         Operation op = {0};
1048         Opheader ohdr = {0};
1049         AttrIxInfo ai = {0}, *aio;
1050
1051         free( ptr );
1052         op.o_hdr = &ohdr;
1053         op.o_bd = mdb_tool_ix_be;
1054         op.o_tmpmemctx = NULL;
1055         op.o_tmpmfuncs = &ch_mfuncs;
1056         aio = mdb_tool_axinfo[base];
1057         mdb_tool_axinfo[base] = &ai;
1058         LDAP_SLIST_INSERT_HEAD( &op.o_extra, &ai.ai_oe, oe_next );
1059         while ( 1 ) {
1060                 ldap_pvt_thread_mutex_lock( &mdb_tool_index_mutex );
1061                 mdb_tool_index_tcount--;
1062                 if ( !mdb_tool_index_tcount )
1063                         ldap_pvt_thread_cond_signal( &mdb_tool_index_cond_main );
1064                 ldap_pvt_thread_cond_wait( &mdb_tool_index_cond_work,
1065                         &mdb_tool_index_mutex );
1066                 if ( slapd_shutdown ) {
1067                         mdb_tool_index_tcount--;
1068                         if ( !mdb_tool_index_tcount )
1069                                 ldap_pvt_thread_cond_signal( &mdb_tool_index_cond_main );
1070                         *aio = ai;
1071                         mdb_tool_axinfo[base] = aio;
1072                         ldap_pvt_thread_mutex_unlock( &mdb_tool_index_mutex );
1073                         break;
1074                 }
1075                 ldap_pvt_thread_mutex_unlock( &mdb_tool_index_mutex );
1076                 mdb_tool_index_rec[base].ir_i = mdb_index_recrun( &op,
1077                         mdb_tool_ix_txn,
1078                         mdb_tool_info, mdb_tool_index_rec, mdb_tool_ix_id, base );
1079         }
1080
1081         return NULL;
1082 }
1083
1084 #ifdef MDB_TOOL_IDL_CACHING
1085 static int
1086 mdb_tool_idl_cmp( const void *v1, const void *v2 )
1087 {
1088         const mdb_tool_idl_cache *c1 = v1, *c2 = v2;
1089         int rc;
1090
1091         if (( rc = c1->kstr.bv_len - c2->kstr.bv_len )) return rc;
1092         return memcmp( c1->kstr.bv_val, c2->kstr.bv_val, c1->kstr.bv_len );
1093 }
1094
1095 static int
1096 mdb_tool_idl_flush_one( MDB_cursor *mc, AttrIxInfo *ai, mdb_tool_idl_cache *ic )
1097 {
1098         mdb_tool_idl_cache_entry *ice;
1099         MDB_val key, data[2];
1100         int i, rc;
1101         ID id, nid;
1102
1103         /* Freshly allocated, ignore it */
1104         if ( !ic->head && ic->count <= MDB_IDL_DB_SIZE ) {
1105                 return 0;
1106         }
1107
1108         key.mv_data = ic->kstr.bv_val;
1109         key.mv_size = ic->kstr.bv_len;
1110
1111         if ( ic->count > MDB_IDL_DB_SIZE ) {
1112                 while ( ic->flags & WAS_FOUND ) {
1113                         rc = mdb_cursor_get( mc, &key, data, MDB_SET );
1114                         if ( rc ) {
1115                                 /* FIXME: find out why this happens */
1116                                 ic->flags = 0;
1117                                 break;
1118                         }
1119                         if ( ic->flags & WAS_RANGE ) {
1120                                 /* Skip lo */
1121                                 rc = mdb_cursor_get( mc, &key, data, MDB_NEXT_DUP );
1122
1123                                 /* Get hi */
1124                                 rc = mdb_cursor_get( mc, &key, data, MDB_NEXT_DUP );
1125
1126                                 /* Store range hi */
1127                                 data[0].mv_data = &ic->last;
1128                                 rc = mdb_cursor_put( mc, &key, data, MDB_CURRENT );
1129                         } else {
1130                                 /* Delete old data, replace with range */
1131                                 ic->first = *(ID *)data[0].mv_data;
1132                                 mdb_cursor_del( mc, MDB_NODUPDATA );
1133                         }
1134                         break;
1135                 }
1136                 if ( !(ic->flags & WAS_RANGE)) {
1137                         /* range, didn't exist before */
1138                         nid = 0;
1139                         data[0].mv_size = sizeof(ID);
1140                         data[0].mv_data = &nid;
1141                         rc = mdb_cursor_put( mc, &key, data, 0 );
1142                         if ( rc == 0 ) {
1143                                 data[0].mv_data = &ic->first;
1144                                 rc = mdb_cursor_put( mc, &key, data, 0 );
1145                                 if ( rc == 0 ) {
1146                                         data[0].mv_data = &ic->last;
1147                                         rc = mdb_cursor_put( mc, &key, data, 0 );
1148                                 }
1149                         }
1150                         if ( rc ) {
1151                                 rc = -1;
1152                         }
1153                 }
1154         } else {
1155                 /* Normal write */
1156                 int n;
1157
1158                 data[0].mv_size = sizeof(ID);
1159                 rc = 0;
1160                 for ( ice = ic->head, n=0; ice; ice = ice->next, n++ ) {
1161                         int end;
1162                         if ( ice->next ) {
1163                                 end = IDBLOCK;
1164                         } else {
1165                                 end = (ic->count-ic->offset) & (IDBLOCK-1);
1166                                 if ( !end )
1167                                         end = IDBLOCK;
1168                         }
1169                         data[1].mv_size = end;
1170                         data[0].mv_data = ice->ids;
1171                         rc = mdb_cursor_put( mc, &key, data, MDB_APPENDDUP|MDB_MULTIPLE );
1172                         if ( rc ) {
1173                                 rc = -1;
1174                                 break;
1175                         }
1176                 }
1177                 if ( ic->head ) {
1178                         ic->tail->next = ai->ai_flist;
1179                         ai->ai_flist = ic->head;
1180                 }
1181         }
1182         ic->head = ai->ai_clist;
1183         ai->ai_clist = ic;
1184         return rc;
1185 }
1186
1187 static int
1188 mdb_tool_idl_flush_db( MDB_txn *txn, AttrInfo *ai, AttrIxInfo *ax )
1189 {
1190         MDB_cursor *mc;
1191         Avlnode *root;
1192         int rc;
1193
1194         mdb_cursor_open( txn, ai->ai_dbi, &mc );
1195         root = tavl_end( ai->ai_root, TAVL_DIR_LEFT );
1196         do {
1197                 rc = mdb_tool_idl_flush_one( mc, ax, root->avl_data );
1198                 if ( rc != -1 )
1199                         rc = 0;
1200         } while ((root = tavl_next(root, TAVL_DIR_RIGHT)));
1201         mdb_cursor_close( mc );
1202
1203         return rc;
1204 }
1205
1206 static int
1207 mdb_tool_idl_flush( BackendDB *be, MDB_txn *txn )
1208 {
1209         struct mdb_info *mdb = (struct mdb_info *) be->be_private;
1210         int rc = 0;
1211         unsigned int i, dbi;
1212
1213         for ( i=0; i < mdb->mi_nattrs; i++ ) {
1214                 if ( !mdb->mi_attrs[i]->ai_root ) continue;
1215                 rc = mdb_tool_idl_flush_db( txn, mdb->mi_attrs[i], mdb_tool_axinfo[i % mdb_tool_threads] );
1216                 tavl_free(mdb->mi_attrs[i]->ai_root, NULL);
1217                 mdb->mi_attrs[i]->ai_root = NULL;
1218                 if ( rc )
1219                         break;
1220         }
1221         return rc;
1222 }
1223
1224 int mdb_tool_idl_add(
1225         BackendDB *be,
1226         MDB_cursor *mc,
1227         struct berval *keys,
1228         ID id )
1229 {
1230         MDB_dbi dbi;
1231         mdb_tool_idl_cache *ic, itmp;
1232         mdb_tool_idl_cache_entry *ice;
1233         int i, rc, lcount;
1234         AttrIxInfo *ax = (AttrIxInfo *)mc;
1235         AttrInfo *ai = (AttrInfo *)ax->ai_ai;
1236         mc = ai->ai_cursor;
1237
1238         dbi = ai->ai_dbi;
1239         for (i=0; keys[i].bv_val; i++) {
1240         itmp.kstr = keys[i];
1241         ic = tavl_find( (Avlnode *)ai->ai_root, &itmp, mdb_tool_idl_cmp );
1242
1243         /* No entry yet, create one */
1244         if ( !ic ) {
1245                 MDB_val key, data;
1246                 ID nid;
1247                 int rc;
1248
1249                 if ( ax->ai_clist ) {
1250                         ic = ax->ai_clist;
1251                         ax->ai_clist = ic->head;
1252                 } else {
1253                         ic = ch_malloc( sizeof( mdb_tool_idl_cache ) + itmp.kstr.bv_len + 4 );
1254                 }
1255                 ic->kstr.bv_len = itmp.kstr.bv_len;
1256                 ic->kstr.bv_val = (char *)(ic+1);
1257                 memcpy( ic->kstr.bv_val, itmp.kstr.bv_val, ic->kstr.bv_len );
1258                 ic->head = ic->tail = NULL;
1259                 ic->last = 0;
1260                 ic->count = 0;
1261                 ic->offset = 0;
1262                 ic->flags = 0;
1263                 tavl_insert( (Avlnode **)&ai->ai_root, ic, mdb_tool_idl_cmp,
1264                         avl_dup_error );
1265
1266                 /* load existing key count here */
1267                 key.mv_size = keys[i].bv_len;
1268                 key.mv_data = keys[i].bv_val;
1269                 rc = mdb_cursor_get( mc, &key, &data, MDB_SET );
1270                 if ( rc == 0 ) {
1271                         ic->flags |= WAS_FOUND;
1272                         nid = *(ID *)data.mv_data;
1273                         if ( nid == 0 ) {
1274                                 ic->count = MDB_IDL_DB_SIZE+1;
1275                                 ic->flags |= WAS_RANGE;
1276                         } else {
1277                                 size_t count;
1278
1279                                 mdb_cursor_count( mc, &count );
1280                                 ic->count = count;
1281                                 ic->first = nid;
1282                                 ic->offset = count & (IDBLOCK-1);
1283                         }
1284                 }
1285         }
1286         /* are we a range already? */
1287         if ( ic->count > MDB_IDL_DB_SIZE ) {
1288                 ic->last = id;
1289                 continue;
1290         /* Are we at the limit, and converting to a range? */
1291         } else if ( ic->count == MDB_IDL_DB_SIZE ) {
1292                 if ( ic->head ) {
1293                         ic->tail->next = ax->ai_flist;
1294                         ax->ai_flist = ic->head;
1295                 }
1296                 ic->head = ic->tail = NULL;
1297                 ic->last = id;
1298                 ic->count++;
1299                 continue;
1300         }
1301         /* No free block, create that too */
1302         lcount = (ic->count-ic->offset) & (IDBLOCK-1);
1303         if ( !ic->tail || lcount == 0) {
1304                 if ( ax->ai_flist ) {
1305                         ice = ax->ai_flist;
1306                         ax->ai_flist = ice->next;
1307                 } else {
1308                         ice = ch_malloc( sizeof( mdb_tool_idl_cache_entry ));
1309                 }
1310                 ice->next = NULL;
1311                 if ( !ic->head ) {
1312                         ic->head = ice;
1313                 } else {
1314                         ic->tail->next = ice;
1315                 }
1316                 ic->tail = ice;
1317                 if ( lcount )
1318                         ice->ids[lcount-1] = 0;
1319                 if ( !ic->count )
1320                         ic->first = id;
1321         }
1322         ice = ic->tail;
1323         if (!lcount || ice->ids[lcount-1] != id) {
1324                 ice->ids[lcount] = id;
1325                 ic->count++;
1326         }
1327         }
1328
1329         return 0;
1330 }
1331 #endif /* MDB_TOOL_IDL_CACHING */
1332
1333 /* Upgrade from pre 2.4.34 dn2id format */
1334
1335 #include <ac/unistd.h>
1336 #include <lutil_meter.h>
1337
1338 #define STACKSIZ        2048
1339
1340 typedef struct rec {
1341         ID id;
1342         size_t len;
1343         char rdn[512];
1344 } rec;
1345
1346 static int
1347 mdb_dn2id_upgrade( BackendDB *be ) {
1348         struct mdb_info *mi = (struct mdb_info *) be->be_private;
1349         MDB_txn *mt;
1350         MDB_cursor *mc = NULL;
1351         MDB_val key, data;
1352         char *ptr;
1353         int rc, writes=0, depth=0;
1354         int enable_meter = 0;
1355         ID id = 0, *num, count = 0;
1356         rec *stack;
1357         lutil_meter_t meter;
1358
1359         if (!(mi->mi_flags & MDB_NEED_UPGRADE)) {
1360                 Debug( LDAP_DEBUG_ANY, "database %s: No upgrade needed.\n",
1361                         be->be_suffix[0].bv_val, 0, 0 );
1362                 return 0;
1363         }
1364
1365         {
1366                 MDB_stat st;
1367
1368                 mdb_stat(mdb_cursor_txn(cursor), mi->mi_dbis[MDB_ID2ENTRY], &st);
1369                 if (!st.ms_entries) {
1370                         /* Empty DB, nothing to upgrade? */
1371                         return 0;
1372                 }
1373                 if (isatty(2))
1374                         enable_meter = !lutil_meter_open(&meter,
1375                                 &lutil_meter_text_display,
1376                                 &lutil_meter_linear_estimator,
1377                                 st.ms_entries);
1378         }
1379
1380         num = ch_malloc(STACKSIZ * (sizeof(ID) + sizeof(rec)));
1381         stack = (rec *)(num + STACKSIZ);
1382
1383         rc = mdb_txn_begin(mi->mi_dbenv, NULL, 0, &mt);
1384         if (rc) {
1385                 Debug(LDAP_DEBUG_ANY, "mdb_dn2id_upgrade: mdb_txn_begin failed, %s (%d)\n",
1386                         mdb_strerror(rc), rc, 0 );
1387                 goto leave;
1388         }
1389         rc = mdb_cursor_open(mt, mi->mi_dbis[MDB_DN2ID], &mc);
1390         if (rc) {
1391                 Debug(LDAP_DEBUG_ANY, "mdb_dn2id_upgrade: mdb_cursor_open failed, %s (%d)\n",
1392                         mdb_strerror(rc), rc, 0 );
1393                 goto leave;
1394         }
1395
1396         key.mv_size = sizeof(ID);
1397         /* post-order depth-first update */
1398         for(;;) {
1399                 size_t dkids;
1400                 unsigned char *ptr;
1401
1402                 /* visit */
1403                 key.mv_data = &id;
1404                 stack[depth].id = id;
1405                 rc = mdb_cursor_get(mc, &key, &data, MDB_SET);
1406                 if (rc) {
1407                         Debug(LDAP_DEBUG_ANY, "mdb_dn2id_upgrade: mdb_cursor_get failed, %s (%d)\n",
1408                                 mdb_strerror(rc), rc, 0 );
1409                         goto leave;
1410                 }
1411                 num[depth] = 1;
1412
1413                 rc = mdb_cursor_count(mc, &dkids);
1414                 if (rc) {
1415                         Debug(LDAP_DEBUG_ANY, "mdb_dn2id_upgrade: mdb_cursor_count failed, %s (%d)\n",
1416                                 mdb_strerror(rc), rc, 0 );
1417                         goto leave;
1418                 }
1419                 if (dkids > 1) {
1420                         rc = mdb_cursor_get(mc, &key, &data, MDB_NEXT_DUP);
1421 down:
1422                         ptr = data.mv_data + data.mv_size - sizeof(ID);
1423                         memcpy(&id, ptr, sizeof(ID));
1424                         depth++;
1425                         memcpy(stack[depth].rdn, data.mv_data, data.mv_size);
1426                         stack[depth].len = data.mv_size;
1427                         continue;
1428                 }
1429
1430
1431                 /* pop: write updated count, advance to next node */
1432 pop:
1433                 /* update superior counts */
1434                 if (depth)
1435                         num[depth-1] += num[depth];
1436
1437                 key.mv_data = &id;
1438                 id = stack[depth-1].id;
1439                 data.mv_data = stack[depth].rdn;
1440                 data.mv_size = stack[depth].len;
1441                 rc = mdb_cursor_get(mc, &key, &data, MDB_GET_BOTH);
1442                 if (rc) {
1443                         Debug(LDAP_DEBUG_ANY, "mdb_dn2id_upgrade: mdb_cursor_get(BOTH) failed, %s (%d)\n",
1444                                 mdb_strerror(rc), rc, 0 );
1445                         goto leave;
1446                 }
1447                 data.mv_data = stack[depth].rdn;
1448                 ptr = data.mv_data + data.mv_size;
1449                 memcpy(ptr, &num[depth], sizeof(ID));
1450                 data.mv_size += sizeof(ID);
1451                 rc = mdb_cursor_del(mc, 0);
1452                 if (rc) {
1453                         Debug(LDAP_DEBUG_ANY, "mdb_dn2id_upgrade: mdb_cursor_del failed, %s (%d)\n",
1454                                 mdb_strerror(rc), rc, 0 );
1455                         goto leave;
1456                 }
1457                 rc = mdb_cursor_put(mc, &key, &data, 0);
1458                 if (rc) {
1459                         Debug(LDAP_DEBUG_ANY, "mdb_dn2id_upgrade: mdb_cursor_put failed, %s (%d)\n",
1460                                 mdb_strerror(rc), rc, 0 );
1461                         goto leave;
1462                 }
1463                 count++;
1464 #if 1
1465                 if (enable_meter)
1466                         lutil_meter_update(&meter, count, 0);
1467 #else
1468                 {
1469                         int len;
1470                         ptr = data.mv_data;
1471                         len = (ptr[0] & 0x7f) << 8 | ptr[1];
1472                         printf("ID: %zu, %zu, %.*s\n", stack[depth].id, num[depth], len, ptr+2);
1473                 }
1474 #endif
1475                 writes++;
1476                 if (writes == 1000) {
1477                         mdb_cursor_close(mc);
1478                         rc = mdb_txn_commit(mt);
1479                         if (rc) {
1480                                 Debug(LDAP_DEBUG_ANY, "mdb_dn2id_upgrade: mdb_txn_commit failed, %s (%d)\n",
1481                                         mdb_strerror(rc), rc, 0 );
1482                                 goto leave;
1483                         }
1484                         rc = mdb_txn_begin(mi->mi_dbenv, NULL, 0, &mt);
1485                         if (rc) {
1486                                 Debug(LDAP_DEBUG_ANY, "mdb_dn2id_upgrade: mdb_txn_begin(2) failed, %s (%d)\n",
1487                                         mdb_strerror(rc), rc, 0 );
1488                                 goto leave;
1489                         }
1490                         rc = mdb_cursor_open(mt, mi->mi_dbis[MDB_DN2ID], &mc);
1491                         if (rc) {
1492                                 Debug(LDAP_DEBUG_ANY, "mdb_dn2id_upgrade: mdb_cursor_open(2) failed, %s (%d)\n",
1493                                         mdb_strerror(rc), rc, 0 );
1494                                 goto leave;
1495                         }
1496                         rc = mdb_cursor_get(mc, &key, &data, MDB_GET_BOTH);
1497                         if (rc) {
1498                                 Debug(LDAP_DEBUG_ANY, "mdb_dn2id_upgrade: mdb_cursor_get(2) failed, %s (%d)\n",
1499                                         mdb_strerror(rc), rc, 0 );
1500                                 goto leave;
1501                         }
1502                         writes = 0;
1503                 }
1504                 depth--;
1505
1506                 rc = mdb_cursor_get(mc, &key, &data, MDB_NEXT_DUP);
1507                 if (rc == 0)
1508                         goto down;
1509                 rc = 0;
1510                 if (depth)
1511                         goto pop;
1512                 else
1513                         break;
1514         }
1515 leave:
1516         mdb_cursor_close(mc);
1517         if (mt) {
1518                 int r2;
1519                 r2 = mdb_txn_commit(mt);
1520                 if (r2) {
1521                         Debug(LDAP_DEBUG_ANY, "mdb_dn2id_upgrade: mdb_txn_commit(2) failed, %s (%d)\n",
1522                                 mdb_strerror(r2), r2, 0 );
1523                         if (!rc)
1524                                 rc = r2;
1525                 }
1526         }
1527         ch_free(num);
1528         if (enable_meter) {
1529                 lutil_meter_update(&meter, count, 1);
1530                 lutil_meter_close(&meter);
1531         }
1532         return rc;
1533 }