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