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