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