Debian Codegraph @

servers/slapd/back-mdb/tools.c

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