]> git.sur5r.net Git - openldap/blob - servers/slapd/back-bdb/tools.c
Sync with HEAD as of 14-March-2004
[openldap] / servers / slapd / back-bdb / 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 2000-2004 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
22 #include "back-bdb.h"
23 #include "external.h"
24
25 static DBC *cursor = NULL;
26 static DBT key, data;
27
28 typedef struct dn_id {
29         ID id;
30         struct berval dn;
31 } dn_id;
32
33 #define HOLE_SIZE       4096
34 static dn_id hbuf[HOLE_SIZE], *holes = hbuf;
35 static unsigned nhmax = HOLE_SIZE;
36 static unsigned nholes;
37
38 int bdb_tool_entry_open(
39         BackendDB *be, int mode )
40 {
41         /* initialize key and data thangs */
42         DBTzero( &key );
43         DBTzero( &data );
44         key.flags = DB_DBT_REALLOC;
45         data.flags = DB_DBT_REALLOC;
46
47         return 0;
48 }
49
50 int bdb_tool_entry_close(
51         BackendDB *be )
52 {
53         assert( be != NULL );
54
55         if( key.data ) {
56                 ch_free( key.data );
57                 key.data = NULL;
58         }
59         if( data.data ) {
60                 ch_free( data.data );
61                 data.data = NULL;
62         }
63
64         if( cursor ) {
65                 cursor->c_close( cursor );
66                 cursor = NULL;
67         }
68
69         if( nholes ) {
70                 unsigned i;
71                 fprintf( stderr, "Error, entries missing!\n");
72                 for (i=0; i<nholes; i++) {
73                         fprintf(stderr, "  entry %ld: %s\n",
74                                 holes[i].id, holes[i].dn.bv_val);
75                 }
76                 return -1;
77         }
78                         
79         return 0;
80 }
81
82 ID bdb_tool_entry_next(
83         BackendDB *be )
84 {
85         int rc;
86         ID id;
87         struct bdb_info *bdb = (struct bdb_info *) be->be_private;
88
89         assert( be != NULL );
90         assert( slapMode & SLAP_TOOL_MODE );
91         assert( bdb != NULL );
92         
93         if (cursor == NULL) {
94                 rc = bdb->bi_id2entry->bdi_db->cursor(
95                         bdb->bi_id2entry->bdi_db, NULL, &cursor,
96                         bdb->bi_db_opflags );
97                 if( rc != 0 ) {
98                         return NOID;
99                 }
100         }
101
102         rc = cursor->c_get( cursor, &key, &data, DB_NEXT );
103
104         if( rc != 0 ) {
105                 return NOID;
106         }
107
108         if( data.data == NULL ) {
109                 return NOID;
110         }
111
112         AC_MEMCPY( &id, key.data, key.size );
113         return id;
114 }
115
116 ID bdb_tool_dn2id_get(
117         Backend *be,
118         struct berval *dn
119 )
120 {
121         struct bdb_info *bdb = (struct bdb_info *) be->be_private;
122         DB *db = bdb->bi_dn2id->bdi_db;
123         int rc;
124         DBT     key, data;
125         ID      id;
126
127         DBTzero( &key );
128         key.size = dn->bv_len + 2;
129         key.data = ch_malloc( key.size );
130         ((char*)key.data)[0] = DN_BASE_PREFIX;
131         AC_MEMCPY( &((char*)key.data)[1], dn->bv_val, key.size - 1 );
132
133         DBTzero( &data );
134         data.data = &id;
135         data.ulen = sizeof(ID);
136         data.flags = DB_DBT_USERMEM;
137
138         rc = db->get( db, NULL, &key, &data, bdb->bi_db_opflags );
139
140     if( rc != 0 ) {
141 #ifdef NEW_LOGGING
142                 LDAP_LOG ( INDEX, ERR, "bdb_tool_dn2id_get: get failed %s (%d)\n",
143                                 db_strerror(rc), rc, 0 );
144 #else
145                 Debug( LDAP_DEBUG_TRACE, "bdb_tool_dn2id_get: get failed: %s (%d)\n",
146                                 db_strerror( rc ), rc, 0 );
147 #endif
148                 id = NOID;
149         }
150
151         ch_free( key.data );
152         return id;
153 }
154
155 int bdb_tool_id2entry_get(
156         Backend *be,
157         ID id,
158         Entry **e
159 )
160 {
161         return bdb_id2entry( be, NULL, id, e );
162 }
163
164 Entry* bdb_tool_entry_get( BackendDB *be, ID id )
165 {
166         int rc;
167         Entry *e = NULL;
168         struct berval bv;
169
170         assert( be != NULL );
171         assert( slapMode & SLAP_TOOL_MODE );
172         assert( data.data != NULL );
173
174 #ifndef BDB_HIER
175         DBT2bv( &data, &bv );
176
177         rc = entry_decode( &bv, &e );
178
179         if( rc == LDAP_SUCCESS ) {
180                 e->e_id = id;
181         }
182 #else
183         {
184                 EntryInfo *ei = NULL;
185                 Operation op = {0};
186
187                 op.o_bd = be;
188                 op.o_tmpmemctx = NULL;
189                 op.o_tmpmfuncs = &ch_mfuncs;
190
191                 rc = bdb_cache_find_id( &op, NULL, id, &ei, 0, 0, NULL );
192                 if ( rc == LDAP_SUCCESS )
193                         e = ei->bei_e;
194         }
195 #endif
196         return e;
197 }
198
199 static int bdb_tool_next_id(
200         Operation *op,
201         DB_TXN *tid,
202         Entry *e,
203         struct berval *text,
204         int hole )
205 {
206         struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
207         struct berval dn = e->e_nname;
208         struct berval pdn;
209         EntryInfo *ei = NULL;
210         int rc;
211
212         rc = bdb_cache_find_ndn( op, tid, &dn, &ei );
213         if ( ei ) bdb_cache_entryinfo_unlock( ei );
214         if ( rc == DB_NOTFOUND ) {
215                 if ( be_issuffix( op->o_bd, &dn ) ) {
216                         pdn = slap_empty_bv;
217                 } else {
218                         dnParent( &dn, &pdn );
219                         e->e_nname = pdn;
220                         rc = bdb_tool_next_id( op, tid, e, text, 1 );
221                         e->e_nname = dn;
222                         if ( rc ) {
223                                 return rc;
224                         }
225                 }
226                 rc = bdb_next_id( op->o_bd, tid, &e->e_id );
227                 if ( rc ) {
228                         snprintf( text->bv_val, text->bv_len,
229                                 "next_id failed: %s (%d)",
230                                 db_strerror(rc), rc );
231 #ifdef NEW_LOGGING
232                 LDAP_LOG ( TOOLS, ERR, 
233                         "=> bdb_tool_next_id: %s\n", text->bv_val, 0, 0 );
234 #else
235                 Debug( LDAP_DEBUG_ANY,
236                         "=> bdb_tool_next_id: %s\n", text->bv_val, 0, 0 );
237 #endif
238                         return rc;
239                 }
240                 rc = bdb_dn2id_add( op, tid, ei, e );
241                 if ( rc ) {
242                         snprintf( text->bv_val, text->bv_len, 
243                                 "dn2id_add failed: %s (%d)",
244                                 db_strerror(rc), rc );
245 #ifdef NEW_LOGGING
246                 LDAP_LOG ( TOOLS, ERR, 
247                         "=> bdb_tool_next_id: %s\n", text->bv_val, 0, 0 );
248 #else
249                 Debug( LDAP_DEBUG_ANY,
250                         "=> bdb_tool_next_id: %s\n", text->bv_val, 0, 0 );
251 #endif
252                 } else if ( hole ) {
253                         if ( nholes == nhmax - 1 ) {
254                                 if ( holes == hbuf ) {
255                                         holes = ch_malloc( nhmax * sizeof(dn_id) * 2 );
256                                         AC_MEMCPY( holes, hbuf, sizeof(hbuf) );
257                                 } else {
258                                         holes = ch_realloc( holes, nhmax * sizeof(dn_id) * 2 );
259                                 }
260                                 nhmax *= 2;
261                         }
262                         ber_dupbv( &holes[nholes].dn, &dn );
263                         holes[nholes++].id = e->e_id;
264                 }
265         } else if ( !hole ) {
266                 unsigned i;
267
268                 e->e_id = ei->bei_id;
269
270                 for ( i=0; i<nholes; i++) {
271                         if ( holes[i].id == e->e_id ) {
272                                 int j;
273                                 free(holes[i].dn.bv_val);
274                                 for (j=i;j<nholes;j++) holes[j] = holes[j+1];
275                                 holes[j].id = 0;
276                                 nholes--;
277                                 break;
278                         } else if ( holes[i].id > e->e_id ) {
279                                 break;
280                         }
281                 }
282         }
283         return rc;
284 }
285
286 ID bdb_tool_entry_put(
287         BackendDB *be,
288         Entry *e,
289         struct berval *text )
290 {
291         int rc;
292         struct bdb_info *bdb = (struct bdb_info *) be->be_private;
293         DB_TXN *tid = NULL;
294         Operation op = {0};
295
296         assert( be != NULL );
297         assert( slapMode & SLAP_TOOL_MODE );
298
299         assert( text );
300         assert( text->bv_val );
301         assert( text->bv_val[0] == '\0' );      /* overconservative? */
302
303 #ifdef NEW_LOGGING
304         LDAP_LOG ( TOOLS, ARGS, "=> bdb_tool_entry_put( %ld, \"%s\" )\n",
305                 (long) e->e_id, e->e_dn, 0 );
306 #else
307         Debug( LDAP_DEBUG_TRACE, "=> bdb_tool_entry_put( %ld, \"%s\" )\n",
308                 (long) e->e_id, e->e_dn, 0 );
309 #endif
310
311         rc = TXN_BEGIN( bdb->bi_dbenv, NULL, &tid, 
312                 bdb->bi_db_opflags );
313         if( rc != 0 ) {
314                 snprintf( text->bv_val, text->bv_len,
315                         "txn_begin failed: %s (%d)",
316                         db_strerror(rc), rc );
317 #ifdef NEW_LOGGING
318         LDAP_LOG ( TOOLS, ERR, "=> bdb_tool_entry_put: %s\n", text->bv_val, 0, 0 );
319 #else
320                 Debug( LDAP_DEBUG_ANY,
321                         "=> bdb_tool_entry_put: %s\n",
322                          text->bv_val, 0, 0 );
323 #endif
324                 return NOID;
325         }
326
327         op.o_bd = be;
328         op.o_tmpmemctx = NULL;
329         op.o_tmpmfuncs = &ch_mfuncs;
330
331         /* add dn2id indices */
332         rc = bdb_tool_next_id( &op, tid, e, text, 0 );
333         if( rc != 0 ) {
334                 goto done;
335         }
336
337         /* id2entry index */
338         rc = bdb_id2entry_add( be, tid, e );
339         if( rc != 0 ) {
340                 snprintf( text->bv_val, text->bv_len,
341                                 "id2entry_add failed: %s (%d)",
342                                 db_strerror(rc), rc );
343 #ifdef NEW_LOGGING
344                 LDAP_LOG ( TOOLS, ERR, 
345                         "=> bdb_tool_entry_put: %s\n", text->bv_val, 0, 0 );
346 #else
347                 Debug( LDAP_DEBUG_ANY,
348                         "=> bdb_tool_entry_put: %s\n", text->bv_val, 0, 0 );
349 #endif
350                 goto done;
351         }
352
353         rc = bdb_index_entry_add( &op, tid, e );
354         if( rc != 0 ) {
355                 snprintf( text->bv_val, text->bv_len,
356                                 "index_entry_add failed: %s (%d)",
357                                 db_strerror(rc), rc );
358 #ifdef NEW_LOGGING
359                 LDAP_LOG ( TOOLS, ERR, 
360                         "=> bdb_tool_entry_put: %s\n", text->bv_val, 0, 0 );
361 #else
362                 Debug( LDAP_DEBUG_ANY,
363                         "=> bdb_tool_entry_put: %s\n", text->bv_val, 0, 0 );
364 #endif
365                 goto done;
366         }
367
368 done:
369         if( rc == 0 ) {
370                 rc = TXN_COMMIT( tid, 0 );
371                 if( rc != 0 ) {
372                         snprintf( text->bv_val, text->bv_len,
373                                         "txn_commit failed: %s (%d)",
374                                         db_strerror(rc), rc );
375 #ifdef NEW_LOGGING
376                         LDAP_LOG ( TOOLS, ERR, 
377                                 "=> bdb_tool_entry_put: %s\n", text->bv_val, 0, 0 );
378 #else
379                         Debug( LDAP_DEBUG_ANY,
380                                 "=> bdb_tool_entry_put: %s\n",
381                                 text->bv_val, 0, 0 );
382 #endif
383                         e->e_id = NOID;
384                 }
385
386         } else {
387                 TXN_ABORT( tid );
388                 snprintf( text->bv_val, text->bv_len,
389                         "txn_aborted! %s (%d)",
390                         db_strerror(rc), rc );
391 #ifdef NEW_LOGGING
392                 LDAP_LOG ( TOOLS, ERR, 
393                         "=> bdb_tool_entry_put: %s\n", text->bv_val, 0, 0 );
394 #else
395                 Debug( LDAP_DEBUG_ANY,
396                         "=> bdb_tool_entry_put: %s\n",
397                         text->bv_val, 0, 0 );
398 #endif
399                 e->e_id = NOID;
400         }
401
402         return e->e_id;
403 }
404
405 int bdb_tool_entry_reindex(
406         BackendDB *be,
407         ID id )
408 {
409         struct bdb_info *bi = (struct bdb_info *) be->be_private;
410         int rc;
411         Entry *e;
412         DB_TXN *tid = NULL;
413         Operation op = {0};
414
415 #ifdef NEW_LOGGING
416         LDAP_LOG ( TOOLS, ARGS, 
417                 "=> bdb_tool_entry_reindex( %ld )\n", (long) id, 0, 0 );
418 #else
419         Debug( LDAP_DEBUG_ARGS, "=> bdb_tool_entry_reindex( %ld )\n",
420                 (long) id, 0, 0 );
421 #endif
422
423         e = bdb_tool_entry_get( be, id );
424
425         if( e == NULL ) {
426 #ifdef NEW_LOGGING
427                 LDAP_LOG ( TOOLS, DETAIL1, 
428                         "bdb_tool_entry_reindex:: could not locate id=%ld\n", 
429                         (long) id, 0, 0 );
430 #else
431                 Debug( LDAP_DEBUG_ANY,
432                         "bdb_tool_entry_reindex:: could not locate id=%ld\n",
433                         (long) id, 0, 0 );
434 #endif
435                 return -1;
436         }
437
438         rc = TXN_BEGIN( bi->bi_dbenv, NULL, &tid, bi->bi_db_opflags );
439         if( rc != 0 ) {
440 #ifdef NEW_LOGGING
441                 LDAP_LOG ( TOOLS, ERR, 
442                         "=> bdb_tool_entry_reindex: txn_begin failed: %s (%d)\n", 
443                         db_strerror(rc), rc, 0 );
444 #else
445                 Debug( LDAP_DEBUG_ANY,
446                         "=> bdb_tool_entry_reindex: txn_begin failed: %s (%d)\n",
447                         db_strerror(rc), rc, 0 );
448 #endif
449                 goto done;
450         }
451         
452         /*
453          * just (re)add them for now
454          * assume that some other routine (not yet implemented)
455          * will zap index databases
456          *
457          */
458
459 #ifdef NEW_LOGGING
460         LDAP_LOG ( TOOLS, ERR, 
461                 "=> bdb_tool_entry_reindex( %ld, \"%s\" )\n", (long) id, e->e_dn, 0 );
462 #else
463         Debug( LDAP_DEBUG_TRACE, "=> bdb_tool_entry_reindex( %ld, \"%s\" )\n",
464                 (long) id, e->e_dn, 0 );
465 #endif
466
467         op.o_bd = be;
468         op.o_tmpmemctx = NULL;
469         op.o_tmpmfuncs = &ch_mfuncs;
470
471 #ifndef BDB_HIER
472         /* add dn2id indices */
473         rc = bdb_dn2id_add( &op, tid, NULL, e );
474         if( rc != 0 && rc != DB_KEYEXIST ) {
475 #ifdef NEW_LOGGING
476                 LDAP_LOG ( TOOLS, ERR, 
477                         "=> bdb_tool_entry_reindex: dn2id_add failed: %s (%d)\n", 
478                         db_strerror(rc), rc, 0 );
479 #else
480                 Debug( LDAP_DEBUG_ANY,
481                         "=> bdb_tool_entry_reindex: dn2id_add failed: %s (%d)\n",
482                         db_strerror(rc), rc, 0 );
483 #endif
484                 goto done;
485         }
486 #endif
487
488         rc = bdb_index_entry_add( &op, tid, e );
489
490 done:
491         if( rc == 0 ) {
492                 rc = TXN_COMMIT( tid, 0 );
493                 if( rc != 0 ) {
494 #ifdef NEW_LOGGING
495                         LDAP_LOG ( TOOLS, ERR, 
496                                 "=> bdb_tool_entry_reindex: txn_commit failed: %s (%d)\n", 
497                                 db_strerror(rc), rc, 0 );
498 #else
499                         Debug( LDAP_DEBUG_ANY,
500                                 "=> bdb_tool_entry_reindex: txn_commit failed: %s (%d)\n",
501                                 db_strerror(rc), rc, 0 );
502 #endif
503                         e->e_id = NOID;
504                 }
505
506         } else {
507                 TXN_ABORT( tid );
508 #ifdef NEW_LOGGING
509                 LDAP_LOG ( TOOLS, DETAIL1, 
510                         "=> bdb_tool_entry_reindex: txn_aborted! %s (%d)\n", 
511                         db_strerror(rc), rc, 0 );
512 #else
513                 Debug( LDAP_DEBUG_ANY,
514                         "=> bdb_tool_entry_reindex: txn_aborted! %s (%d)\n",
515                         db_strerror(rc), rc, 0 );
516 #endif
517                 e->e_id = NOID;
518         }
519         bdb_entry_release( &op, e, 0 );
520
521         return rc;
522 }
523
524 ID bdb_tool_entry_modify(
525         BackendDB *be,
526         Entry *e,
527         struct berval *text )
528 {
529         int rc;
530         struct bdb_info *bdb = (struct bdb_info *) be->be_private;
531         DB_TXN *tid = NULL;
532         Operation op = {0};
533
534         assert( be != NULL );
535         assert( slapMode & SLAP_TOOL_MODE );
536
537         assert( text );
538         assert( text->bv_val );
539         assert( text->bv_val[0] == '\0' );      /* overconservative? */
540
541         assert ( e->e_id != NOID );
542         assert ( e->e_id != 0 );
543
544 #ifdef NEW_LOGGING
545         LDAP_LOG ( TOOLS, ARGS, "=> bdb_tool_entry_put( %ld, \"%s\" )\n",
546                 (long) e->e_id, e->e_dn, 0 );
547 #else
548         Debug( LDAP_DEBUG_TRACE, "=> bdb_tool_entry_put( %ld, \"%s\" )\n",
549                 (long) e->e_id, e->e_dn, 0 );
550 #endif
551
552         rc = TXN_BEGIN( bdb->bi_dbenv, NULL, &tid, 
553                 bdb->bi_db_opflags );
554         if( rc != 0 ) {
555                 snprintf( text->bv_val, text->bv_len,
556                         "txn_begin failed: %s (%d)",
557                         db_strerror(rc), rc );
558 #ifdef NEW_LOGGING
559         LDAP_LOG ( TOOLS, ERR, "=> bdb_tool_entry_put: %s\n", text->bv_val, 0, 0 );
560 #else
561                 Debug( LDAP_DEBUG_ANY,
562                         "=> bdb_tool_entry_put: %s\n",
563                          text->bv_val, 0, 0 );
564 #endif
565                 return NOID;
566         }
567
568         op.o_bd = be;
569         op.o_tmpmemctx = NULL;
570         op.o_tmpmfuncs = &ch_mfuncs;
571
572         /* id2entry index */
573         rc = bdb_id2entry_update( be, tid, e );
574         if( rc != 0 ) {
575                 snprintf( text->bv_val, text->bv_len,
576                                 "id2entry_add failed: %s (%d)",
577                                 db_strerror(rc), rc );
578 #ifdef NEW_LOGGING
579                 LDAP_LOG ( TOOLS, ERR, 
580                         "=> bdb_tool_entry_put: %s\n", text->bv_val, 0, 0 );
581 #else
582                 Debug( LDAP_DEBUG_ANY,
583                         "=> bdb_tool_entry_put: %s\n", text->bv_val, 0, 0 );
584 #endif
585                 goto done;
586         }
587
588         rc = bdb_index_entry_del( &op, tid, e );
589         if( rc != 0 ) {
590                 snprintf( text->bv_val, text->bv_len,
591                                 "index_entry_del failed: %s (%d)",
592                                 db_strerror(rc), rc );
593 #ifdef NEW_LOGGING
594                 LDAP_LOG ( TOOLS, ERR, 
595                         "=> bdb_tool_entry_put: %s\n", text->bv_val, 0, 0 );
596 #else
597                 Debug( LDAP_DEBUG_ANY,
598                         "=> bdb_tool_entry_put: %s\n", text->bv_val, 0, 0 );
599 #endif
600                 goto done;
601         }
602
603         rc = bdb_index_entry_add( &op, tid, e );
604         if( rc != 0 ) {
605                 snprintf( text->bv_val, text->bv_len,
606                                 "index_entry_add failed: %s (%d)",
607                                 db_strerror(rc), rc );
608 #ifdef NEW_LOGGING
609                 LDAP_LOG ( TOOLS, ERR, 
610                         "=> bdb_tool_entry_put: %s\n", text->bv_val, 0, 0 );
611 #else
612                 Debug( LDAP_DEBUG_ANY,
613                         "=> bdb_tool_entry_put: %s\n", text->bv_val, 0, 0 );
614 #endif
615                 goto done;
616         }
617
618 done:
619         if( rc == 0 ) {
620                 rc = TXN_COMMIT( tid, 0 );
621                 if( rc != 0 ) {
622                         snprintf( text->bv_val, text->bv_len,
623                                         "txn_commit failed: %s (%d)",
624                                         db_strerror(rc), rc );
625 #ifdef NEW_LOGGING
626                         LDAP_LOG ( TOOLS, ERR, 
627                                 "=> bdb_tool_entry_put: %s\n", text->bv_val, 0, 0 );
628 #else
629                         Debug( LDAP_DEBUG_ANY,
630                                 "=> bdb_tool_entry_put: %s\n",
631                                 text->bv_val, 0, 0 );
632 #endif
633                         e->e_id = NOID;
634                 }
635
636         } else {
637                 TXN_ABORT( tid );
638                 snprintf( text->bv_val, text->bv_len,
639                         "txn_aborted! %s (%d)",
640                         db_strerror(rc), rc );
641 #ifdef NEW_LOGGING
642                 LDAP_LOG ( TOOLS, ERR, 
643                         "=> bdb_tool_entry_put: %s\n", text->bv_val, 0, 0 );
644 #else
645                 Debug( LDAP_DEBUG_ANY,
646                         "=> bdb_tool_entry_put: %s\n",
647                         text->bv_val, 0, 0 );
648 #endif
649                 e->e_id = NOID;
650         }
651
652         return e->e_id;
653 }