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