]> git.sur5r.net Git - openldap/blob - servers/slapd/back-bdb/dn2id.c
fix previous commit (ITS#5819)
[openldap] / servers / slapd / back-bdb / dn2id.c
1 /* dn2id.c - routines to deal with the dn2id index */
2 /* $OpenLDAP$ */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4  *
5  * Copyright 2000-2008 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 #include "back-bdb.h"
23 #include "idl.h"
24 #include "lutil.h"
25
26 #define bdb_dn2id_lock                                  BDB_SYMBOL(dn2id_lock)
27
28 static int
29 bdb_dn2id_lock( struct bdb_info *bdb, struct berval *dn,
30         int rw, DB_TXN *txn, DB_LOCK *lock )
31 {
32         int       rc;
33         DBT       lockobj;
34         int       db_rw;
35
36         if (!txn)
37                 return 0;
38
39         if (rw)
40                 db_rw = DB_LOCK_WRITE;
41         else
42                 db_rw = DB_LOCK_READ;
43
44         lockobj.data = dn->bv_val;
45         lockobj.size = dn->bv_len;
46
47         rc = LOCK_GET(bdb->bi_dbenv, TXN_ID(txn), DB_LOCK_NOWAIT,
48                                         &lockobj, db_rw, lock);
49         return rc;
50 }
51
52 #ifndef BDB_HIER
53 int
54 bdb_dn2id_add(
55         Operation *op,
56         DB_TXN *txn,
57         EntryInfo *eip,
58         Entry           *e )
59 {
60         struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
61         DB *db = bdb->bi_dn2id->bdi_db;
62         int             rc;
63         DBT             key, data;
64         ID              nid;
65         char            *buf;
66         struct berval   ptr, pdn;
67
68         Debug( LDAP_DEBUG_TRACE, "=> bdb_dn2id_add 0x%lx: \"%s\"\n",
69                 e->e_id, e->e_ndn, 0 );
70         assert( e->e_id != NOID );
71
72         DBTzero( &key );
73         key.size = e->e_nname.bv_len + 2;
74         key.ulen = key.size;
75         key.flags = DB_DBT_USERMEM;
76         buf = op->o_tmpalloc( key.size, op->o_tmpmemctx );
77         key.data = buf;
78         buf[0] = DN_BASE_PREFIX;
79         ptr.bv_val = buf + 1;
80         ptr.bv_len = e->e_nname.bv_len;
81         AC_MEMCPY( ptr.bv_val, e->e_nname.bv_val, e->e_nname.bv_len );
82         ptr.bv_val[ptr.bv_len] = '\0';
83
84         DBTzero( &data );
85         data.data = &nid;
86         data.size = sizeof( nid );
87         BDB_ID2DISK( e->e_id, &nid );
88
89         /* store it -- don't override */
90         rc = db->put( db, txn, &key, &data, DB_NOOVERWRITE );
91         if( rc != 0 ) {
92                 Debug( LDAP_DEBUG_ANY, "=> bdb_dn2id_add 0x%lx: put failed: %s %d\n",
93                         e->e_id, db_strerror(rc), rc );
94                 goto done;
95         }
96
97 #ifndef BDB_MULTIPLE_SUFFIXES
98         if( !be_issuffix( op->o_bd, &ptr ))
99 #endif
100         {
101                 buf[0] = DN_SUBTREE_PREFIX;
102                 rc = db->put( db, txn, &key, &data, DB_NOOVERWRITE );
103                 if( rc != 0 ) {
104                         Debug( LDAP_DEBUG_ANY,
105                         "=> bdb_dn2id_add 0x%lx: subtree (%s) put failed: %d\n",
106                         e->e_id, ptr.bv_val, rc );
107                         goto done;
108                 }
109                 
110 #ifdef BDB_MULTIPLE_SUFFIXES
111         if( !be_issuffix( op->o_bd, &ptr ))
112 #endif
113         {
114                 dnParent( &ptr, &pdn );
115         
116                 key.size = pdn.bv_len + 2;
117                 key.ulen = key.size;
118                 pdn.bv_val[-1] = DN_ONE_PREFIX;
119                 key.data = pdn.bv_val-1;
120                 ptr = pdn;
121
122                 rc = bdb_idl_insert_key( op->o_bd, db, txn, &key, e->e_id );
123
124                 if( rc != 0 ) {
125                         Debug( LDAP_DEBUG_ANY,
126                                 "=> bdb_dn2id_add 0x%lx: parent (%s) insert failed: %d\n",
127                                         e->e_id, ptr.bv_val, rc );
128                         goto done;
129                 }
130         }
131
132 #ifndef BDB_MULTIPLE_SUFFIXES
133         while( !be_issuffix( op->o_bd, &ptr ))
134 #else
135         for (;;)
136 #endif
137         {
138                 ptr.bv_val[-1] = DN_SUBTREE_PREFIX;
139
140                 rc = bdb_idl_insert_key( op->o_bd, db, txn, &key, e->e_id );
141
142                 if( rc != 0 ) {
143                         Debug( LDAP_DEBUG_ANY,
144                                 "=> bdb_dn2id_add 0x%lx: subtree (%s) insert failed: %d\n",
145                                         e->e_id, ptr.bv_val, rc );
146                         break;
147                 }
148 #ifdef BDB_MULTIPLE_SUFFIXES
149                 if( be_issuffix( op->o_bd, &ptr )) break;
150 #endif
151                 dnParent( &ptr, &pdn );
152
153                 key.size = pdn.bv_len + 2;
154                 key.ulen = key.size;
155                 key.data = pdn.bv_val - 1;
156                 ptr = pdn;
157         }
158         }
159
160 done:
161         op->o_tmpfree( buf, op->o_tmpmemctx );
162         Debug( LDAP_DEBUG_TRACE, "<= bdb_dn2id_add 0x%lx: %d\n", e->e_id, rc, 0 );
163         return rc;
164 }
165
166 int
167 bdb_dn2id_delete(
168         Operation *op,
169         DB_TXN *txn,
170         EntryInfo       *eip,
171         Entry           *e )
172 {
173         struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
174         DB *db = bdb->bi_dn2id->bdi_db;
175         char            *buf;
176         DBT             key;
177         DB_LOCK lock;
178         struct berval   pdn, ptr;
179         int             rc;
180
181         Debug( LDAP_DEBUG_TRACE, "=> bdb_dn2id_delete 0x%lx: \"%s\"\n",
182                 e->e_id, e->e_ndn, 0 );
183
184         DBTzero( &key );
185         key.size = e->e_nname.bv_len + 2;
186         buf = op->o_tmpalloc( key.size, op->o_tmpmemctx );
187         key.data = buf;
188         key.flags = DB_DBT_USERMEM;
189         buf[0] = DN_BASE_PREFIX;
190         ptr.bv_val = buf+1;
191         ptr.bv_len = e->e_nname.bv_len;
192         AC_MEMCPY( ptr.bv_val, e->e_nname.bv_val, e->e_nname.bv_len );
193         ptr.bv_val[ptr.bv_len] = '\0';
194
195         /* We hold this lock until the TXN completes */
196         rc = bdb_dn2id_lock( bdb, &e->e_nname, 1, txn, &lock );
197         if ( rc ) goto done;
198
199         /* delete it */
200         rc = db->del( db, txn, &key, 0 );
201         if( rc != 0 ) {
202                 Debug( LDAP_DEBUG_ANY, "=> bdb_dn2id_delete 0x%lx: delete failed: %s %d\n",
203                         e->e_id, db_strerror(rc), rc );
204                 goto done;
205         }
206
207 #ifndef BDB_MULTIPLE_SUFFIXES
208         if( !be_issuffix( op->o_bd, &ptr ))
209 #endif
210         {
211                 buf[0] = DN_SUBTREE_PREFIX;
212                 rc = bdb_idl_delete_key( op->o_bd, db, txn, &key, e->e_id );
213                 if( rc != 0 ) {
214                         Debug( LDAP_DEBUG_ANY,
215                         "=> bdb_dn2id_delete 0x%lx: subtree (%s) delete failed: %d\n",
216                         e->e_id, ptr.bv_val, rc );
217                         goto done;
218                 }
219
220 #ifdef BDB_MULTIPLE_SUFFIXES
221         if( !be_issuffix( op->o_bd, &ptr ))
222 #endif
223         {
224                 dnParent( &ptr, &pdn );
225
226                 key.size = pdn.bv_len + 2;
227                 key.ulen = key.size;
228                 pdn.bv_val[-1] = DN_ONE_PREFIX;
229                 key.data = pdn.bv_val - 1;
230                 ptr = pdn;
231
232                 rc = bdb_idl_delete_key( op->o_bd, db, txn, &key, e->e_id );
233
234                 if( rc != 0 ) {
235                         Debug( LDAP_DEBUG_ANY,
236                                 "=> bdb_dn2id_delete 0x%lx: parent (%s) delete failed: %d\n",
237                                 e->e_id, ptr.bv_val, rc );
238                         goto done;
239                 }
240         }
241
242 #ifndef BDB_MULTIPLE_SUFFIXES
243         while( !be_issuffix( op->o_bd, &ptr ))
244 #else
245         for (;;)
246 #endif
247         {
248                 ptr.bv_val[-1] = DN_SUBTREE_PREFIX;
249
250                 rc = bdb_idl_delete_key( op->o_bd, db, txn, &key, e->e_id );
251                 if( rc != 0 ) {
252                         Debug( LDAP_DEBUG_ANY,
253                                 "=> bdb_dn2id_delete 0x%lx: subtree (%s) delete failed: %d\n",
254                                 e->e_id, ptr.bv_val, rc );
255                         goto done;
256                 }
257 #ifdef BDB_MULTIPLE_SUFFIXES
258                 if( be_issuffix( op->o_bd, &ptr )) break;
259 #endif
260                 dnParent( &ptr, &pdn );
261
262                 key.size = pdn.bv_len + 2;
263                 key.ulen = key.size;
264                 key.data = pdn.bv_val - 1;
265                 ptr = pdn;
266         }
267         }
268
269 done:
270         op->o_tmpfree( buf, op->o_tmpmemctx );
271         Debug( LDAP_DEBUG_TRACE, "<= bdb_dn2id_delete 0x%lx: %d\n", e->e_id, rc, 0 );
272         return rc;
273 }
274
275 int
276 bdb_dn2id(
277         Operation *op,
278         struct berval   *dn,
279         EntryInfo *ei,
280         DB_TXN *txn,
281         DB_LOCK *lock )
282 {
283         struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
284         DB *db = bdb->bi_dn2id->bdi_db;
285         DBC     *cursor;
286         int             rc;
287         DBT             key, data;
288         ID              nid;
289
290         Debug( LDAP_DEBUG_TRACE, "=> bdb_dn2id(\"%s\")\n", dn->bv_val, 0, 0 );
291
292         DBTzero( &key );
293         key.size = dn->bv_len + 2;
294         key.data = op->o_tmpalloc( key.size, op->o_tmpmemctx );
295         ((char *)key.data)[0] = DN_BASE_PREFIX;
296         AC_MEMCPY( &((char *)key.data)[1], dn->bv_val, key.size - 1 );
297
298         /* store the ID */
299         DBTzero( &data );
300         data.data = &nid;
301         data.ulen = sizeof(ID);
302         data.flags = DB_DBT_USERMEM;
303
304         rc = db->cursor( db, txn, &cursor, bdb->bi_db_opflags );
305         if ( rc ) goto func_leave;
306
307         rc = bdb_dn2id_lock( bdb, dn, 0, txn, lock );
308         if ( rc ) goto nolock;
309
310         /* fetch it */
311         rc = cursor->c_get( cursor, &key, &data, DB_SET );
312
313 nolock:
314         cursor->c_close( cursor );
315 func_leave:
316
317         if( rc != 0 ) {
318                 Debug( LDAP_DEBUG_TRACE, "<= bdb_dn2id: get failed: %s (%d)\n",
319                         db_strerror( rc ), rc, 0 );
320         } else {
321                 BDB_DISK2ID( &nid, &ei->bei_id );
322                 Debug( LDAP_DEBUG_TRACE, "<= bdb_dn2id: got id=0x%lx\n",
323                         ei->bei_id, 0, 0 );
324         }
325         op->o_tmpfree( key.data, op->o_tmpmemctx );
326         return rc;
327 }
328
329 int
330 bdb_dn2id_children(
331         Operation *op,
332         DB_TXN *txn,
333         Entry *e )
334 {
335         DBT             key, data;
336         struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
337         DB *db = bdb->bi_dn2id->bdi_db;
338         ID              id;
339         int             rc;
340
341         Debug( LDAP_DEBUG_TRACE, "=> bdb_dn2id_children(\"%s\")\n",
342                 e->e_nname.bv_val, 0, 0 );
343         DBTzero( &key );
344         key.size = e->e_nname.bv_len + 2;
345         key.data = op->o_tmpalloc( key.size, op->o_tmpmemctx );
346         ((char *)key.data)[0] = DN_ONE_PREFIX;
347         AC_MEMCPY( &((char *)key.data)[1], e->e_nname.bv_val, key.size - 1 );
348
349         if ( bdb->bi_idl_cache_size ) {
350                 rc = bdb_idl_cache_get( bdb, db, &key, NULL );
351                 if ( rc != LDAP_NO_SUCH_OBJECT ) {
352                         op->o_tmpfree( key.data, op->o_tmpmemctx );
353                         return rc;
354                 }
355         }
356         /* we actually could do a empty get... */
357         DBTzero( &data );
358         data.data = &id;
359         data.ulen = sizeof(id);
360         data.flags = DB_DBT_USERMEM;
361         data.doff = 0;
362         data.dlen = sizeof(id);
363
364         rc = db->get( db, txn, &key, &data, bdb->bi_db_opflags );
365         op->o_tmpfree( key.data, op->o_tmpmemctx );
366
367         Debug( LDAP_DEBUG_TRACE, "<= bdb_dn2id_children(\"%s\"): %s (%d)\n",
368                 e->e_nname.bv_val,
369                 rc == 0 ? "" : ( rc == DB_NOTFOUND ? "no " :
370                         db_strerror(rc) ), rc );
371
372         return rc;
373 }
374
375 int
376 bdb_dn2idl(
377         Operation *op,
378         DB_TXN *txn,
379         struct berval *ndn,
380         EntryInfo *ei,
381         ID *ids,
382         ID *stack )
383 {
384         int             rc;
385         DBT             key;
386         struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
387         DB *db = bdb->bi_dn2id->bdi_db;
388         int prefix = ( op->ors_scope == LDAP_SCOPE_ONELEVEL )
389                 ? DN_ONE_PREFIX : DN_SUBTREE_PREFIX;
390
391         Debug( LDAP_DEBUG_TRACE, "=> bdb_dn2idl(\"%s\")\n",
392                 ndn->bv_val, 0, 0 );
393
394 #ifndef BDB_MULTIPLE_SUFFIXES
395         if ( prefix == DN_SUBTREE_PREFIX
396                 && ( ei->bei_id == 0 ||
397                 ( ei->bei_parent->bei_id == 0 && op->o_bd->be_suffix[0].bv_len ))) {
398                 BDB_IDL_ALL(bdb, ids);
399                 return 0;
400         }
401 #endif
402
403         DBTzero( &key );
404         key.size = ndn->bv_len + 2;
405         key.ulen = key.size;
406         key.flags = DB_DBT_USERMEM;
407         key.data = op->o_tmpalloc( key.size, op->o_tmpmemctx );
408         ((char *)key.data)[0] = prefix;
409         AC_MEMCPY( &((char *)key.data)[1], ndn->bv_val, key.size - 1 );
410
411         BDB_IDL_ZERO( ids );
412         rc = bdb_idl_fetch_key( op->o_bd, db, txn, &key, ids, NULL, 0 );
413
414         if( rc != 0 ) {
415                 Debug( LDAP_DEBUG_TRACE,
416                         "<= bdb_dn2idl: get failed: %s (%d)\n",
417                         db_strerror( rc ), rc, 0 );
418
419         } else {
420                 Debug( LDAP_DEBUG_TRACE,
421                         "<= bdb_dn2idl: id=%ld first=%ld last=%ld\n",
422                         (long) ids[0],
423                         (long) BDB_IDL_FIRST( ids ), (long) BDB_IDL_LAST( ids ) );
424         }
425
426         op->o_tmpfree( key.data, op->o_tmpmemctx );
427         return rc;
428 }
429
430 #else   /* BDB_HIER */
431 /* Management routines for a hierarchically structured database.
432  *
433  * Instead of a ldbm-style dn2id database, we use a hierarchical one. Each
434  * entry in this database is a struct diskNode, keyed by entryID and with
435  * the data containing the RDN and entryID of the node's children. We use
436  * a B-Tree with sorted duplicates to store all the children of a node under
437  * the same key. Also, the first item under the key contains the entry's own
438  * rdn and the ID of the node's parent, to allow bottom-up tree traversal as
439  * well as top-down. To keep this info first in the list, the high bit of all
440  * subsequent nrdnlen's is always set. This means we can only accomodate
441  * RDNs up to length 32767, but that's fine since full DNs are already
442  * restricted to 8192.
443  *
444  * The diskNode is a variable length structure. This definition is not
445  * directly usable for in-memory manipulation.
446  */
447 typedef struct diskNode {
448         unsigned char nrdnlen[2];
449         char nrdn[1];
450         char rdn[1];                        /* variable placement */
451         unsigned char entryID[sizeof(ID)];  /* variable placement */
452 } diskNode;
453
454 /* Sort function for the sorted duplicate data items of a dn2id key.
455  * Sorts based on normalized RDN, in length order.
456  */
457 int
458 hdb_dup_compare(
459         DB *db, 
460         const DBT *usrkey,
461         const DBT *curkey
462 )
463 {
464         diskNode *un, *cn;
465         int rc;
466
467         un = (diskNode *)usrkey->data;
468         cn = (diskNode *)curkey->data;
469
470         /* data is not aligned, cannot compare directly */
471         rc = un->nrdnlen[0] - cn->nrdnlen[0];
472         if ( rc ) return rc;
473         rc = un->nrdnlen[1] - cn->nrdnlen[1];
474         if ( rc ) return rc;
475
476         return strcmp( un->nrdn, cn->nrdn );
477 }
478
479 /* This function constructs a full DN for a given entry.
480  */
481 int hdb_fix_dn(
482         Entry *e,
483         int checkit )
484 {
485         EntryInfo *ei;
486         int rlen = 0, nrlen = 0;
487         char *ptr, *nptr;
488         int max = 0;
489
490         if ( !e->e_id )
491                 return 0;
492
493         /* count length of all DN components */
494         for ( ei = BEI(e); ei && ei->bei_id; ei=ei->bei_parent ) {
495                 rlen += ei->bei_rdn.bv_len + 1;
496                 nrlen += ei->bei_nrdn.bv_len + 1;
497                 if (ei->bei_modrdns > max) max = ei->bei_modrdns;
498         }
499
500         /* See if the entry DN was invalidated by a subtree rename */
501         if ( checkit ) {
502                 if ( BEI(e)->bei_modrdns >= max ) {
503                         return 0;
504                 }
505                 /* We found a mismatch, tell the caller to lock it */
506                 if ( checkit == 1 ) {
507                         return 1;
508                 }
509                 /* checkit == 2. do the fix. */
510                 free( e->e_name.bv_val );
511                 free( e->e_nname.bv_val );
512         }
513
514         e->e_name.bv_len = rlen - 1;
515         e->e_nname.bv_len = nrlen - 1;
516         e->e_name.bv_val = ch_malloc(rlen);
517         e->e_nname.bv_val = ch_malloc(nrlen);
518         ptr = e->e_name.bv_val;
519         nptr = e->e_nname.bv_val;
520         for ( ei = BEI(e); ei && ei->bei_id; ei=ei->bei_parent ) {
521                 ptr = lutil_strcopy(ptr, ei->bei_rdn.bv_val);
522                 nptr = lutil_strcopy(nptr, ei->bei_nrdn.bv_val);
523                 if ( ei->bei_parent ) {
524                         *ptr++ = ',';
525                         *nptr++ = ',';
526                 }
527         }
528         BEI(e)->bei_modrdns = max;
529         ptr[-1] = '\0';
530         nptr[-1] = '\0';
531
532         return 0;
533 }
534
535 /* We add two elements to the DN2ID database - a data item under the parent's
536  * entryID containing the child's RDN and entryID, and an item under the
537  * child's entryID containing the parent's entryID.
538  */
539 int
540 hdb_dn2id_add(
541         Operation       *op,
542         DB_TXN *txn,
543         EntryInfo       *eip,
544         Entry           *e )
545 {
546         struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
547         DB *db = bdb->bi_dn2id->bdi_db;
548         DBT             key, data;
549         ID              nid;
550         int             rc, rlen, nrlen;
551         diskNode *d;
552         char *ptr;
553
554         Debug( LDAP_DEBUG_TRACE, "=> hdb_dn2id_add 0x%lx: \"%s\"\n",
555                 e->e_id, e->e_ndn, 0 );
556
557         nrlen = dn_rdnlen( op->o_bd, &e->e_nname );
558         if (nrlen) {
559                 rlen = dn_rdnlen( op->o_bd, &e->e_name );
560         } else {
561                 nrlen = e->e_nname.bv_len;
562                 rlen = e->e_name.bv_len;
563         }
564
565         d = op->o_tmpalloc(sizeof(diskNode) + rlen + nrlen, op->o_tmpmemctx);
566         d->nrdnlen[1] = nrlen & 0xff;
567         d->nrdnlen[0] = (nrlen >> 8) | 0x80;
568         ptr = lutil_strncopy( d->nrdn, e->e_nname.bv_val, nrlen );
569         *ptr++ = '\0';
570         ptr = lutil_strncopy( ptr, e->e_name.bv_val, rlen );
571         *ptr++ = '\0';
572         BDB_ID2DISK( e->e_id, ptr );
573
574         DBTzero(&key);
575         DBTzero(&data);
576         key.size = sizeof(ID);
577         key.flags = DB_DBT_USERMEM;
578         BDB_ID2DISK( eip->bei_id, &nid );
579
580         key.data = &nid;
581
582         /* Need to make dummy root node once. Subsequent attempts
583          * will fail harmlessly.
584          */
585         if ( eip->bei_id == 0 ) {
586                 diskNode dummy = {{0, 0}, "", "", ""};
587                 data.data = &dummy;
588                 data.size = sizeof(diskNode);
589                 data.flags = DB_DBT_USERMEM;
590
591                 db->put( db, txn, &key, &data, DB_NODUPDATA );
592         }
593
594         data.data = d;
595         data.size = sizeof(diskNode) + rlen + nrlen;
596         data.flags = DB_DBT_USERMEM;
597
598         rc = db->put( db, txn, &key, &data, DB_NODUPDATA );
599
600         if (rc == 0) {
601                 BDB_ID2DISK( e->e_id, &nid );
602                 BDB_ID2DISK( eip->bei_id, ptr );
603                 d->nrdnlen[0] ^= 0x80;
604
605                 rc = db->put( db, txn, &key, &data, DB_NODUPDATA );
606         }
607
608         /* Update all parents' IDL cache entries */
609         if ( rc == 0 && bdb->bi_idl_cache_size ) {
610                 ID tmp[2];
611                 char *ptr = ((char *)&tmp[1])-1;
612                 key.data = ptr;
613                 key.size = sizeof(ID)+1;
614                 tmp[1] = eip->bei_id;
615                 *ptr = DN_ONE_PREFIX;
616                 bdb_idl_cache_add_id( bdb, db, &key, e->e_id );
617                 if ( eip->bei_parent ) {
618                         *ptr = DN_SUBTREE_PREFIX;
619                         for (; eip && eip->bei_parent->bei_id; eip = eip->bei_parent) {
620                                 tmp[1] = eip->bei_id;
621                                 bdb_idl_cache_add_id( bdb, db, &key, e->e_id );
622                         }
623                 }
624         }
625
626         op->o_tmpfree( d, op->o_tmpmemctx );
627         Debug( LDAP_DEBUG_TRACE, "<= hdb_dn2id_add 0x%lx: %d\n", e->e_id, rc, 0 );
628
629         return rc;
630 }
631
632 int
633 hdb_dn2id_delete(
634         Operation       *op,
635         DB_TXN *txn,
636         EntryInfo       *eip,
637         Entry   *e )
638 {
639         struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
640         DB *db = bdb->bi_dn2id->bdi_db;
641         DBT             key, data;
642         DBC     *cursor;
643         diskNode *d;
644         int rc;
645         ID      nid;
646         unsigned char dlen[2];
647         DB_LOCK lock;
648
649         Debug( LDAP_DEBUG_TRACE, "=> hdb_dn2id_delete 0x%lx: \"%s\"\n",
650                 e->e_id, e->e_ndn, 0 );
651
652         DBTzero(&key);
653         key.size = sizeof(ID);
654         key.ulen = key.size;
655         key.flags = DB_DBT_USERMEM;
656         BDB_ID2DISK( eip->bei_id, &nid );
657
658         DBTzero(&data);
659         data.size = sizeof(diskNode) + BEI(e)->bei_nrdn.bv_len - sizeof(ID) - 1;
660         data.ulen = data.size;
661         data.dlen = data.size;
662         data.flags = DB_DBT_USERMEM | DB_DBT_PARTIAL;
663
664         key.data = &nid;
665
666         d = op->o_tmpalloc( data.size, op->o_tmpmemctx );
667         d->nrdnlen[1] = BEI(e)->bei_nrdn.bv_len & 0xff;
668         d->nrdnlen[0] = (BEI(e)->bei_nrdn.bv_len >> 8) | 0x80;
669         dlen[0] = d->nrdnlen[0];
670         dlen[1] = d->nrdnlen[1];
671         strcpy( d->nrdn, BEI(e)->bei_nrdn.bv_val );
672         data.data = d;
673
674         rc = db->cursor( db, txn, &cursor, bdb->bi_db_opflags );
675         if ( rc ) goto func_leave;
676
677         /* We hold this lock until the TXN completes */
678         rc = bdb_dn2id_lock( bdb, &e->e_nname, 1, txn, &lock );
679         if ( rc ) goto nolock;
680
681         /* Delete our ID from the parent's list */
682         rc = cursor->c_get( cursor, &key, &data, DB_GET_BOTH_RANGE );
683         if ( rc == 0 ) {
684                 if ( dlen[1] == d->nrdnlen[1] && dlen[0] == d->nrdnlen[0] &&
685                         !strcmp( d->nrdn, BEI(e)->bei_nrdn.bv_val ))
686                         rc = cursor->c_del( cursor, 0 );
687                 else
688                         rc = DB_NOTFOUND;
689         }
690
691         /* Delete our ID from the tree. With sorted duplicates, this
692          * will leave any child nodes still hanging around. This is OK
693          * for modrdn, which will add our info back in later.
694          */
695         if ( rc == 0 ) {
696                 BDB_ID2DISK( e->e_id, &nid );
697                 rc = cursor->c_get( cursor, &key, &data, DB_SET );
698                 if ( rc == 0 )
699                         rc = cursor->c_del( cursor, 0 );
700         }
701
702 nolock:
703         cursor->c_close( cursor );
704 func_leave:
705         op->o_tmpfree( d, op->o_tmpmemctx );
706
707         /* Delete IDL cache entries */
708         if ( rc == 0 && bdb->bi_idl_cache_size ) {
709                 ID tmp[2];
710                 char *ptr = ((char *)&tmp[1])-1;
711                 key.data = ptr;
712                 key.size = sizeof(ID)+1;
713                 tmp[1] = eip->bei_id;
714                 *ptr = DN_ONE_PREFIX;
715                 bdb_idl_cache_del_id( bdb, db, &key, e->e_id );
716                 if ( eip ->bei_parent ) {
717                         *ptr = DN_SUBTREE_PREFIX;
718                         for (; eip && eip->bei_parent->bei_id; eip = eip->bei_parent) {
719                                 tmp[1] = eip->bei_id;
720                                 bdb_idl_cache_del_id( bdb, db, &key, e->e_id );
721                         }
722                 }
723         }
724         Debug( LDAP_DEBUG_TRACE, "<= hdb_dn2id_delete 0x%lx: %d\n", e->e_id, rc, 0 );
725         return rc;
726 }
727
728
729 int
730 hdb_dn2id(
731         Operation       *op,
732         struct berval   *in,
733         EntryInfo       *ei,
734         DB_TXN *txn,
735         DB_LOCK *lock )
736 {
737         struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
738         DB *db = bdb->bi_dn2id->bdi_db;
739         DBT             key, data;
740         DBC     *cursor;
741         int             rc = 0, nrlen;
742         diskNode *d;
743         char    *ptr;
744         unsigned char dlen[2];
745         ID idp, parentID;
746
747         Debug( LDAP_DEBUG_TRACE, "=> hdb_dn2id(\"%s\")\n", in->bv_val, 0, 0 );
748
749         nrlen = dn_rdnlen( op->o_bd, in );
750         if (!nrlen) nrlen = in->bv_len;
751
752         DBTzero(&key);
753         key.size = sizeof(ID);
754         key.data = &idp;
755         key.ulen = sizeof(ID);
756         key.flags = DB_DBT_USERMEM;
757         parentID = ( ei->bei_parent != NULL ) ? ei->bei_parent->bei_id : 0;
758         BDB_ID2DISK( parentID, &idp );
759
760         DBTzero(&data);
761         data.size = sizeof(diskNode) + nrlen - sizeof(ID) - 1;
762         data.ulen = data.size * 3;
763         data.dlen = data.ulen;
764         data.flags = DB_DBT_USERMEM | DB_DBT_PARTIAL;
765
766         rc = db->cursor( db, txn, &cursor, bdb->bi_db_opflags );
767         if ( rc ) return rc;
768
769         d = op->o_tmpalloc( data.size * 3, op->o_tmpmemctx );
770         d->nrdnlen[1] = nrlen & 0xff;
771         d->nrdnlen[0] = (nrlen >> 8) | 0x80;
772         dlen[0] = d->nrdnlen[0];
773         dlen[1] = d->nrdnlen[1];
774         ptr = lutil_strncopy( d->nrdn, in->bv_val, nrlen );
775         *ptr = '\0';
776         data.data = d;
777
778         rc = bdb_dn2id_lock( bdb, in, 0, txn, lock );
779         if ( rc ) goto func_leave;
780
781         rc = cursor->c_get( cursor, &key, &data, DB_GET_BOTH_RANGE );
782         if ( rc == 0 && (dlen[1] != d->nrdnlen[1] || dlen[0] != d->nrdnlen[0] ||
783                 strncmp( d->nrdn, in->bv_val, nrlen ))) {
784                 rc = DB_NOTFOUND;
785         }
786         if ( rc == 0 ) {
787                 ptr = (char *) data.data + data.size - sizeof(ID);
788                 BDB_DISK2ID( ptr, &ei->bei_id );
789                 ei->bei_rdn.bv_len = data.size - sizeof(diskNode) - nrlen;
790                 ptr = d->nrdn + nrlen + 1;
791                 ber_str2bv( ptr, ei->bei_rdn.bv_len, 1, &ei->bei_rdn );
792                 if ( ei->bei_parent != NULL && !ei->bei_parent->bei_dkids ) {
793                         db_recno_t dkids;
794                         /* How many children does the parent have? */
795                         /* FIXME: do we need to lock the parent
796                          * entryinfo? Seems safe...
797                          */
798                         cursor->c_count( cursor, &dkids, 0 );
799                         ei->bei_parent->bei_dkids = dkids;
800                 }
801         }
802
803 func_leave:
804         cursor->c_close( cursor );
805         op->o_tmpfree( d, op->o_tmpmemctx );
806         if( rc != 0 ) {
807                 Debug( LDAP_DEBUG_TRACE, "<= hdb_dn2id: get failed: %s (%d)\n",
808                         db_strerror( rc ), rc, 0 );
809         } else {
810                 Debug( LDAP_DEBUG_TRACE, "<= hdb_dn2id: got id=0x%lx\n",
811                         ei->bei_id, 0, 0 );
812         }
813
814         return rc;
815 }
816
817 int
818 hdb_dn2id_parent(
819         Operation *op,
820         DB_TXN *txn,
821         EntryInfo *ei,
822         ID *idp )
823 {
824         struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
825         DB *db = bdb->bi_dn2id->bdi_db;
826         DBT             key, data;
827         DBC     *cursor;
828         int             rc = 0;
829         diskNode *d;
830         char    *ptr;
831         ID      nid;
832
833         DBTzero(&key);
834         key.size = sizeof(ID);
835         key.data = &nid;
836         key.ulen = sizeof(ID);
837         key.flags = DB_DBT_USERMEM;
838         BDB_ID2DISK( ei->bei_id, &nid );
839
840         DBTzero(&data);
841         data.flags = DB_DBT_USERMEM;
842
843         rc = db->cursor( db, txn, &cursor, bdb->bi_db_opflags );
844         if ( rc ) return rc;
845
846         data.ulen = sizeof(diskNode) + (SLAP_LDAPDN_MAXLEN * 2);
847         d = op->o_tmpalloc( data.ulen, op->o_tmpmemctx );
848         data.data = d;
849
850         rc = cursor->c_get( cursor, &key, &data, DB_SET );
851         if ( rc == 0 ) {
852                 if (d->nrdnlen[0] & 0x80) {
853                         rc = LDAP_OTHER;
854                 } else {
855                         db_recno_t dkids;
856                         ptr = (char *) data.data + data.size - sizeof(ID);
857                         BDB_DISK2ID( ptr, idp );
858                         ei->bei_nrdn.bv_len = (d->nrdnlen[0] << 8) | d->nrdnlen[1];
859                         ber_str2bv( d->nrdn, ei->bei_nrdn.bv_len, 1, &ei->bei_nrdn );
860                         ei->bei_rdn.bv_len = data.size - sizeof(diskNode) -
861                                 ei->bei_nrdn.bv_len;
862                         ptr = d->nrdn + ei->bei_nrdn.bv_len + 1;
863                         ber_str2bv( ptr, ei->bei_rdn.bv_len, 1, &ei->bei_rdn );
864                         /* How many children does this node have? */
865                         cursor->c_count( cursor, &dkids, 0 );
866                         ei->bei_dkids = dkids;
867                 }
868         }
869         cursor->c_close( cursor );
870         op->o_tmpfree( d, op->o_tmpmemctx );
871         return rc;
872 }
873
874 int
875 hdb_dn2id_children(
876         Operation *op,
877         DB_TXN *txn,
878         Entry *e )
879 {
880         struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
881         DB *db = bdb->bi_dn2id->bdi_db;
882         DBT             key, data;
883         DBC             *cursor;
884         int             rc;
885         ID              id;
886         diskNode d;
887
888         DBTzero(&key);
889         key.size = sizeof(ID);
890         key.data = &e->e_id;
891         key.flags = DB_DBT_USERMEM;
892         BDB_ID2DISK( e->e_id, &id );
893
894         /* IDL cache is in host byte order */
895         if ( bdb->bi_idl_cache_size ) {
896                 rc = bdb_idl_cache_get( bdb, db, &key, NULL );
897                 if ( rc != LDAP_NO_SUCH_OBJECT ) {
898                         return rc;
899                 }
900         }
901
902         key.data = &id;
903         DBTzero(&data);
904         data.data = &d;
905         data.ulen = sizeof(d);
906         data.flags = DB_DBT_USERMEM | DB_DBT_PARTIAL;
907         data.dlen = sizeof(d);
908
909         rc = db->cursor( db, txn, &cursor, bdb->bi_db_opflags );
910         if ( rc ) return rc;
911
912         rc = cursor->c_get( cursor, &key, &data, DB_SET );
913         if ( rc == 0 ) {
914                 db_recno_t dkids;
915                 rc = cursor->c_count( cursor, &dkids, 0 );
916                 if ( rc == 0 ) {
917                         BEI(e)->bei_dkids = dkids;
918                         if ( dkids < 2 ) rc = DB_NOTFOUND;
919                 }
920         }
921         cursor->c_close( cursor );
922         return rc;
923 }
924
925 /* bdb_dn2idl:
926  * We can't just use bdb_idl_fetch_key because
927  * 1 - our data items are longer than just an entry ID
928  * 2 - our data items are sorted alphabetically by nrdn, not by ID.
929  *
930  * We descend the tree recursively, so we define this cookie
931  * to hold our necessary state information. The bdb_dn2idl_internal
932  * function uses this cookie when calling itself.
933  */
934
935 struct dn2id_cookie {
936         struct bdb_info *bdb;
937         Operation *op;
938         DB_TXN *txn;
939         EntryInfo *ei;
940         ID *ids;
941         ID *tmp;
942         ID *buf;
943         DB *db;
944         DBC *dbc;
945         DBT key;
946         DBT data;
947         ID dbuf;
948         ID id;
949         ID nid;
950         int rc;
951         int depth;
952         char need_sort;
953         char prefix;
954 };
955
956 static int
957 apply_func(
958         void *data,
959         void *arg )
960 {
961         EntryInfo *ei = data;
962         ID *idl = arg;
963
964         bdb_idl_append_one( idl, ei->bei_id );
965         return 0;
966 }
967
968 static int
969 hdb_dn2idl_internal(
970         struct dn2id_cookie *cx
971 )
972 {
973         BDB_IDL_ZERO( cx->tmp );
974
975         if ( cx->bdb->bi_idl_cache_size ) {
976                 char *ptr = ((char *)&cx->id)-1;
977
978                 cx->key.data = ptr;
979                 cx->key.size = sizeof(ID)+1;
980                 if ( cx->prefix == DN_SUBTREE_PREFIX ) {
981                         ID *ids = cx->depth ? cx->tmp : cx->ids;
982                         *ptr = cx->prefix;
983                         cx->rc = bdb_idl_cache_get(cx->bdb, cx->db, &cx->key, ids);
984                         if ( cx->rc == LDAP_SUCCESS ) {
985                                 if ( cx->depth ) {
986                                         bdb_idl_append( cx->ids, cx->tmp );
987                                         cx->need_sort = 1;
988                                 }
989                                 return cx->rc;
990                         }
991                 }
992                 *ptr = DN_ONE_PREFIX;
993                 cx->rc = bdb_idl_cache_get(cx->bdb, cx->db, &cx->key, cx->tmp);
994                 if ( cx->rc == LDAP_SUCCESS ) {
995                         goto gotit;
996                 }
997                 if ( cx->rc == DB_NOTFOUND ) {
998                         return cx->rc;
999                 }
1000         }
1001
1002         bdb_cache_entryinfo_lock( cx->ei );
1003
1004         /* If number of kids in the cache differs from on-disk, load
1005          * up all the kids from the database
1006          */
1007         if ( cx->ei->bei_ckids+1 != cx->ei->bei_dkids ) {
1008                 EntryInfo ei;
1009                 db_recno_t dkids = cx->ei->bei_dkids;
1010                 ei.bei_parent = cx->ei;
1011
1012                 /* Only one thread should load the cache */
1013                 while ( cx->ei->bei_state & CACHE_ENTRY_ONELEVEL ) {
1014                         bdb_cache_entryinfo_unlock( cx->ei );
1015                         ldap_pvt_thread_yield();
1016                         bdb_cache_entryinfo_lock( cx->ei );
1017                         if ( cx->ei->bei_ckids+1 == cx->ei->bei_dkids ) {
1018                                 goto synced;
1019                         }
1020                 }
1021
1022                 cx->ei->bei_state |= CACHE_ENTRY_ONELEVEL;
1023
1024                 bdb_cache_entryinfo_unlock( cx->ei );
1025
1026                 cx->rc = cx->db->cursor( cx->db, NULL, &cx->dbc,
1027                         cx->bdb->bi_db_opflags );
1028                 if ( cx->rc )
1029                         goto done_one;
1030
1031                 cx->data.data = &cx->dbuf;
1032                 cx->data.ulen = sizeof(ID);
1033                 cx->data.dlen = sizeof(ID);
1034                 cx->data.flags = DB_DBT_USERMEM | DB_DBT_PARTIAL;
1035
1036                 /* The first item holds the parent ID. Ignore it. */
1037                 cx->key.data = &cx->nid;
1038                 cx->key.size = sizeof(ID);
1039                 cx->rc = cx->dbc->c_get( cx->dbc, &cx->key, &cx->data, DB_SET );
1040                 if ( cx->rc ) {
1041                         cx->dbc->c_close( cx->dbc );
1042                         goto done_one;
1043                 }
1044
1045                 /* If the on-disk count is zero we've never checked it.
1046                  * Count it now.
1047                  */
1048                 if ( !dkids ) {
1049                         cx->dbc->c_count( cx->dbc, &dkids, 0 );
1050                         cx->ei->bei_dkids = dkids;
1051                 }
1052
1053                 cx->data.data = cx->buf;
1054                 cx->data.ulen = BDB_IDL_UM_SIZE * sizeof(ID);
1055                 cx->data.flags = DB_DBT_USERMEM;
1056
1057                 if ( dkids > 1 ) {
1058                         /* Fetch the rest of the IDs in a loop... */
1059                         while ( (cx->rc = cx->dbc->c_get( cx->dbc, &cx->key, &cx->data,
1060                                 DB_MULTIPLE | DB_NEXT_DUP )) == 0 ) {
1061                                 u_int8_t *j;
1062                                 size_t len;
1063                                 void *ptr;
1064                                 DB_MULTIPLE_INIT( ptr, &cx->data );
1065                                 while (ptr) {
1066                                         DB_MULTIPLE_NEXT( ptr, &cx->data, j, len );
1067                                         if (j) {
1068                                                 EntryInfo *ei2;
1069                                                 diskNode *d = (diskNode *)j;
1070                                                 short nrlen;
1071
1072                                                 BDB_DISK2ID( j + len - sizeof(ID), &ei.bei_id );
1073                                                 nrlen = ((d->nrdnlen[0] ^ 0x80) << 8) | d->nrdnlen[1];
1074                                                 ei.bei_nrdn.bv_len = nrlen;
1075                                                 /* nrdn/rdn are set in-place.
1076                                                  * hdb_cache_load will copy them as needed
1077                                                  */
1078                                                 ei.bei_nrdn.bv_val = d->nrdn;
1079                                                 ei.bei_rdn.bv_len = len - sizeof(diskNode)
1080                                                         - ei.bei_nrdn.bv_len;
1081                                                 ei.bei_rdn.bv_val = d->nrdn + ei.bei_nrdn.bv_len + 1;
1082                                                 bdb_idl_append_one( cx->tmp, ei.bei_id );
1083                                                 hdb_cache_load( cx->bdb, &ei, &ei2 );
1084                                         }
1085                                 }
1086                         }
1087                 }
1088
1089                 cx->rc = cx->dbc->c_close( cx->dbc );
1090 done_one:
1091                 bdb_cache_entryinfo_lock( cx->ei );
1092                 cx->ei->bei_state ^= CACHE_ENTRY_ONELEVEL;
1093                 bdb_cache_entryinfo_unlock( cx->ei );
1094                 if ( cx->rc )
1095                         return cx->rc;
1096
1097         } else {
1098                 /* The in-memory cache is in sync with the on-disk data.
1099                  * do we have any kids?
1100                  */
1101 synced:
1102                 cx->rc = 0;
1103                 if ( cx->ei->bei_ckids > 0 ) {
1104                         /* Walk the kids tree; order is irrelevant since bdb_idl_sort
1105                          * will sort it later.
1106                          */
1107                         avl_apply( cx->ei->bei_kids, apply_func,
1108                                 cx->tmp, -1, AVL_POSTORDER );
1109                 }
1110                 bdb_cache_entryinfo_unlock( cx->ei );
1111         }
1112
1113         if ( !BDB_IDL_IS_RANGE( cx->tmp ) && cx->tmp[0] > 3 )
1114                 bdb_idl_sort( cx->tmp, cx->buf );
1115         if ( cx->bdb->bi_idl_cache_max_size && !BDB_IDL_IS_ZERO( cx->tmp )) {
1116                 char *ptr = ((char *)&cx->id)-1;
1117                 cx->key.data = ptr;
1118                 cx->key.size = sizeof(ID)+1;
1119                 *ptr = DN_ONE_PREFIX;
1120                 bdb_idl_cache_put( cx->bdb, cx->db, &cx->key, cx->tmp, cx->rc );
1121         }
1122
1123 gotit:
1124         if ( !BDB_IDL_IS_ZERO( cx->tmp )) {
1125                 if ( cx->prefix == DN_SUBTREE_PREFIX ) {
1126                         bdb_idl_append( cx->ids, cx->tmp );
1127                         cx->need_sort = 1;
1128                         if ( !(cx->ei->bei_state & CACHE_ENTRY_NO_GRANDKIDS)) {
1129                                 ID *save, idcurs;
1130                                 EntryInfo *ei = cx->ei;
1131                                 int nokids = 1;
1132                                 save = cx->op->o_tmpalloc( BDB_IDL_SIZEOF( cx->tmp ),
1133                                         cx->op->o_tmpmemctx );
1134                                 BDB_IDL_CPY( save, cx->tmp );
1135
1136                                 idcurs = 0;
1137                                 cx->depth++;
1138                                 for ( cx->id = bdb_idl_first( save, &idcurs );
1139                                         cx->id != NOID;
1140                                         cx->id = bdb_idl_next( save, &idcurs )) {
1141                                         cx->ei = bdb_cache_find_info( cx->bdb, cx->id );
1142                                         if ( !cx->ei ||
1143                                                 ( cx->ei->bei_state & CACHE_ENTRY_NO_KIDS ))
1144                                                 continue;
1145
1146                                         BDB_ID2DISK( cx->id, &cx->nid );
1147                                         hdb_dn2idl_internal( cx );
1148                                         if ( !BDB_IDL_IS_ZERO( cx->tmp ))
1149                                                 nokids = 0;
1150                                 }
1151                                 cx->depth--;
1152                                 cx->op->o_tmpfree( save, cx->op->o_tmpmemctx );
1153                                 if ( nokids ) {
1154                                         bdb_cache_entryinfo_lock( ei );
1155                                         ei->bei_state |= CACHE_ENTRY_NO_GRANDKIDS;
1156                                         bdb_cache_entryinfo_unlock( ei );
1157                                 }
1158                         }
1159                         /* Make sure caller knows it had kids! */
1160                         cx->tmp[0]=1;
1161
1162                         cx->rc = 0;
1163                 } else {
1164                         BDB_IDL_CPY( cx->ids, cx->tmp );
1165                 }
1166         }
1167         return cx->rc;
1168 }
1169
1170 int
1171 hdb_dn2idl(
1172         Operation       *op,
1173         DB_TXN *txn,
1174         struct berval *ndn,
1175         EntryInfo       *ei,
1176         ID *ids,
1177         ID *stack )
1178 {
1179         struct bdb_info *bdb = (struct bdb_info *)op->o_bd->be_private;
1180         struct dn2id_cookie cx;
1181
1182         Debug( LDAP_DEBUG_TRACE, "=> hdb_dn2idl(\"%s\")\n",
1183                 ndn->bv_val, 0, 0 );
1184
1185 #ifndef BDB_MULTIPLE_SUFFIXES
1186         if ( op->ors_scope != LDAP_SCOPE_ONELEVEL && 
1187                 ( ei->bei_id == 0 ||
1188                 ( ei->bei_parent->bei_id == 0 && op->o_bd->be_suffix[0].bv_len )))
1189         {
1190                 BDB_IDL_ALL( bdb, ids );
1191                 return 0;
1192         }
1193 #endif
1194
1195         cx.id = ei->bei_id;
1196         BDB_ID2DISK( cx.id, &cx.nid );
1197         cx.ei = ei;
1198         cx.bdb = bdb;
1199         cx.db = cx.bdb->bi_dn2id->bdi_db;
1200         cx.prefix = (op->ors_scope == LDAP_SCOPE_ONELEVEL) ?
1201                 DN_ONE_PREFIX : DN_SUBTREE_PREFIX;
1202         cx.ids = ids;
1203         cx.tmp = stack;
1204         cx.buf = stack + BDB_IDL_UM_SIZE;
1205         cx.op = op;
1206         cx.txn = txn;
1207         cx.need_sort = 0;
1208         cx.depth = 0;
1209
1210         if ( cx.prefix == DN_SUBTREE_PREFIX ) {
1211                 ids[0] = 1;
1212                 ids[1] = cx.id;
1213         } else {
1214                 BDB_IDL_ZERO( ids );
1215         }
1216         if ( cx.ei->bei_state & CACHE_ENTRY_NO_KIDS )
1217                 return LDAP_SUCCESS;
1218
1219         DBTzero(&cx.key);
1220         cx.key.ulen = sizeof(ID);
1221         cx.key.size = sizeof(ID);
1222         cx.key.flags = DB_DBT_USERMEM;
1223
1224         DBTzero(&cx.data);
1225
1226         hdb_dn2idl_internal(&cx);
1227         if ( cx.need_sort ) {
1228                 char *ptr = ((char *)&cx.id)-1;
1229                 if ( !BDB_IDL_IS_RANGE( cx.ids ) && cx.ids[0] > 3 ) 
1230                         bdb_idl_sort( cx.ids, cx.tmp );
1231                 cx.key.data = ptr;
1232                 cx.key.size = sizeof(ID)+1;
1233                 *ptr = cx.prefix;
1234                 cx.id = ei->bei_id;
1235                 if ( cx.bdb->bi_idl_cache_max_size )
1236                         bdb_idl_cache_put( cx.bdb, cx.db, &cx.key, cx.ids, cx.rc );
1237         }
1238
1239         if ( cx.rc == DB_NOTFOUND )
1240                 cx.rc = LDAP_SUCCESS;
1241
1242         return cx.rc;
1243 }
1244 #endif  /* BDB_HIER */