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