]> git.sur5r.net Git - openldap/blob - servers/slapd/back-bdb/tools.c
530b0f3dee32771cd8e0fd15736a40b299ab81e0
[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 berval dn = e->e_name;
216         struct berval ndn = e->e_nname;
217         struct berval pdn, npdn;
218         EntryInfo *ei = NULL, eidummy;
219         int rc;
220
221         if (ndn.bv_len == 0) return 0;
222
223         rc = bdb_cache_find_ndn( op, tid, &ndn, &ei );
224         if ( ei ) bdb_cache_entryinfo_unlock( ei );
225         if ( rc == DB_NOTFOUND ) {
226                 if ( !be_issuffix( op->o_bd, &ndn ) ) {
227                         ID eid = e->e_id;
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                         /* If parent didn't exist, it was created just now
239                          * and its ID is now in e->e_id. Make sure the current
240                          * entry gets added under the new parent ID.
241                          */
242                         if ( eid != e->e_id ) {
243                                 eidummy.bei_id = e->e_id;
244                                 ei = &eidummy;
245                         }
246                 }
247                 rc = bdb_next_id( op->o_bd, tid, &e->e_id );
248                 if ( rc ) {
249                         snprintf( text->bv_val, text->bv_len,
250                                 "next_id failed: %s (%d)",
251                                 db_strerror(rc), rc );
252                 Debug( LDAP_DEBUG_ANY,
253                         "=> bdb_tool_next_id: %s\n", text->bv_val, 0, 0 );
254                         return rc;
255                 }
256                 rc = bdb_dn2id_add( op, tid, ei, e );
257                 if ( rc ) {
258                         snprintf( text->bv_val, text->bv_len, 
259                                 "dn2id_add failed: %s (%d)",
260                                 db_strerror(rc), rc );
261                 Debug( LDAP_DEBUG_ANY,
262                         "=> bdb_tool_next_id: %s\n", text->bv_val, 0, 0 );
263                 } else if ( hole ) {
264                         if ( nholes == nhmax - 1 ) {
265                                 if ( holes == hbuf ) {
266                                         holes = ch_malloc( nhmax * sizeof(dn_id) * 2 );
267                                         AC_MEMCPY( holes, hbuf, sizeof(hbuf) );
268                                 } else {
269                                         holes = ch_realloc( holes, nhmax * sizeof(dn_id) * 2 );
270                                 }
271                                 nhmax *= 2;
272                         }
273                         ber_dupbv( &holes[nholes].dn, &ndn );
274                         holes[nholes++].id = e->e_id;
275                 }
276         } else if ( !hole ) {
277                 unsigned i;
278
279                 e->e_id = ei->bei_id;
280
281                 for ( i=0; i<nholes; i++) {
282                         if ( holes[i].id == e->e_id ) {
283                                 int j;
284                                 free(holes[i].dn.bv_val);
285                                 for (j=i;j<nholes;j++) holes[j] = holes[j+1];
286                                 holes[j].id = 0;
287                                 nholes--;
288                                 break;
289                         } else if ( holes[i].id > e->e_id ) {
290                                 break;
291                         }
292                 }
293         }
294         return rc;
295 }
296
297 ID bdb_tool_entry_put(
298         BackendDB *be,
299         Entry *e,
300         struct berval *text )
301 {
302         int rc;
303         struct bdb_info *bdb = (struct bdb_info *) be->be_private;
304         DB_TXN *tid = NULL;
305         Operation op = {0};
306         Opheader ohdr = {0};
307
308         assert( be != NULL );
309         assert( slapMode & SLAP_TOOL_MODE );
310
311         assert( text );
312         assert( text->bv_val );
313         assert( text->bv_val[0] == '\0' );      /* overconservative? */
314
315         Debug( LDAP_DEBUG_TRACE, "=> " LDAP_XSTRING(bdb_tool_entry_put)
316                 "( %ld, \"%s\" )\n", (long) e->e_id, e->e_dn, 0 );
317
318         if (! (slapMode & SLAP_TOOL_QUICK)) {
319         rc = TXN_BEGIN( bdb->bi_dbenv, NULL, &tid, 
320                 bdb->bi_db_opflags );
321         if( rc != 0 ) {
322                 snprintf( text->bv_val, text->bv_len,
323                         "txn_begin failed: %s (%d)",
324                         db_strerror(rc), rc );
325                 Debug( LDAP_DEBUG_ANY,
326                         "=> " LDAP_XSTRING(bdb_tool_entry_put) ": %s\n",
327                          text->bv_val, 0, 0 );
328                 return NOID;
329         }
330         }
331
332         op.o_hdr = &ohdr;
333         op.o_bd = be;
334         op.o_tmpmemctx = NULL;
335         op.o_tmpmfuncs = &ch_mfuncs;
336
337         /* add dn2id indices */
338         rc = bdb_tool_next_id( &op, tid, e, text, 0 );
339         if( rc != 0 ) {
340                 goto done;
341         }
342
343         /* id2entry index */
344         rc = bdb_id2entry_add( be, tid, e );
345         if( rc != 0 ) {
346                 snprintf( text->bv_val, text->bv_len,
347                                 "id2entry_add failed: %s (%d)",
348                                 db_strerror(rc), rc );
349                 Debug( LDAP_DEBUG_ANY,
350                         "=> " LDAP_XSTRING(bdb_tool_entry_put) ": %s\n",
351                         text->bv_val, 0, 0 );
352                 goto done;
353         }
354
355         if ( !bdb->bi_linear_index )
356                 rc = bdb_index_entry_add( &op, tid, e );
357         if( rc != 0 ) {
358                 snprintf( text->bv_val, text->bv_len,
359                                 "index_entry_add failed: %s (%d)",
360                                 db_strerror(rc), rc );
361                 Debug( LDAP_DEBUG_ANY,
362                         "=> " LDAP_XSTRING(bdb_tool_entry_put) ": %s\n",
363                         text->bv_val, 0, 0 );
364                 goto done;
365         }
366
367 done:
368         if( rc == 0 ) {
369                 if ( !( slapMode & SLAP_TOOL_QUICK )) {
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                         Debug( LDAP_DEBUG_ANY,
376                                 "=> " LDAP_XSTRING(bdb_tool_entry_put) ": %s\n",
377                                 text->bv_val, 0, 0 );
378                         e->e_id = NOID;
379                 }
380                 }
381
382         } else {
383                 if ( !( slapMode & SLAP_TOOL_QUICK )) {
384                 TXN_ABORT( tid );
385                 snprintf( text->bv_val, text->bv_len,
386                         "txn_aborted! %s (%d)",
387                         db_strerror(rc), rc );
388                 Debug( LDAP_DEBUG_ANY,
389                         "=> " LDAP_XSTRING(bdb_tool_entry_put) ": %s\n",
390                         text->bv_val, 0, 0 );
391                 }
392                 e->e_id = NOID;
393         }
394
395         return e->e_id;
396 }
397
398 int bdb_tool_entry_reindex(
399         BackendDB *be,
400         ID id )
401 {
402         struct bdb_info *bi = (struct bdb_info *) be->be_private;
403         int rc;
404         Entry *e;
405         DB_TXN *tid = NULL;
406         Operation op = {0};
407         Opheader ohdr = {0};
408
409         Debug( LDAP_DEBUG_ARGS,
410                 "=> " LDAP_XSTRING(bdb_tool_entry_reindex) "( %ld )\n",
411                 (long) id, 0, 0 );
412
413         /* No indexes configured, nothing to do. Could return an
414          * error here to shortcut things.
415          */
416         if (!bi->bi_attrs) {
417                 return 0;
418         }
419
420         /* Get the first attribute to index */
421         if (bi->bi_linear_index && !index_attrs && bi->bi_attrs != &index_dummy) {
422                 index_attrs = bi->bi_attrs;
423                 bi->bi_attrs = &index_dummy;
424                 index_dummy.avl_data = avl_delete(&index_attrs, NULL, bdb_reindex_cmp);
425         }
426
427         e = bdb_tool_entry_get( be, id );
428
429         if( e == NULL ) {
430                 Debug( LDAP_DEBUG_ANY,
431                         LDAP_XSTRING(bdb_tool_entry_reindex)
432                         ": could not locate id=%ld\n",
433                         (long) id, 0, 0 );
434                 return -1;
435         }
436
437         if (! (slapMode & SLAP_TOOL_QUICK)) {
438         rc = TXN_BEGIN( bi->bi_dbenv, NULL, &tid, bi->bi_db_opflags );
439         if( rc != 0 ) {
440                 Debug( LDAP_DEBUG_ANY,
441                         "=> " LDAP_XSTRING(bdb_tool_entry_reindex) ": "
442                         "txn_begin failed: %s (%d)\n",
443                         db_strerror(rc), rc, 0 );
444                 goto done;
445         }
446         }
447         
448         /*
449          * just (re)add them for now
450          * assume that some other routine (not yet implemented)
451          * will zap index databases
452          *
453          */
454
455         Debug( LDAP_DEBUG_TRACE,
456                 "=> " LDAP_XSTRING(bdb_tool_entry_reindex) "( %ld, \"%s\" )\n",
457                 (long) id, e->e_dn, 0 );
458
459         op.o_hdr = &ohdr;
460         op.o_bd = be;
461         op.o_tmpmemctx = NULL;
462         op.o_tmpmfuncs = &ch_mfuncs;
463
464         rc = bdb_index_entry_add( &op, tid, e );
465
466 done:
467         if( rc == 0 ) {
468                 if (! (slapMode & SLAP_TOOL_QUICK)) {
469                 rc = TXN_COMMIT( tid, 0 );
470                 if( rc != 0 ) {
471                         Debug( LDAP_DEBUG_ANY,
472                                 "=> " LDAP_XSTRING(bdb_tool_entry_reindex)
473                                 ": txn_commit failed: %s (%d)\n",
474                                 db_strerror(rc), rc, 0 );
475                         e->e_id = NOID;
476                 }
477                 }
478
479         } else {
480                 if (! (slapMode & SLAP_TOOL_QUICK)) {
481                 TXN_ABORT( tid );
482                 Debug( LDAP_DEBUG_ANY,
483                         "=> " LDAP_XSTRING(bdb_tool_entry_reindex)
484                         ": txn_aborted! %s (%d)\n",
485                         db_strerror(rc), rc, 0 );
486                 }
487                 e->e_id = NOID;
488         }
489         bdb_entry_release( &op, e, 0 );
490
491         return rc;
492 }
493
494 ID bdb_tool_entry_modify(
495         BackendDB *be,
496         Entry *e,
497         struct berval *text )
498 {
499         int rc;
500         struct bdb_info *bdb = (struct bdb_info *) be->be_private;
501         DB_TXN *tid = NULL;
502         Operation op = {0};
503         Opheader ohdr = {0};
504
505         assert( be != NULL );
506         assert( slapMode & SLAP_TOOL_MODE );
507
508         assert( text );
509         assert( text->bv_val );
510         assert( text->bv_val[0] == '\0' );      /* overconservative? */
511
512         assert ( e->e_id != NOID );
513         assert ( e->e_id != 0 );
514
515         Debug( LDAP_DEBUG_TRACE,
516                 "=> " LDAP_XSTRING(bdb_tool_entry_modify) "( %ld, \"%s\" )\n",
517                 (long) e->e_id, e->e_dn, 0 );
518
519         if (! (slapMode & SLAP_TOOL_QUICK)) {
520         rc = TXN_BEGIN( bdb->bi_dbenv, NULL, &tid, 
521                 bdb->bi_db_opflags );
522         if( rc != 0 ) {
523                 snprintf( text->bv_val, text->bv_len,
524                         "txn_begin failed: %s (%d)",
525                         db_strerror(rc), rc );
526                 Debug( LDAP_DEBUG_ANY,
527                         "=> " LDAP_XSTRING(bdb_tool_entry_modify) ": %s\n",
528                          text->bv_val, 0, 0 );
529                 return NOID;
530         }
531         }
532
533         op.o_hdr = &ohdr;
534         op.o_bd = be;
535         op.o_tmpmemctx = NULL;
536         op.o_tmpmfuncs = &ch_mfuncs;
537
538         /* id2entry index */
539         rc = bdb_id2entry_update( be, tid, e );
540         if( rc != 0 ) {
541                 snprintf( text->bv_val, text->bv_len,
542                                 "id2entry_add 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                 goto done;
548         }
549
550         rc = bdb_index_entry_del( &op, tid, e );
551         if( rc != 0 ) {
552                 snprintf( text->bv_val, text->bv_len,
553                                 "index_entry_del failed: %s (%d)",
554                                 db_strerror(rc), rc );
555                 Debug( LDAP_DEBUG_ANY,
556                         "=> " LDAP_XSTRING(bdb_tool_entry_modify) ": %s\n",
557                         text->bv_val, 0, 0 );
558                 goto done;
559         }
560
561         rc = bdb_index_entry_add( &op, tid, e );
562         if( rc != 0 ) {
563                 snprintf( text->bv_val, text->bv_len,
564                                 "index_entry_add failed: %s (%d)",
565                                 db_strerror(rc), rc );
566                 Debug( LDAP_DEBUG_ANY,
567                         "=> " LDAP_XSTRING(bdb_tool_entry_modify) ": %s\n",
568                         text->bv_val, 0, 0 );
569                 goto done;
570         }
571
572 done:
573         if( rc == 0 ) {
574                 if (! (slapMode & SLAP_TOOL_QUICK)) {
575                 rc = TXN_COMMIT( tid, 0 );
576                 if( rc != 0 ) {
577                         snprintf( text->bv_val, text->bv_len,
578                                         "txn_commit failed: %s (%d)",
579                                         db_strerror(rc), rc );
580                         Debug( LDAP_DEBUG_ANY,
581                                 "=> " LDAP_XSTRING(bdb_tool_entry_modify) ": "
582                                 "%s\n", text->bv_val, 0, 0 );
583                         e->e_id = NOID;
584                 }
585                 }
586
587         } else {
588                 if (! (slapMode & SLAP_TOOL_QUICK)) {
589                 TXN_ABORT( tid );
590                 snprintf( text->bv_val, text->bv_len,
591                         "txn_aborted! %s (%d)",
592                         db_strerror(rc), rc );
593                 Debug( LDAP_DEBUG_ANY,
594                         "=> " LDAP_XSTRING(bdb_tool_entry_modify) ": %s\n",
595                         text->bv_val, 0, 0 );
596                 }
597                 e->e_id = NOID;
598         }
599
600         return e->e_id;
601 }