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