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