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