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