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