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