]> git.sur5r.net Git - openldap/blob - servers/slapd/back-mdb/tools.c
ITS#7604 fix modrdn, base-scope search
[openldap] / servers / slapd / back-mdb / tools.c
1 /* tools.c - tools for slap tools */
2 /* $OpenLDAP$ */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4  *
5  * Copyright 2011-2013 The OpenLDAP Foundation.
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted only as authorized by the OpenLDAP
10  * Public License.
11  *
12  * A copy of this license is available in the file LICENSE in the
13  * top-level directory of the distribution or, alternatively, at
14  * <http://www.OpenLDAP.org/license.html>.
15  */
16
17 #include "portable.h"
18
19 #include <stdio.h>
20 #include <ac/string.h>
21 #include <ac/errno.h>
22
23 #define AVL_INTERNAL
24 #include "back-mdb.h"
25 #include "idl.h"
26
27 #ifdef MDB_TOOL_IDL_CACHING
28 static int mdb_tool_idl_flush( BackendDB *be, MDB_txn *txn );
29
30 #define IDBLOCK 1024
31
32 typedef struct mdb_tool_idl_cache_entry {
33         struct mdb_tool_idl_cache_entry *next;
34         ID ids[IDBLOCK];
35 } mdb_tool_idl_cache_entry;
36
37 typedef struct mdb_tool_idl_cache {
38         struct berval kstr;
39         mdb_tool_idl_cache_entry *head, *tail;
40         ID first, last;
41         int count;
42         short offset;
43         short flags;
44 } mdb_tool_idl_cache;
45 #define WAS_FOUND       0x01
46 #define WAS_RANGE       0x02
47
48 #define MDB_TOOL_IDL_FLUSH(be, txn)     mdb_tool_idl_flush(be, txn)
49 #else
50 #define MDB_TOOL_IDL_FLUSH(be, txn)
51 #endif /* MDB_TOOL_IDL_CACHING */
52
53 static MDB_txn *txn = NULL, *txi = NULL;
54 static MDB_cursor *cursor = NULL, *idcursor = NULL;
55 static MDB_cursor *mcp = NULL, *mcd = NULL;
56 static MDB_val key, data;
57 static ID previd = NOID;
58
59 typedef struct dn_id {
60         ID id;
61         struct berval dn;
62 } dn_id;
63
64 #define HOLE_SIZE       4096
65 static dn_id hbuf[HOLE_SIZE], *holes = hbuf;
66 static unsigned nhmax = HOLE_SIZE;
67 static unsigned nholes;
68
69 static struct berval    *tool_base;
70 static int              tool_scope;
71 static Filter           *tool_filter;
72 static Entry            *tool_next_entry;
73
74 static ID mdb_tool_ix_id;
75 static Operation *mdb_tool_ix_op;
76 static MDB_txn *mdb_tool_ix_txn;
77 static int mdb_tool_index_tcount, mdb_tool_threads;
78 static IndexRec *mdb_tool_index_rec;
79 static struct mdb_info *mdb_tool_info;
80 static ldap_pvt_thread_mutex_t mdb_tool_index_mutex;
81 static ldap_pvt_thread_cond_t mdb_tool_index_cond_main;
82 static ldap_pvt_thread_cond_t mdb_tool_index_cond_work;
83 static void * mdb_tool_index_task( void *ctx, void *ptr );
84
85 static int      mdb_writes, mdb_writes_per_commit;
86
87 /* Number of ops per commit in Quick mode.
88  * Batching speeds writes overall, but too large a
89  * batch will fail with MDB_TXN_FULL.
90  */
91 #ifndef MDB_WRITES_PER_COMMIT
92 #define MDB_WRITES_PER_COMMIT   500
93 #endif
94
95 static int
96 mdb_tool_entry_get_int( BackendDB *be, ID id, Entry **ep );
97
98 int mdb_tool_entry_open(
99         BackendDB *be, int mode )
100 {
101         /* In Quick mode, commit once per 500 entries */
102         mdb_writes = 0;
103         if ( slapMode & SLAP_TOOL_QUICK )
104                 mdb_writes_per_commit = MDB_WRITES_PER_COMMIT;
105         else
106                 mdb_writes_per_commit = 1;
107
108         /* Set up for threaded slapindex */
109         if (( slapMode & (SLAP_TOOL_QUICK|SLAP_TOOL_READONLY)) == SLAP_TOOL_QUICK ) {
110                 if ( !mdb_tool_info ) {
111                         struct mdb_info *mdb = (struct mdb_info *) be->be_private;
112                         ldap_pvt_thread_mutex_init( &mdb_tool_index_mutex );
113                         ldap_pvt_thread_cond_init( &mdb_tool_index_cond_main );
114                         ldap_pvt_thread_cond_init( &mdb_tool_index_cond_work );
115                         if ( mdb->mi_nattrs ) {
116                                 int i;
117 #if 0                   /* threaded indexing has no performance advantage */
118                                 mdb_tool_threads = slap_tool_thread_max - 1;
119 #endif
120                                 if ( mdb_tool_threads > 1 ) {
121                                         mdb_tool_index_rec = ch_calloc( mdb->mi_nattrs, sizeof( IndexRec ));
122                                         mdb_tool_index_tcount = mdb_tool_threads - 1;
123                                         for (i=1; i<mdb_tool_threads; i++) {
124                                                 int *ptr = ch_malloc( sizeof( int ));
125                                                 *ptr = i;
126                                                 ldap_pvt_thread_pool_submit( &connection_pool,
127                                                         mdb_tool_index_task, ptr );
128                                         }
129                                         mdb_tool_info = mdb;
130                                 }
131                         }
132                 }
133         }
134
135         return 0;
136 }
137
138 int mdb_tool_entry_close(
139         BackendDB *be )
140 {
141         if ( mdb_tool_info ) {
142                 slapd_shutdown = 1;
143                 ldap_pvt_thread_mutex_lock( &mdb_tool_index_mutex );
144
145                 /* There might still be some threads starting */
146                 while ( mdb_tool_index_tcount > 0 ) {
147                         ldap_pvt_thread_cond_wait( &mdb_tool_index_cond_main,
148                                         &mdb_tool_index_mutex );
149                 }
150
151                 mdb_tool_index_tcount = mdb_tool_threads - 1;
152                 ldap_pvt_thread_cond_broadcast( &mdb_tool_index_cond_work );
153
154                 /* Make sure all threads are stopped */
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                 ldap_pvt_thread_mutex_unlock( &mdb_tool_index_mutex );
160
161                 mdb_tool_info = NULL;
162                 slapd_shutdown = 0;
163                 ch_free( mdb_tool_index_rec );
164                 mdb_tool_index_tcount = mdb_tool_threads - 1;
165         }
166
167         if( idcursor ) {
168                 mdb_cursor_close( idcursor );
169                 idcursor = NULL;
170         }
171         if( cursor ) {
172                 mdb_cursor_close( cursor );
173                 cursor = NULL;
174         }
175         if( txn ) {
176                 int rc;
177                 MDB_TOOL_IDL_FLUSH( be, txn );
178                 if (( rc = mdb_txn_commit( txn ))) {
179                         Debug( LDAP_DEBUG_ANY,
180                                 LDAP_XSTRING(mdb_tool_entry_close) ": database %s: "
181                                 "txn_commit failed: %s (%d)\n",
182                                 be->be_suffix[0].bv_val, mdb_strerror(rc), rc );
183                         return -1;
184                 }
185                 txn = NULL;
186         }
187
188         if( nholes ) {
189                 unsigned i;
190                 fprintf( stderr, "Error, entries missing!\n");
191                 for (i=0; i<nholes; i++) {
192                         fprintf(stderr, "  entry %ld: %s\n",
193                                 holes[i].id, holes[i].dn.bv_val);
194                 }
195                 nholes = 0;
196                 return -1;
197         }
198
199         return 0;
200 }
201
202 ID
203 mdb_tool_entry_first_x(
204         BackendDB *be,
205         struct berval *base,
206         int scope,
207         Filter *f )
208 {
209         tool_base = base;
210         tool_scope = scope;
211         tool_filter = f;
212
213         return mdb_tool_entry_next( be );
214 }
215
216 ID mdb_tool_entry_next(
217         BackendDB *be )
218 {
219         int rc;
220         ID id;
221         struct mdb_info *mdb;
222
223         assert( be != NULL );
224         assert( slapMode & SLAP_TOOL_MODE );
225
226         mdb = (struct mdb_info *) be->be_private;
227         assert( mdb != NULL );
228
229         if ( !txn ) {
230                 rc = mdb_txn_begin( mdb->mi_dbenv, NULL, MDB_RDONLY, &txn );
231                 if ( rc )
232                         return NOID;
233                 rc = mdb_cursor_open( txn, mdb->mi_id2entry, &cursor );
234                 if ( rc ) {
235                         mdb_txn_abort( txn );
236                         return NOID;
237                 }
238         }
239
240 next:;
241         rc = mdb_cursor_get( cursor, &key, &data, MDB_NEXT );
242
243         if( rc ) {
244                 return NOID;
245         }
246
247         previd = *(ID *)key.mv_data;
248         id = previd;
249
250         if ( !data.mv_size )
251                 goto next;
252
253         if ( tool_filter || tool_base ) {
254                 static Operation op = {0};
255                 static Opheader ohdr = {0};
256
257                 op.o_hdr = &ohdr;
258                 op.o_bd = be;
259                 op.o_tmpmemctx = NULL;
260                 op.o_tmpmfuncs = &ch_mfuncs;
261
262                 if ( tool_next_entry ) {
263                         mdb_entry_release( &op, tool_next_entry, 0 );
264                         tool_next_entry = NULL;
265                 }
266
267                 rc = mdb_tool_entry_get_int( be, id, &tool_next_entry );
268                 if ( rc == LDAP_NO_SUCH_OBJECT ) {
269                         goto next;
270                 }
271
272                 assert( tool_next_entry != NULL );
273
274                 if ( tool_filter && test_filter( NULL, tool_next_entry, tool_filter ) != LDAP_COMPARE_TRUE )
275                 {
276                         mdb_entry_release( &op, tool_next_entry, 0 );
277                         tool_next_entry = NULL;
278                         goto next;
279                 }
280         }
281
282         return id;
283 }
284
285 ID mdb_tool_dn2id_get(
286         Backend *be,
287         struct berval *dn
288 )
289 {
290         struct mdb_info *mdb;
291         Operation op = {0};
292         Opheader ohdr = {0};
293         ID id;
294         int rc;
295
296         if ( BER_BVISEMPTY(dn) )
297                 return 0;
298
299         mdb = (struct mdb_info *) be->be_private;
300
301         if ( !txn ) {
302                 rc = mdb_txn_begin( mdb->mi_dbenv, NULL, (slapMode & SLAP_TOOL_READONLY) != 0 ?
303                         MDB_RDONLY : 0, &txn );
304                 if ( rc )
305                         return NOID;
306         }
307
308         op.o_hdr = &ohdr;
309         op.o_bd = be;
310         op.o_tmpmemctx = NULL;
311         op.o_tmpmfuncs = &ch_mfuncs;
312
313         rc = mdb_dn2id( &op, txn, NULL, dn, &id, NULL, NULL, NULL );
314         if ( rc == MDB_NOTFOUND )
315                 return NOID;
316
317         return id;
318 }
319
320 static int
321 mdb_tool_entry_get_int( BackendDB *be, ID id, Entry **ep )
322 {
323         Operation op = {0};
324         Opheader ohdr = {0};
325
326         Entry *e = NULL;
327         struct berval dn = BER_BVNULL, ndn = BER_BVNULL;
328         int rc;
329
330         assert( be != NULL );
331         assert( slapMode & SLAP_TOOL_MODE );
332
333         if ( ( tool_filter || tool_base ) && id == previd && tool_next_entry != NULL ) {
334                 *ep = tool_next_entry;
335                 tool_next_entry = NULL;
336                 return LDAP_SUCCESS;
337         }
338
339         if ( id != previd ) {
340                 key.mv_size = sizeof(ID);
341                 key.mv_data = &id;
342                 rc = mdb_cursor_get( cursor, &key, &data, MDB_SET );
343                 if ( rc ) {
344                         rc = LDAP_OTHER;
345                         goto done;
346                 }
347         }
348         if ( !data.mv_size ) {
349                 rc = LDAP_NO_SUCH_OBJECT;
350                 goto done;
351         }
352
353         op.o_hdr = &ohdr;
354         op.o_bd = be;
355         op.o_tmpmemctx = NULL;
356         op.o_tmpmfuncs = &ch_mfuncs;
357         if ( slapMode & SLAP_TOOL_READONLY ) {
358                 rc = mdb_id2name( &op, txn, &idcursor, id, &dn, &ndn );
359                 if ( rc  ) {
360                         rc = LDAP_OTHER;
361                         if ( e ) {
362                                 mdb_entry_return( &op, e );
363                                 e = NULL;
364                         }
365                         goto done;
366                 }
367                 if ( tool_base != NULL ) {
368                         if ( !dnIsSuffixScope( &ndn, tool_base, tool_scope ) ) {
369                                 ch_free( dn.bv_val );
370                                 ch_free( ndn.bv_val );
371                                 rc = LDAP_NO_SUCH_OBJECT;
372                                 goto done;
373                         }
374                 }
375         }
376         rc = mdb_entry_decode( &op, &data, &e );
377         e->e_id = id;
378         if ( !BER_BVISNULL( &dn )) {
379                 e->e_name = dn;
380                 e->e_nname = ndn;
381         } else {
382                 e->e_name.bv_val = NULL;
383                 e->e_nname.bv_val = NULL;
384         }
385
386 done:
387         if ( e != NULL ) {
388                 *ep = e;
389         }
390
391         return rc;
392 }
393
394 Entry*
395 mdb_tool_entry_get( BackendDB *be, ID id )
396 {
397         Entry *e = NULL;
398         int rc;
399
400         if ( !txn ) {
401                 struct mdb_info *mdb = (struct mdb_info *) be->be_private;
402                 rc = mdb_txn_begin( mdb->mi_dbenv, NULL,
403                         (slapMode & SLAP_TOOL_READONLY) ? MDB_RDONLY : 0, &txn );
404                 if ( rc )
405                         return NULL;
406         }
407         if ( !cursor ) {
408                 struct mdb_info *mdb = (struct mdb_info *) be->be_private;
409                 rc = mdb_cursor_open( txn, mdb->mi_id2entry, &cursor );
410                 if ( rc ) {
411                         mdb_txn_abort( txn );
412                         txn = NULL;
413                         return NULL;
414                 }
415         }
416         (void)mdb_tool_entry_get_int( be, id, &e );
417         return e;
418 }
419
420 static int mdb_tool_next_id(
421         Operation *op,
422         MDB_txn *tid,
423         Entry *e,
424         struct berval *text,
425         int hole )
426 {
427         struct mdb_info *mdb = (struct mdb_info *) op->o_bd->be_private;
428         struct berval dn = e->e_name;
429         struct berval ndn = e->e_nname;
430         struct berval pdn, npdn, nmatched;
431         ID id, pid = 0;
432         int rc;
433
434         if (ndn.bv_len == 0) {
435                 e->e_id = 0;
436                 return 0;
437         }
438
439         rc = mdb_dn2id( op, tid, mcp, &ndn, &id, NULL, NULL, &nmatched );
440         if ( rc == MDB_NOTFOUND ) {
441                 if ( !be_issuffix( op->o_bd, &ndn ) ) {
442                         ID eid = e->e_id;
443                         dnParent( &ndn, &npdn );
444                         if ( nmatched.bv_len != npdn.bv_len ) {
445                                 dnParent( &dn, &pdn );
446                                 e->e_name = pdn;
447                                 e->e_nname = npdn;
448                                 rc = mdb_tool_next_id( op, tid, e, text, 1 );
449                                 e->e_name = dn;
450                                 e->e_nname = ndn;
451                                 if ( rc ) {
452                                         return rc;
453                                 }
454                                 /* If parent didn't exist, it was created just now
455                                  * and its ID is now in e->e_id. Make sure the current
456                                  * entry gets added under the new parent ID.
457                                  */
458                                 if ( eid != e->e_id ) {
459                                         pid = e->e_id;
460                                 }
461                         } else {
462                                 pid = id;
463                         }
464                 }
465                 rc = mdb_next_id( op->o_bd, idcursor, &e->e_id );
466                 if ( rc ) {
467                         snprintf( text->bv_val, text->bv_len,
468                                 "next_id failed: %s (%d)",
469                                 mdb_strerror(rc), rc );
470                 Debug( LDAP_DEBUG_ANY,
471                         "=> mdb_tool_next_id: %s\n", text->bv_val, 0, 0 );
472                         return rc;
473                 }
474                 rc = mdb_dn2id_add( op, mcp, mcd, pid, 1, 1, e );
475                 if ( rc ) {
476                         snprintf( text->bv_val, text->bv_len,
477                                 "dn2id_add failed: %s (%d)",
478                                 mdb_strerror(rc), rc );
479                         Debug( LDAP_DEBUG_ANY,
480                                 "=> mdb_tool_next_id: %s\n", text->bv_val, 0, 0 );
481                 } else if ( hole ) {
482                         MDB_val key, data;
483                         if ( nholes == nhmax - 1 ) {
484                                 if ( holes == hbuf ) {
485                                         holes = ch_malloc( nhmax * sizeof(dn_id) * 2 );
486                                         AC_MEMCPY( holes, hbuf, sizeof(hbuf) );
487                                 } else {
488                                         holes = ch_realloc( holes, nhmax * sizeof(dn_id) * 2 );
489                                 }
490                                 nhmax *= 2;
491                         }
492                         ber_dupbv( &holes[nholes].dn, &ndn );
493                         holes[nholes++].id = e->e_id;
494                         key.mv_size = sizeof(ID);
495                         key.mv_data = &e->e_id;
496                         data.mv_size = 0;
497                         data.mv_data = NULL;
498                         rc = mdb_cursor_put( idcursor, &key, &data, MDB_NOOVERWRITE );
499                         if ( rc == MDB_KEYEXIST )
500                                 rc = 0;
501                         if ( rc ) {
502                                 snprintf( text->bv_val, text->bv_len,
503                                         "dummy id2entry add failed: %s (%d)",
504                                         mdb_strerror(rc), rc );
505                                 Debug( LDAP_DEBUG_ANY,
506                                         "=> mdb_tool_next_id: %s\n", text->bv_val, 0, 0 );
507                         }
508                 }
509         } else if ( !hole ) {
510                 unsigned i, j;
511
512                 e->e_id = id;
513
514                 for ( i=0; i<nholes; i++) {
515                         if ( holes[i].id == e->e_id ) {
516                                 free(holes[i].dn.bv_val);
517                                 for (j=i;j<nholes;j++) holes[j] = holes[j+1];
518                                 holes[j].id = 0;
519                                 nholes--;
520                                 break;
521                         } else if ( holes[i].id > e->e_id ) {
522                                 break;
523                         }
524                 }
525         }
526         return rc;
527 }
528
529 static int
530 mdb_tool_index_add(
531         Operation *op,
532         MDB_txn *txn,
533         Entry *e )
534 {
535         struct mdb_info *mdb = (struct mdb_info *) op->o_bd->be_private;
536
537         if ( !mdb->mi_nattrs )
538                 return 0;
539
540         if ( mdb_tool_threads > 1 ) {
541                 IndexRec *ir;
542                 int i, rc;
543                 Attribute *a;
544
545                 ir = mdb_tool_index_rec;
546                 for (i=0; i<mdb->mi_nattrs; i++)
547                         ir[i].ir_attrs = NULL;
548
549                 for ( a = e->e_attrs; a != NULL; a = a->a_next ) {
550                         rc = mdb_index_recset( mdb, a, a->a_desc->ad_type,
551                                 &a->a_desc->ad_tags, ir );
552                         if ( rc )
553                                 return rc;
554                 }
555                 for (i=0; i<mdb->mi_nattrs; i++) {
556                         if ( !ir[i].ir_ai )
557                                 break;
558                         rc = mdb_cursor_open( txn, ir[i].ir_ai->ai_dbi,
559                                  &ir[i].ir_ai->ai_cursor );
560                         if ( rc )
561                                 return rc;
562                 }
563                 mdb_tool_ix_id = e->e_id;
564                 mdb_tool_ix_op = op;
565                 mdb_tool_ix_txn = txn;
566                 ldap_pvt_thread_mutex_lock( &mdb_tool_index_mutex );
567                 /* Wait for all threads to be ready */
568                 while ( mdb_tool_index_tcount ) {
569                         ldap_pvt_thread_cond_wait( &mdb_tool_index_cond_main,
570                                 &mdb_tool_index_mutex );
571                 }
572
573                 for ( i=1; i<mdb_tool_threads; i++ )
574                         mdb_tool_index_rec[i].ir_i = LDAP_BUSY;
575                 mdb_tool_index_tcount = mdb_tool_threads - 1;
576                 ldap_pvt_thread_mutex_unlock( &mdb_tool_index_mutex );
577                 ldap_pvt_thread_cond_broadcast( &mdb_tool_index_cond_work );
578
579                 rc = mdb_index_recrun( op, txn, mdb, ir, e->e_id, 0 );
580                 if ( rc )
581                         return rc;
582                 ldap_pvt_thread_mutex_lock( &mdb_tool_index_mutex );
583                 for ( i=1; i<mdb_tool_threads; i++ ) {
584                         if ( mdb_tool_index_rec[i].ir_i == LDAP_BUSY ) {
585                                 ldap_pvt_thread_cond_wait( &mdb_tool_index_cond_main,
586                                         &mdb_tool_index_mutex );
587                                 i--;
588                                 continue;
589                         }
590                         if ( mdb_tool_index_rec[i].ir_i ) {
591                                 rc = mdb_tool_index_rec[i].ir_i;
592                                 break;
593                         }
594                 }
595                 ldap_pvt_thread_mutex_unlock( &mdb_tool_index_mutex );
596                 return rc;
597         } else
598         {
599                 return mdb_index_entry_add( op, txn, e );
600         }
601 }
602
603 ID mdb_tool_entry_put(
604         BackendDB *be,
605         Entry *e,
606         struct berval *text )
607 {
608         int rc;
609         struct mdb_info *mdb;
610         Operation op = {0};
611         Opheader ohdr = {0};
612
613         assert( be != NULL );
614         assert( slapMode & SLAP_TOOL_MODE );
615
616         assert( text != NULL );
617         assert( text->bv_val != NULL );
618         assert( text->bv_val[0] == '\0' );      /* overconservative? */
619
620         Debug( LDAP_DEBUG_TRACE, "=> " LDAP_XSTRING(mdb_tool_entry_put)
621                 "( %ld, \"%s\" )\n", (long) e->e_id, e->e_dn, 0 );
622
623         mdb = (struct mdb_info *) be->be_private;
624
625         if ( !txn ) {
626                 rc = mdb_txn_begin( mdb->mi_dbenv, NULL, 0, &txn );
627                 if( rc != 0 ) {
628                         snprintf( text->bv_val, text->bv_len,
629                                 "txn_begin failed: %s (%d)",
630                                 mdb_strerror(rc), rc );
631                         Debug( LDAP_DEBUG_ANY,
632                                 "=> " LDAP_XSTRING(mdb_tool_entry_put) ": %s\n",
633                                  text->bv_val, 0, 0 );
634                         return NOID;
635                 }
636                 rc = mdb_cursor_open( txn, mdb->mi_id2entry, &idcursor );
637                 if( rc != 0 ) {
638                         snprintf( text->bv_val, text->bv_len,
639                                 "cursor_open failed: %s (%d)",
640                                 mdb_strerror(rc), rc );
641                         Debug( LDAP_DEBUG_ANY,
642                                 "=> " LDAP_XSTRING(mdb_tool_entry_put) ": %s\n",
643                                  text->bv_val, 0, 0 );
644                         return NOID;
645                 }
646                 if ( !mdb->mi_nextid ) {
647                         ID dummy;
648                         mdb_next_id( be, idcursor, &dummy );
649                 }
650                 rc = mdb_cursor_open( txn, mdb->mi_dn2id, &mcp );
651                 if( rc != 0 ) {
652                         snprintf( text->bv_val, text->bv_len,
653                                 "cursor_open failed: %s (%d)",
654                                 mdb_strerror(rc), rc );
655                         Debug( LDAP_DEBUG_ANY,
656                                 "=> " LDAP_XSTRING(mdb_tool_entry_put) ": %s\n",
657                                  text->bv_val, 0, 0 );
658                         return NOID;
659                 }
660                 rc = mdb_cursor_open( txn, mdb->mi_dn2id, &mcd );
661                 if( rc != 0 ) {
662                         snprintf( text->bv_val, text->bv_len,
663                                 "cursor_open failed: %s (%d)",
664                                 mdb_strerror(rc), rc );
665                         Debug( LDAP_DEBUG_ANY,
666                                 "=> " LDAP_XSTRING(mdb_tool_entry_put) ": %s\n",
667                                  text->bv_val, 0, 0 );
668                         return NOID;
669                 }
670         }
671
672         op.o_hdr = &ohdr;
673         op.o_bd = be;
674         op.o_tmpmemctx = NULL;
675         op.o_tmpmfuncs = &ch_mfuncs;
676
677         /* add dn2id indices */
678         rc = mdb_tool_next_id( &op, txn, e, text, 0 );
679         if( rc != 0 ) {
680                 goto done;
681         }
682
683         rc = mdb_tool_index_add( &op, txn, e );
684         if( rc != 0 ) {
685                 snprintf( text->bv_val, text->bv_len,
686                                 "index_entry_add failed: err=%d", rc );
687                 Debug( LDAP_DEBUG_ANY,
688                         "=> " LDAP_XSTRING(mdb_tool_entry_put) ": %s\n",
689                         text->bv_val, 0, 0 );
690                 goto done;
691         }
692
693
694         /* id2entry index */
695         rc = mdb_id2entry_add( &op, txn, idcursor, e );
696         if( rc != 0 ) {
697                 snprintf( text->bv_val, text->bv_len,
698                                 "id2entry_add failed: err=%d", rc );
699                 Debug( LDAP_DEBUG_ANY,
700                         "=> " LDAP_XSTRING(mdb_tool_entry_put) ": %s\n",
701                         text->bv_val, 0, 0 );
702                 goto done;
703         }
704
705 done:
706         if( rc == 0 ) {
707                 mdb_writes++;
708                 if ( mdb_writes >= mdb_writes_per_commit ) {
709                         unsigned i;
710                         MDB_TOOL_IDL_FLUSH( be, txn );
711                         rc = mdb_txn_commit( txn );
712                         for ( i=0; i<mdb->mi_nattrs; i++ )
713                                 mdb->mi_attrs[i]->ai_cursor = NULL;
714                         mdb_writes = 0;
715                         txn = NULL;
716                         idcursor = NULL;
717                         if( rc != 0 ) {
718                                 snprintf( text->bv_val, text->bv_len,
719                                                 "txn_commit failed: %s (%d)",
720                                                 mdb_strerror(rc), rc );
721                                 Debug( LDAP_DEBUG_ANY,
722                                         "=> " LDAP_XSTRING(mdb_tool_entry_put) ": %s\n",
723                                         text->bv_val, 0, 0 );
724                                 e->e_id = NOID;
725                         }
726                 }
727
728         } else {
729                 unsigned i;
730                 mdb_txn_abort( txn );
731                 txn = NULL;
732                 idcursor = NULL;
733                 for ( i=0; i<mdb->mi_nattrs; i++ )
734                         mdb->mi_attrs[i]->ai_cursor = NULL;
735                 mdb_writes = 0;
736                 snprintf( text->bv_val, text->bv_len,
737                         "txn_aborted! %s (%d)",
738                         rc == LDAP_OTHER ? "Internal error" :
739                         mdb_strerror(rc), rc );
740                 Debug( LDAP_DEBUG_ANY,
741                         "=> " LDAP_XSTRING(mdb_tool_entry_put) ": %s\n",
742                         text->bv_val, 0, 0 );
743                 e->e_id = NOID;
744         }
745
746         return e->e_id;
747 }
748
749 static int mdb_dn2id_upgrade( BackendDB *be );
750
751 int mdb_tool_entry_reindex(
752         BackendDB *be,
753         ID id,
754         AttributeDescription **adv )
755 {
756         struct mdb_info *mi = (struct mdb_info *) be->be_private;
757         int rc;
758         Entry *e;
759         Operation op = {0};
760         Opheader ohdr = {0};
761
762         Debug( LDAP_DEBUG_ARGS,
763                 "=> " LDAP_XSTRING(mdb_tool_entry_reindex) "( %ld )\n",
764                 (long) id, 0, 0 );
765         assert( tool_base == NULL );
766         assert( tool_filter == NULL );
767
768         /* Special: do a dn2id upgrade */
769         if ( adv && adv[0] == slap_schema.si_ad_entryDN ) {
770                 /* short-circuit tool_entry_next() */
771                 mdb_cursor_get( cursor, &key, &data, MDB_LAST );
772                 return mdb_dn2id_upgrade( be );
773         }
774
775         /* No indexes configured, nothing to do. Could return an
776          * error here to shortcut things.
777          */
778         if (!mi->mi_attrs) {
779                 return 0;
780         }
781
782         /* Check for explicit list of attrs to index */
783         if ( adv ) {
784                 int i, j, n;
785
786                 if ( mi->mi_attrs[0]->ai_desc != adv[0] ) {
787                         /* count */
788                         for ( n = 0; adv[n]; n++ ) ;
789
790                         /* insertion sort */
791                         for ( i = 0; i < n; i++ ) {
792                                 AttributeDescription *ad = adv[i];
793                                 for ( j = i-1; j>=0; j--) {
794                                         if ( SLAP_PTRCMP( adv[j], ad ) <= 0 ) break;
795                                         adv[j+1] = adv[j];
796                                 }
797                                 adv[j+1] = ad;
798                         }
799                 }
800
801                 for ( i = 0; adv[i]; i++ ) {
802                         if ( mi->mi_attrs[i]->ai_desc != adv[i] ) {
803                                 for ( j = i+1; j < mi->mi_nattrs; j++ ) {
804                                         if ( mi->mi_attrs[j]->ai_desc == adv[i] ) {
805                                                 AttrInfo *ai = mi->mi_attrs[i];
806                                                 mi->mi_attrs[i] = mi->mi_attrs[j];
807                                                 mi->mi_attrs[j] = ai;
808                                                 break;
809                                         }
810                                 }
811                                 if ( j == mi->mi_nattrs ) {
812                                         Debug( LDAP_DEBUG_ANY,
813                                                 LDAP_XSTRING(mdb_tool_entry_reindex)
814                                                 ": no index configured for %s\n",
815                                                 adv[i]->ad_cname.bv_val, 0, 0 );
816                                         return -1;
817                                 }
818                         }
819                 }
820                 mi->mi_nattrs = i;
821         }
822
823         e = mdb_tool_entry_get( be, id );
824
825         if( e == NULL ) {
826                 Debug( LDAP_DEBUG_ANY,
827                         LDAP_XSTRING(mdb_tool_entry_reindex)
828                         ": could not locate id=%ld\n",
829                         (long) id, 0, 0 );
830                 return -1;
831         }
832
833         if ( !txi ) {
834                 rc = mdb_txn_begin( mi->mi_dbenv, NULL, 0, &txi );
835                 if( rc != 0 ) {
836                         Debug( LDAP_DEBUG_ANY,
837                                 "=> " LDAP_XSTRING(mdb_tool_entry_reindex) ": "
838                                 "txn_begin failed: %s (%d)\n",
839                                 mdb_strerror(rc), rc, 0 );
840                         goto done;
841                 }
842         }
843
844         if ( slapMode & SLAP_TRUNCATE_MODE ) {
845                 int i;
846                 for ( i=0; i < mi->mi_nattrs; i++ ) {
847                         rc = mdb_drop( txi, mi->mi_attrs[i]->ai_dbi, 0 );
848                         if ( rc ) {
849                                 Debug( LDAP_DEBUG_ANY,
850                                         LDAP_XSTRING(mdb_tool_entry_reindex)
851                                         ": (Truncate) mdb_drop(%s) failed: %s (%d)\n",
852                                         mi->mi_attrs[i]->ai_desc->ad_type->sat_cname.bv_val,
853                                         mdb_strerror(rc), rc );
854                                 return -1;
855                         }
856                 }
857                 slapMode ^= SLAP_TRUNCATE_MODE;
858         }
859
860         /*
861          * just (re)add them for now
862          * Use truncate mode to empty/reset index databases
863          */
864
865         Debug( LDAP_DEBUG_TRACE,
866                 "=> " LDAP_XSTRING(mdb_tool_entry_reindex) "( %ld )\n",
867                 (long) id, 0, 0 );
868
869         op.o_hdr = &ohdr;
870         op.o_bd = be;
871         op.o_tmpmemctx = NULL;
872         op.o_tmpmfuncs = &ch_mfuncs;
873
874         rc = mdb_tool_index_add( &op, txi, e );
875
876 done:
877         if( rc == 0 ) {
878                 mdb_writes++;
879                 if ( mdb_writes >= mdb_writes_per_commit ) {
880                         MDB_val key;
881                         unsigned i;
882                         MDB_TOOL_IDL_FLUSH( be, txi );
883                         rc = mdb_txn_commit( txi );
884                         mdb_writes = 0;
885                         for ( i=0; i<mi->mi_nattrs; i++ )
886                                 mi->mi_attrs[i]->ai_cursor = NULL;
887                         if( rc != 0 ) {
888                                 Debug( LDAP_DEBUG_ANY,
889                                         "=> " LDAP_XSTRING(mdb_tool_entry_reindex)
890                                         ": txn_commit failed: %s (%d)\n",
891                                         mdb_strerror(rc), rc, 0 );
892                                 e->e_id = NOID;
893                         }
894                         mdb_cursor_close( cursor );
895                         txi = NULL;
896                         /* Must close the read txn to allow old pages to be reclaimed. */
897                         mdb_txn_abort( txn );
898                         /* and then reopen it so that tool_entry_next still works. */
899                         mdb_txn_begin( mi->mi_dbenv, NULL, MDB_RDONLY, &txn );
900                         mdb_cursor_open( txn, mi->mi_id2entry, &cursor );
901                         key.mv_data = &id;
902                         key.mv_size = sizeof(ID);
903                         mdb_cursor_get( cursor, &key, NULL, MDB_SET );
904                 }
905
906         } else {
907                 unsigned i;
908                 mdb_writes = 0;
909                 mdb_cursor_close( cursor );
910                 cursor = NULL;
911                 mdb_txn_abort( txi );
912                 for ( i=0; i<mi->mi_nattrs; i++ )
913                         mi->mi_attrs[i]->ai_cursor = NULL;
914                 Debug( LDAP_DEBUG_ANY,
915                         "=> " LDAP_XSTRING(mdb_tool_entry_reindex)
916                         ": txn_aborted! err=%d\n",
917                         rc, 0, 0 );
918                 e->e_id = NOID;
919                 txi = NULL;
920         }
921         mdb_entry_release( &op, e, 0 );
922
923         return rc;
924 }
925
926 ID mdb_tool_entry_modify(
927         BackendDB *be,
928         Entry *e,
929         struct berval *text )
930 {
931         int rc;
932         struct mdb_info *mdb;
933         Operation op = {0};
934         Opheader ohdr = {0};
935
936         assert( be != NULL );
937         assert( slapMode & SLAP_TOOL_MODE );
938
939         assert( text != NULL );
940         assert( text->bv_val != NULL );
941         assert( text->bv_val[0] == '\0' );      /* overconservative? */
942
943         assert ( e->e_id != NOID );
944
945         Debug( LDAP_DEBUG_TRACE,
946                 "=> " LDAP_XSTRING(mdb_tool_entry_modify) "( %ld, \"%s\" )\n",
947                 (long) e->e_id, e->e_dn, 0 );
948
949         mdb = (struct mdb_info *) be->be_private;
950
951         if( cursor ) {
952                 mdb_cursor_close( cursor );
953                 cursor = NULL;
954         }
955         if ( !txn ) {
956                 rc = mdb_txn_begin( mdb->mi_dbenv, NULL, 0, &txn );
957                 if( rc != 0 ) {
958                         snprintf( text->bv_val, text->bv_len,
959                                 "txn_begin failed: %s (%d)",
960                                 mdb_strerror(rc), rc );
961                         Debug( LDAP_DEBUG_ANY,
962                                 "=> " LDAP_XSTRING(mdb_tool_entry_modify) ": %s\n",
963                                  text->bv_val, 0, 0 );
964                         return NOID;
965                 }
966         }
967
968         op.o_hdr = &ohdr;
969         op.o_bd = be;
970         op.o_tmpmemctx = NULL;
971         op.o_tmpmfuncs = &ch_mfuncs;
972
973         /* id2entry index */
974         rc = mdb_id2entry_update( &op, txn, NULL, e );
975         if( rc != 0 ) {
976                 snprintf( text->bv_val, text->bv_len,
977                                 "id2entry_update failed: err=%d", rc );
978                 Debug( LDAP_DEBUG_ANY,
979                         "=> " LDAP_XSTRING(mdb_tool_entry_modify) ": %s\n",
980                         text->bv_val, 0, 0 );
981                 goto done;
982         }
983
984 done:
985         if( rc == 0 ) {
986                 rc = mdb_txn_commit( txn );
987                 if( rc != 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( 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         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 = 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 = 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 }