]> git.sur5r.net Git - openldap/blob - servers/slapd/back-mdb/dn2id.c
Merge remote branch 'origin/mdb.master'
[openldap] / servers / slapd / back-mdb / 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-2011 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-mdb.h"
23 #include "idl.h"
24 #include "lutil.h"
25
26 /* Management routines for a hierarchically structured database.
27  *
28  * Instead of a ldbm-style dn2id database, we use a hierarchical one. Each
29  * entry in this database is a struct diskNode, keyed by entryID and with
30  * the data containing the RDN and entryID of the node's children. We use
31  * a B-Tree with sorted duplicates to store all the children of a node under
32  * the same key. Also, the first item under the key contains the entry's own
33  * rdn and the ID of the node's parent, to allow bottom-up tree traversal as
34  * well as top-down. To keep this info first in the list, the high bit of all
35  * subsequent nrdnlen's is always set. This means we can only accomodate
36  * RDNs up to length 32767, but that's fine since full DNs are already
37  * restricted to 8192.
38  *
39  * The diskNode is a variable length structure. This definition is not
40  * directly usable for in-memory manipulation.
41  */
42 typedef struct diskNode {
43         unsigned char nrdnlen[2];
44         char nrdn[1];
45         char rdn[1];                        /* variable placement */
46         unsigned char entryID[sizeof(ID)];  /* variable placement */
47 } diskNode;
48
49 /* Sort function for the sorted duplicate data items of a dn2id key.
50  * Sorts based on normalized RDN, in length order.
51  */
52 int
53 mdb_dup_compare(
54         const MDB_val *usrkey,
55         const MDB_val *curkey
56 )
57 {
58         diskNode *un, *cn;
59         int rc, nrlen;
60
61         un = (diskNode *)usrkey->mv_data;
62         cn = (diskNode *)curkey->mv_data;
63
64         /* data is not aligned, cannot compare directly */
65         rc = un->nrdnlen[0] - cn->nrdnlen[0];
66         if ( rc ) return rc;
67         rc = un->nrdnlen[1] - cn->nrdnlen[1];
68         if ( rc ) return rc;
69
70         nrlen = (un->nrdnlen[0] << 8) | un->nrdnlen[1];
71         return strncmp( un->nrdn, cn->nrdn, nrlen );
72 }
73
74 #if 0
75 /* This function constructs a full DN for a given entry.
76  */
77 int mdb_fix_dn(
78         Entry *e,
79         int checkit )
80 {
81         EntryInfo *ei;
82         int rlen = 0, nrlen = 0;
83         char *ptr, *nptr;
84         int max = 0;
85
86         if ( !e->e_id )
87                 return 0;
88
89         /* count length of all DN components */
90         for ( ei = BEI(e); ei && ei->bei_id; ei=ei->bei_parent ) {
91                 rlen += ei->bei_rdn.bv_len + 1;
92                 nrlen += ei->bei_nrdn.bv_len + 1;
93                 if (ei->bei_modrdns > max) max = ei->bei_modrdns;
94         }
95
96         /* See if the entry DN was invalidated by a subtree rename */
97         if ( checkit ) {
98                 if ( BEI(e)->bei_modrdns >= max ) {
99                         return 0;
100                 }
101                 /* We found a mismatch, tell the caller to lock it */
102                 if ( checkit == 1 ) {
103                         return 1;
104                 }
105                 /* checkit == 2. do the fix. */
106                 free( e->e_name.bv_val );
107                 free( e->e_nname.bv_val );
108         }
109
110         e->e_name.bv_len = rlen - 1;
111         e->e_nname.bv_len = nrlen - 1;
112         e->e_name.bv_val = ch_malloc(rlen);
113         e->e_nname.bv_val = ch_malloc(nrlen);
114         ptr = e->e_name.bv_val;
115         nptr = e->e_nname.bv_val;
116         for ( ei = BEI(e); ei && ei->bei_id; ei=ei->bei_parent ) {
117                 ptr = lutil_strcopy(ptr, ei->bei_rdn.bv_val);
118                 nptr = lutil_strcopy(nptr, ei->bei_nrdn.bv_val);
119                 if ( ei->bei_parent ) {
120                         *ptr++ = ',';
121                         *nptr++ = ',';
122                 }
123         }
124         BEI(e)->bei_modrdns = max;
125         if ( ptr > e->e_name.bv_val ) ptr[-1] = '\0';
126         if ( nptr > e->e_nname.bv_val ) nptr[-1] = '\0';
127
128         return 0;
129 }
130 #endif
131
132 /* We add two elements to the DN2ID database - a data item under the parent's
133  * entryID containing the child's RDN and entryID, and an item under the
134  * child's entryID containing the parent's entryID.
135  */
136 int
137 mdb_dn2id_add(
138         Operation       *op,
139         MDB_txn *txn,
140         ID pid,
141         Entry           *e )
142 {
143         struct mdb_info *mdb = (struct mdb_info *) op->o_bd->be_private;
144         MDB_dbi dbi = mdb->mi_dn2id;
145         MDB_val         key, data;
146         MDB_cursor      *mc;
147         ID              nid;
148         int             rc, rlen, nrlen;
149         diskNode *d;
150         char *ptr;
151
152         Debug( LDAP_DEBUG_TRACE, "=> mdb_dn2id_add 0x%lx: \"%s\"\n",
153                 e->e_id, e->e_ndn, 0 );
154
155         nrlen = dn_rdnlen( op->o_bd, &e->e_nname );
156         if (nrlen) {
157                 rlen = dn_rdnlen( op->o_bd, &e->e_name );
158         } else {
159                 nrlen = e->e_nname.bv_len;
160                 rlen = e->e_name.bv_len;
161         }
162
163         d = op->o_tmpalloc(sizeof(diskNode) + rlen + nrlen, op->o_tmpmemctx);
164         d->nrdnlen[1] = nrlen & 0xff;
165         d->nrdnlen[0] = (nrlen >> 8) | 0x80;
166         ptr = lutil_strncopy( d->nrdn, e->e_nname.bv_val, nrlen );
167         *ptr++ = '\0';
168         ptr = lutil_strncopy( ptr, e->e_name.bv_val, rlen );
169         *ptr++ = '\0';
170         memcpy( ptr, &e->e_id, sizeof( ID ));
171
172         key.mv_size = sizeof(ID);
173         key.mv_data = &nid;
174
175         nid = pid;
176
177         rc = mdb_cursor_open( txn, dbi, &mc );
178         if ( rc )
179                 goto fail;
180
181         /* Need to make dummy root node once. Subsequent attempts
182          * will fail harmlessly.
183          */
184         if ( pid == 0 ) {
185                 diskNode dummy = {{0, 0}, "", "", ""};
186                 data.mv_data = &dummy;
187                 data.mv_size = sizeof(diskNode);
188
189                 mdb_cursor_put( mc, &key, &data, MDB_NODUPDATA );
190         }
191
192         data.mv_data = d;
193         data.mv_size = sizeof(diskNode) + rlen + nrlen;
194
195         rc = mdb_cursor_put( mc, &key, &data, MDB_NODUPDATA );
196
197         if (rc == 0) {
198                 nid = e->e_id;
199                 memcpy( ptr, &pid, sizeof( ID ));
200                 d->nrdnlen[0] ^= 0x80;
201
202                 rc = mdb_cursor_put( mc, &key, &data, MDB_NODUPDATA );
203         }
204         mdb_cursor_close( mc );
205
206 fail:
207         op->o_tmpfree( d, op->o_tmpmemctx );
208         Debug( LDAP_DEBUG_TRACE, "<= mdb_dn2id_add 0x%lx: %d\n", e->e_id, rc, 0 );
209
210         return rc;
211 }
212
213 int
214 mdb_dn2id_delete(
215         Operation       *op,
216         MDB_txn *txn,
217         ID pid,
218         Entry   *e )
219 {
220         struct mdb_info *mdb = (struct mdb_info *) op->o_bd->be_private;
221         MDB_dbi dbi = mdb->mi_dn2id;
222         MDB_val key, data;
223         diskNode *d;
224         int rc, nrlen;
225         ID      nid;
226
227         Debug( LDAP_DEBUG_TRACE, "=> mdb_dn2id_delete 0x%lx: \"%s\"\n",
228                 e->e_id, e->e_ndn, 0 );
229
230         key.mv_size = sizeof(ID);
231         key.mv_data = &nid;
232         nid = pid;
233
234         nrlen = dn_rdnlen( op->o_bd, &e->e_nname );
235         data.mv_size = sizeof(diskNode) + nrlen - sizeof(ID) - 1;
236
237         d = op->o_tmpalloc( data.mv_size, op->o_tmpmemctx );
238         d->nrdnlen[1] = nrlen & 0xff;
239         d->nrdnlen[0] = (nrlen >> 8) | 0x80;
240         memcpy( d->nrdn, e->e_nname.bv_val, nrlen );
241         d->nrdn[nrlen] = '\0';
242         data.mv_data = d;
243
244         /* Delete our ID from the parent's list */
245         rc = mdb_del( txn, dbi, &key, &data );
246
247         /* Delete our ID from the tree. With sorted duplicates, this
248          * will leave any child nodes still hanging around. This is OK
249          * for modrdn, which will add our info back in later.
250          */
251         if ( rc == 0 ) {
252                 nid = e->e_id;
253                 d->nrdnlen[0] ^= 0x80;
254                 rc = mdb_del( txn, dbi, &key, &data );
255         }
256
257         op->o_tmpfree( d, op->o_tmpmemctx );
258
259         Debug( LDAP_DEBUG_TRACE, "<= mdb_dn2id_delete 0x%lx: %d\n", e->e_id, rc, 0 );
260         return rc;
261 }
262
263 /* return last found ID in *id if no match */
264 int
265 mdb_dn2id(
266         Operation       *op,
267         MDB_txn *txn,
268         struct berval   *in,
269         ID      *id,
270         struct berval   *matched,
271         struct berval   *nmatched )
272 {
273         struct mdb_info *mdb = (struct mdb_info *) op->o_bd->be_private;
274         MDB_cursor *cursor;
275         MDB_dbi dbi = mdb->mi_dn2id;
276         MDB_val         key, data;
277         int             rc = 0, nrlen;
278         diskNode *d;
279         char    *ptr;
280         char dn[SLAP_LDAPDN_MAXLEN];
281         ID pid, nid;
282         struct berval tmp;
283
284         Debug( LDAP_DEBUG_TRACE, "=> mdb_dn2id(\"%s\")\n", in->bv_val, 0, 0 );
285
286         if ( matched ) {
287                 matched->bv_val = dn + sizeof(dn) - 1;
288                 matched->bv_len = 0;
289                 *matched->bv_val-- = '\0';
290         }
291         if ( nmatched ) {
292                 nmatched->bv_len = 0;
293                 nmatched->bv_val = 0;
294         }
295
296         if ( !in->bv_len ) {
297                 *id = 0;
298                 nid = 0;
299                 goto done;
300         }
301
302         tmp = *in;
303
304         if ( op->o_bd->be_nsuffix[0].bv_len ) {
305                 nrlen = tmp.bv_len - op->o_bd->be_nsuffix[0].bv_len;
306                 tmp.bv_val += nrlen;
307                 tmp.bv_len = op->o_bd->be_nsuffix[0].bv_len;
308         } else {
309                 for ( ptr = tmp.bv_val + tmp.bv_len - 1; ptr >= tmp.bv_val; ptr-- )
310                         if (DN_SEPARATOR(*ptr))
311                                 break;
312                 ptr++;
313                 tmp.bv_len -= ptr - tmp.bv_val;
314                 tmp.bv_val = ptr;
315         }
316         nid = 0;
317         key.mv_size = sizeof(ID);
318
319         rc = mdb_cursor_open( txn, dbi, &cursor );
320         if ( rc ) return rc;
321
322         for (;;) {
323                 key.mv_data = &pid;
324                 pid = nid;
325
326                 data.mv_size = sizeof(diskNode) + tmp.bv_len;
327                 d = op->o_tmpalloc( data.mv_size, op->o_tmpmemctx );
328                 d->nrdnlen[1] = tmp.bv_len & 0xff;
329                 d->nrdnlen[0] = (tmp.bv_len >> 8) | 0x80;
330                 ptr = lutil_strncopy( d->nrdn, tmp.bv_val, tmp.bv_len );
331                 *ptr = '\0';
332                 data.mv_data = d;
333                 rc = mdb_cursor_get( cursor, &key, &data, MDB_GET_BOTH );
334                 op->o_tmpfree( d, op->o_tmpmemctx );
335                 if ( rc )
336                         break;
337                 ptr = (char *) data.mv_data + data.mv_size - sizeof(ID);
338                 memcpy( &nid, ptr, sizeof(ID));
339
340                 /* grab the non-normalized RDN */
341                 if ( matched ) {
342                         int rlen;
343                         d = data.mv_data;
344                         rlen = data.mv_size - sizeof(diskNode) - tmp.bv_len;
345                         matched->bv_len += rlen;
346                         matched->bv_val -= rlen + 1;
347                         ptr = lutil_strcopy( matched->bv_val, d->rdn + tmp.bv_len );
348                         if ( pid ) {
349                                 *ptr = ',';
350                                 matched->bv_len++;
351                         }
352                 }
353                 if ( nmatched ) {
354                         nmatched->bv_val = tmp.bv_val;
355                 }
356
357                 if ( tmp.bv_val > in->bv_val ) {
358                         for (ptr = tmp.bv_val - 2; ptr > in->bv_val &&
359                                 !DN_SEPARATOR(*ptr); ptr--)     /* empty */;
360                         if ( ptr >= in->bv_val ) {
361                                 if (DN_SEPARATOR(*ptr)) ptr++;
362                                 tmp.bv_len = tmp.bv_val - ptr - 1;
363                                 tmp.bv_val = ptr;
364                         }
365                 } else {
366                         break;
367                 }
368         }
369         *id = nid; 
370         mdb_cursor_close( cursor );
371 done:
372         if ( matched ) {
373                 if ( matched->bv_len ) {
374                         ptr = op->o_tmpalloc( matched->bv_len+1, op->o_tmpmemctx );
375                         strcpy( ptr, matched->bv_val );
376                         matched->bv_val = ptr;
377                 } else {
378                         if ( BER_BVISEMPTY( &op->o_bd->be_nsuffix[0] ) && !nid ) {
379                                 ber_dupbv( matched, (struct berval *)&slap_empty_bv );
380                         } else {
381                                 matched->bv_val = NULL;
382                         }
383                 }
384         }
385         if ( nmatched ) {
386                 if ( nmatched->bv_val ) {
387                         nmatched->bv_len = in->bv_len - (nmatched->bv_val - in->bv_val);
388                 } else {
389                         *nmatched = slap_empty_bv;
390                 }
391         }
392
393         if( rc != 0 ) {
394                 Debug( LDAP_DEBUG_TRACE, "<= mdb_dn2id: get failed: %s (%d)\n",
395                         mdb_strerror( rc ), rc, 0 );
396         } else {
397                 Debug( LDAP_DEBUG_TRACE, "<= mdb_dn2id: got id=0x%lx\n",
398                         nid, 0, 0 );
399         }
400
401         return rc;
402 }
403
404 /* return IDs from root to parent of DN */
405 int
406 mdb_dn2sups(
407         Operation       *op,
408         MDB_txn *txn,
409         struct berval   *in,
410         ID      *ids )
411 {
412         struct mdb_info *mdb = (struct mdb_info *) op->o_bd->be_private;
413         MDB_cursor *cursor;
414         MDB_dbi dbi = mdb->mi_dn2id;
415         MDB_val         key, data;
416         int             rc = 0, nrlen;
417         diskNode *d;
418         char    *ptr;
419         ID pid, nid;
420         struct berval tmp;
421
422         Debug( LDAP_DEBUG_TRACE, "=> mdb_dn2sups(\"%s\")\n", in->bv_val, 0, 0 );
423
424         if ( !in->bv_len ) {
425                 goto done;
426         }
427
428         tmp = *in;
429
430         nrlen = tmp.bv_len - op->o_bd->be_nsuffix[0].bv_len;
431         tmp.bv_val += nrlen;
432         tmp.bv_len = op->o_bd->be_nsuffix[0].bv_len;
433         nid = 0;
434         key.mv_size = sizeof(ID);
435
436         rc = mdb_cursor_open( txn, dbi, &cursor );
437         if ( rc ) return rc;
438
439         for (;;) {
440                 key.mv_data = &pid;
441                 pid = nid;
442
443                 data.mv_size = sizeof(diskNode) + tmp.bv_len;
444                 d = op->o_tmpalloc( data.mv_size, op->o_tmpmemctx );
445                 d->nrdnlen[1] = tmp.bv_len & 0xff;
446                 d->nrdnlen[0] = (tmp.bv_len >> 8) | 0x80;
447                 ptr = lutil_strncopy( d->nrdn, tmp.bv_val, tmp.bv_len );
448                 *ptr = '\0';
449                 data.mv_data = d;
450                 rc = mdb_cursor_get( cursor, &key, &data, MDB_GET_BOTH );
451                 op->o_tmpfree( d, op->o_tmpmemctx );
452                 if ( rc ) {
453                         mdb_cursor_close( cursor );
454                         break;
455                 }
456                 ptr = (char *) data.mv_data + data.mv_size - sizeof(ID);
457                 memcpy( &nid, ptr, sizeof(ID));
458
459                 if ( pid )
460                         mdb_idl_insert( ids, pid );
461
462                 if ( tmp.bv_val > in->bv_val ) {
463                         for (ptr = tmp.bv_val - 2; ptr > in->bv_val &&
464                                 !DN_SEPARATOR(*ptr); ptr--)     /* empty */;
465                         if ( ptr >= in->bv_val ) {
466                                 if (DN_SEPARATOR(*ptr)) ptr++;
467                                 tmp.bv_len = tmp.bv_val - ptr - 1;
468                                 tmp.bv_val = ptr;
469                         }
470                 } else {
471                         break;
472                 }
473         }
474
475 done:
476         if( rc != 0 ) {
477                 Debug( LDAP_DEBUG_TRACE, "<= mdb_dn2sups: get failed: %s (%d)\n",
478                         mdb_strerror( rc ), rc, 0 );
479         }
480
481         return rc;
482 }
483
484 #if 0
485 int
486 mdb_dn2id_parent(
487         Operation *op,
488         DB_TXN *txn,
489         EntryInfo *ei,
490         ID *idp )
491 {
492         struct mdb_info *mdb = (struct mdb_info *) op->o_bd->be_private;
493         DB *db = mdb->bi_dn2id->bdi_db;
494         DBT             key, data;
495         DBC     *cursor;
496         int             rc = 0;
497         diskNode *d;
498         char    *ptr;
499         ID      nid;
500
501         DBTzero(&key);
502         key.size = sizeof(ID);
503         key.data = &nid;
504         key.ulen = sizeof(ID);
505         key.flags = DB_DBT_USERMEM;
506         MDB_ID2DISK( ei->bei_id, &nid );
507
508         DBTzero(&data);
509         data.flags = DB_DBT_USERMEM;
510
511         rc = db->cursor( db, txn, &cursor, mdb->bi_db_opflags );
512         if ( rc ) return rc;
513
514         data.ulen = sizeof(diskNode) + (SLAP_LDAPDN_MAXLEN * 2);
515         d = op->o_tmpalloc( data.ulen, op->o_tmpmemctx );
516         data.data = d;
517
518         rc = cursor->c_get( cursor, &key, &data, DB_SET );
519         if ( rc == 0 ) {
520                 if (d->nrdnlen[0] & 0x80) {
521                         rc = LDAP_OTHER;
522                 } else {
523                         db_recno_t dkids;
524                         ptr = (char *) data.data + data.size - sizeof(ID);
525                         MDB_DISK2ID( ptr, idp );
526                         ei->bei_nrdn.bv_len = (d->nrdnlen[0] << 8) | d->nrdnlen[1];
527                         ber_str2bv( d->nrdn, ei->bei_nrdn.bv_len, 1, &ei->bei_nrdn );
528                         ei->bei_rdn.bv_len = data.size - sizeof(diskNode) -
529                                 ei->bei_nrdn.bv_len;
530                         ptr = d->nrdn + ei->bei_nrdn.bv_len + 1;
531                         ber_str2bv( ptr, ei->bei_rdn.bv_len, 1, &ei->bei_rdn );
532                         /* How many children does this node have? */
533                         cursor->c_count( cursor, &dkids, 0 );
534                         ei->bei_dkids = dkids;
535                 }
536         }
537         cursor->c_close( cursor );
538         op->o_tmpfree( d, op->o_tmpmemctx );
539         return rc;
540 }
541 #endif
542
543 int
544 mdb_dn2id_children(
545         Operation *op,
546         MDB_txn *txn,
547         Entry *e )
548 {
549         struct mdb_info *mdb = (struct mdb_info *) op->o_bd->be_private;
550         MDB_dbi dbi = mdb->mi_dn2id;
551         MDB_val         key, data;
552         MDB_cursor      *cursor;
553         int             rc;
554         ID              id;
555
556         key.mv_size = sizeof(ID);
557         key.mv_data = &id;
558         id = e->e_id;
559
560         rc = mdb_cursor_open( txn, dbi, &cursor );
561         if ( rc ) return rc;
562
563         rc = mdb_cursor_get( cursor, &key, &data, MDB_SET );
564         if ( rc == 0 ) {
565                 unsigned long dkids;
566                 rc = mdb_cursor_count( cursor, &dkids );
567                 if ( rc == 0 ) {
568                         if ( dkids < 2 ) rc = MDB_NOTFOUND;
569                 }
570         }
571         mdb_cursor_close( cursor );
572         return rc;
573 }
574
575 int
576 mdb_id2name(
577         Operation *op,
578         MDB_txn *txn,
579         MDB_cursor **cursp,
580         ID id,
581         struct berval *name,
582         struct berval *nname )
583 {
584         struct mdb_info *mdb = (struct mdb_info *) op->o_bd->be_private;
585         MDB_dbi dbi = mdb->mi_dn2id;
586         MDB_val         key, data;
587         MDB_cursor      *cursor;
588         int             rc, len, nlen;
589         char dn[SLAP_LDAPDN_MAXLEN], ndn[SLAP_LDAPDN_MAXLEN], *ptr;
590         char *dptr, *nptr;
591         diskNode *d;
592
593         key.mv_size = sizeof(ID);
594
595         if ( !*cursp ) {
596                 rc = mdb_cursor_open( txn, dbi, cursp );
597                 if ( rc ) return rc;
598         }
599         cursor = *cursp;
600
601         len = 0;
602         nlen = 0;
603         dptr = dn;
604         nptr = ndn;
605         while (id) {
606                 unsigned int nrlen, rlen;
607                 key.mv_data = &id;
608                 data.mv_size = 0;
609                 data.mv_data = "";
610                 rc = mdb_cursor_get( cursor, &key, &data, MDB_SET );
611                 if ( rc ) break;
612                 ptr = data.mv_data;
613                 ptr += data.mv_size - sizeof(ID);
614                 memcpy( &id, ptr, sizeof(ID) );
615                 d = data.mv_data;
616                 nrlen = (d->nrdnlen[0] << 8) | d->nrdnlen[1];
617                 rlen = data.mv_size - sizeof(diskNode) - nrlen;
618                 assert( nrlen < 1024 && rlen < 1024 );  /* FIXME: Sanity check */
619                 if (nptr > ndn) {
620                         *nptr++ = ',';
621                         *dptr++ = ',';
622                 }
623                 /* copy name and trailing NUL */
624                 memcpy( nptr, d->nrdn, nrlen+1 );
625                 memcpy( dptr, d->nrdn+nrlen+1, rlen+1 );
626                 nptr += nrlen;
627                 dptr += rlen;
628         }
629         if ( rc == 0 ) {
630                 name->bv_len = dptr - dn;
631                 nname->bv_len = nptr - ndn;
632                 name->bv_val = op->o_tmpalloc( name->bv_len + 1, op->o_tmpmemctx );
633                 nname->bv_val = op->o_tmpalloc( nname->bv_len + 1, op->o_tmpmemctx );
634                 memcpy( name->bv_val, dn, name->bv_len );
635                 name->bv_val[name->bv_len] = '\0';
636                 memcpy( nname->bv_val, ndn, nname->bv_len );
637                 nname->bv_val[nname->bv_len] = '\0';
638         }
639         return rc;
640 }
641
642 /* Find each id in ids that is a child of base and move it to res.
643  */
644 int
645 mdb_idscope(
646         Operation *op,
647         MDB_txn *txn,
648         ID base,
649         ID *ids,
650         ID *res )
651 {
652         struct mdb_info *mdb = (struct mdb_info *) op->o_bd->be_private;
653         MDB_dbi dbi = mdb->mi_dn2id;
654         MDB_val         key, data;
655         MDB_cursor      *cursor;
656         ID ida, id, cid, ci0, idc = 0;
657         char    *ptr;
658         int             rc;
659
660         key.mv_size = sizeof(ID);
661
662         MDB_IDL_ZERO( res );
663
664         rc = mdb_cursor_open( txn, dbi, &cursor );
665         if ( rc ) return rc;
666
667         ida = mdb_idl_first( ids, &cid );
668
669         /* Don't bother moving out of ids if it's a range */
670         if (!MDB_IDL_IS_RANGE(ids)) {
671                 idc = ids[0];
672                 ci0 = cid;
673         }
674
675         while (ida != NOID) {
676                 id = ida;
677                 while (id) {
678                         key.mv_data = &id;
679                         rc = mdb_cursor_get( cursor, &key, &data, MDB_SET );
680                         if ( rc ) {
681                                 /* not found, move on to next */
682                                 if (idc) {
683                                         if (ci0 != cid)
684                                                 ids[ci0] = ids[cid];
685                                         ci0++;
686                                 }
687                                 break;
688                         }
689                         ptr = data.mv_data;
690                         ptr += data.mv_size - sizeof(ID);
691                         memcpy( &id, ptr, sizeof(ID) );
692                         if ( id == base ) {
693                                 res[0]++;
694                                 res[res[0]] = ida;
695                                 if (idc)
696                                         idc--;
697                                 break;
698                         } else {
699                                 if (idc) {
700                                         if (ci0 != cid)
701                                                 ids[ci0] = ids[cid];
702                                         ci0++;
703                                 }
704                         }
705                         if ( op->ors_scope == LDAP_SCOPE_ONELEVEL )
706                                 break;
707                 }
708                 ida = mdb_idl_next( ids, &cid );
709         }
710         if (!MDB_IDL_IS_RANGE( ids ))
711                 ids[0] = idc;
712
713         mdb_cursor_close( cursor );
714         return rc;
715 }
716
717 /* See if base is a child of any of the scopes
718  */
719 int
720 mdb_idscopes(
721         Operation *op,
722         IdScopes *isc )
723 {
724         struct mdb_info *mdb = (struct mdb_info *) op->o_bd->be_private;
725         MDB_dbi dbi = mdb->mi_dn2id;
726         MDB_val         key, data;
727         ID id;
728         char    *ptr;
729         int             rc;
730         unsigned int x;
731         unsigned int nrlen, rlen;
732         diskNode *d;
733
734         key.mv_size = sizeof(ID);
735
736         if ( !isc->mc ) {
737                 rc = mdb_cursor_open( isc->mt, dbi, &isc->mc );
738                 if ( rc ) return rc;
739         }
740
741         id = isc->id;
742         while (id) {
743                 key.mv_data = &id;
744                 rc = mdb_cursor_get( isc->mc, &key, &data, MDB_SET );
745                 if ( rc )
746                         break;
747
748                 /* save RDN info */
749                 d = data.mv_data;
750                 nrlen = (d->nrdnlen[0] << 8) | d->nrdnlen[1];
751                 rlen = data.mv_size - sizeof(diskNode) - nrlen;
752                 isc->nrdns[isc->numrdns].bv_len = nrlen;
753                 isc->nrdns[isc->numrdns].bv_val = d->nrdn;
754                 isc->rdns[isc->numrdns].bv_len = rlen;
755                 isc->rdns[isc->numrdns].bv_val = d->nrdn+nrlen+1;
756                 isc->numrdns++;
757
758                 ptr = data.mv_data;
759                 ptr += data.mv_size - sizeof(ID);
760                 memcpy( &id, ptr, sizeof(ID) );
761                 x = mdb_idl_search( isc->scopes, id );
762                 if ( isc->scopes[x] == id ) {
763                         isc->nscope = x;
764                         return MDB_SUCCESS;
765                 }
766                 if ( op->ors_scope == LDAP_SCOPE_ONELEVEL )
767                         break;
768         }
769         return MDB_NOTFOUND;
770 }
771
772 #if 0
773 /* mdb_dn2idl:
774  * We can't just use mdb_idl_fetch_key because
775  * 1 - our data items are longer than just an entry ID
776  * 2 - our data items are sorted alphabetically by nrdn, not by ID.
777  *
778  * We descend the tree recursively, so we define this cookie
779  * to hold our necessary state information. The mdb_dn2idl_internal
780  * function uses this cookie when calling itself.
781  */
782
783 struct dn2id_cookie {
784         struct mdb_info *mdb;
785         Operation *op;
786         DB_TXN *txn;
787         EntryInfo *ei;
788         ID *ids;
789         ID *tmp;
790         ID *buf;
791         DB *db;
792         DBC *dbc;
793         DBT key;
794         DBT data;
795         ID dbuf;
796         ID id;
797         ID nid;
798         int rc;
799         int depth;
800         char need_sort;
801         char prefix;
802 };
803
804 static int
805 apply_func(
806         void *data,
807         void *arg )
808 {
809         EntryInfo *ei = data;
810         ID *idl = arg;
811
812         mdb_idl_append_one( idl, ei->bei_id );
813         return 0;
814 }
815
816 static int
817 mdb_dn2idl_internal(
818         struct dn2id_cookie *cx
819 )
820 {
821         MDB_IDL_ZERO( cx->tmp );
822
823         if ( cx->mdb->bi_idl_cache_size ) {
824                 char *ptr = ((char *)&cx->id)-1;
825
826                 cx->key.data = ptr;
827                 cx->key.size = sizeof(ID)+1;
828                 if ( cx->prefix == DN_SUBTREE_PREFIX ) {
829                         ID *ids = cx->depth ? cx->tmp : cx->ids;
830                         *ptr = cx->prefix;
831                         cx->rc = mdb_idl_cache_get(cx->mdb, cx->db, &cx->key, ids);
832                         if ( cx->rc == LDAP_SUCCESS ) {
833                                 if ( cx->depth ) {
834                                         mdb_idl_append( cx->ids, cx->tmp );
835                                         cx->need_sort = 1;
836                                 }
837                                 return cx->rc;
838                         }
839                 }
840                 *ptr = DN_ONE_PREFIX;
841                 cx->rc = mdb_idl_cache_get(cx->mdb, cx->db, &cx->key, cx->tmp);
842                 if ( cx->rc == LDAP_SUCCESS ) {
843                         goto gotit;
844                 }
845                 if ( cx->rc == DB_NOTFOUND ) {
846                         return cx->rc;
847                 }
848         }
849
850         mdb_cache_entryinfo_lock( cx->ei );
851
852         /* If number of kids in the cache differs from on-disk, load
853          * up all the kids from the database
854          */
855         if ( cx->ei->bei_ckids+1 != cx->ei->bei_dkids ) {
856                 EntryInfo ei;
857                 db_recno_t dkids = cx->ei->bei_dkids;
858                 ei.bei_parent = cx->ei;
859
860                 /* Only one thread should load the cache */
861                 while ( cx->ei->bei_state & CACHE_ENTRY_ONELEVEL ) {
862                         mdb_cache_entryinfo_unlock( cx->ei );
863                         ldap_pvt_thread_yield();
864                         mdb_cache_entryinfo_lock( cx->ei );
865                         if ( cx->ei->bei_ckids+1 == cx->ei->bei_dkids ) {
866                                 goto synced;
867                         }
868                 }
869
870                 cx->ei->bei_state |= CACHE_ENTRY_ONELEVEL;
871
872                 mdb_cache_entryinfo_unlock( cx->ei );
873
874                 cx->rc = cx->db->cursor( cx->db, NULL, &cx->dbc,
875                         cx->mdb->bi_db_opflags );
876                 if ( cx->rc )
877                         goto done_one;
878
879                 cx->data.data = &cx->dbuf;
880                 cx->data.ulen = sizeof(ID);
881                 cx->data.dlen = sizeof(ID);
882                 cx->data.flags = DB_DBT_USERMEM | DB_DBT_PARTIAL;
883
884                 /* The first item holds the parent ID. Ignore it. */
885                 cx->key.data = &cx->nid;
886                 cx->key.size = sizeof(ID);
887                 cx->rc = cx->dbc->c_get( cx->dbc, &cx->key, &cx->data, DB_SET );
888                 if ( cx->rc ) {
889                         cx->dbc->c_close( cx->dbc );
890                         goto done_one;
891                 }
892
893                 /* If the on-disk count is zero we've never checked it.
894                  * Count it now.
895                  */
896                 if ( !dkids ) {
897                         cx->dbc->c_count( cx->dbc, &dkids, 0 );
898                         cx->ei->bei_dkids = dkids;
899                 }
900
901                 cx->data.data = cx->buf;
902                 cx->data.ulen = MDB_IDL_UM_SIZE * sizeof(ID);
903                 cx->data.flags = DB_DBT_USERMEM;
904
905                 if ( dkids > 1 ) {
906                         /* Fetch the rest of the IDs in a loop... */
907                         while ( (cx->rc = cx->dbc->c_get( cx->dbc, &cx->key, &cx->data,
908                                 DB_MULTIPLE | DB_NEXT_DUP )) == 0 ) {
909                                 u_int8_t *j;
910                                 size_t len;
911                                 void *ptr;
912                                 DB_MULTIPLE_INIT( ptr, &cx->data );
913                                 while (ptr) {
914                                         DB_MULTIPLE_NEXT( ptr, &cx->data, j, len );
915                                         if (j) {
916                                                 EntryInfo *ei2;
917                                                 diskNode *d = (diskNode *)j;
918                                                 short nrlen;
919
920                                                 MDB_DISK2ID( j + len - sizeof(ID), &ei.bei_id );
921                                                 nrlen = ((d->nrdnlen[0] ^ 0x80) << 8) | d->nrdnlen[1];
922                                                 ei.bei_nrdn.bv_len = nrlen;
923                                                 /* nrdn/rdn are set in-place.
924                                                  * mdb_cache_load will copy them as needed
925                                                  */
926                                                 ei.bei_nrdn.bv_val = d->nrdn;
927                                                 ei.bei_rdn.bv_len = len - sizeof(diskNode)
928                                                         - ei.bei_nrdn.bv_len;
929                                                 ei.bei_rdn.bv_val = d->nrdn + ei.bei_nrdn.bv_len + 1;
930                                                 mdb_idl_append_one( cx->tmp, ei.bei_id );
931                                                 mdb_cache_load( cx->mdb, &ei, &ei2 );
932                                         }
933                                 }
934                         }
935                 }
936
937                 cx->rc = cx->dbc->c_close( cx->dbc );
938 done_one:
939                 mdb_cache_entryinfo_lock( cx->ei );
940                 cx->ei->bei_state &= ~CACHE_ENTRY_ONELEVEL;
941                 mdb_cache_entryinfo_unlock( cx->ei );
942                 if ( cx->rc )
943                         return cx->rc;
944
945         } else {
946                 /* The in-memory cache is in sync with the on-disk data.
947                  * do we have any kids?
948                  */
949 synced:
950                 cx->rc = 0;
951                 if ( cx->ei->bei_ckids > 0 ) {
952                         /* Walk the kids tree; order is irrelevant since mdb_idl_sort
953                          * will sort it later.
954                          */
955                         avl_apply( cx->ei->bei_kids, apply_func,
956                                 cx->tmp, -1, AVL_POSTORDER );
957                 }
958                 mdb_cache_entryinfo_unlock( cx->ei );
959         }
960
961         if ( !MDB_IDL_IS_RANGE( cx->tmp ) && cx->tmp[0] > 3 )
962                 mdb_idl_sort( cx->tmp, cx->buf );
963         if ( cx->mdb->bi_idl_cache_max_size && !MDB_IDL_IS_ZERO( cx->tmp )) {
964                 char *ptr = ((char *)&cx->id)-1;
965                 cx->key.data = ptr;
966                 cx->key.size = sizeof(ID)+1;
967                 *ptr = DN_ONE_PREFIX;
968                 mdb_idl_cache_put( cx->mdb, cx->db, &cx->key, cx->tmp, cx->rc );
969         }
970
971 gotit:
972         if ( !MDB_IDL_IS_ZERO( cx->tmp )) {
973                 if ( cx->prefix == DN_SUBTREE_PREFIX ) {
974                         mdb_idl_append( cx->ids, cx->tmp );
975                         cx->need_sort = 1;
976                         if ( !(cx->ei->bei_state & CACHE_ENTRY_NO_GRANDKIDS)) {
977                                 ID *save, idcurs;
978                                 EntryInfo *ei = cx->ei;
979                                 int nokids = 1;
980                                 save = cx->op->o_tmpalloc( MDB_IDL_SIZEOF( cx->tmp ),
981                                         cx->op->o_tmpmemctx );
982                                 MDB_IDL_CPY( save, cx->tmp );
983
984                                 idcurs = 0;
985                                 cx->depth++;
986                                 for ( cx->id = mdb_idl_first( save, &idcurs );
987                                         cx->id != NOID;
988                                         cx->id = mdb_idl_next( save, &idcurs )) {
989                                         EntryInfo *ei2;
990                                         cx->ei = NULL;
991                                         if ( mdb_cache_find_id( cx->op, cx->txn, cx->id, &cx->ei,
992                                                 ID_NOENTRY, NULL ))
993                                                 continue;
994                                         if ( cx->ei ) {
995                                                 ei2 = cx->ei;
996                                                 if ( !( ei2->bei_state & CACHE_ENTRY_NO_KIDS )) {
997                                                         MDB_ID2DISK( cx->id, &cx->nid );
998                                                         mdb_dn2idl_internal( cx );
999                                                         if ( !MDB_IDL_IS_ZERO( cx->tmp ))
1000                                                                 nokids = 0;
1001                                                 }
1002                                                 mdb_cache_entryinfo_lock( ei2 );
1003                                                 ei2->bei_finders--;
1004                                                 mdb_cache_entryinfo_unlock( ei2 );
1005                                         }
1006                                 }
1007                                 cx->depth--;
1008                                 cx->op->o_tmpfree( save, cx->op->o_tmpmemctx );
1009                                 if ( nokids ) {
1010                                         mdb_cache_entryinfo_lock( ei );
1011                                         ei->bei_state |= CACHE_ENTRY_NO_GRANDKIDS;
1012                                         mdb_cache_entryinfo_unlock( ei );
1013                                 }
1014                         }
1015                         /* Make sure caller knows it had kids! */
1016                         cx->tmp[0]=1;
1017
1018                         cx->rc = 0;
1019                 } else {
1020                         MDB_IDL_CPY( cx->ids, cx->tmp );
1021                 }
1022         }
1023         return cx->rc;
1024 }
1025
1026 int
1027 mdb_dn2idl(
1028         Operation       *op,
1029         DB_TXN *txn,
1030         struct berval *ndn,
1031         EntryInfo       *ei,
1032         ID *ids,
1033         ID *stack )
1034 {
1035         struct mdb_info *mdb = (struct mdb_info *)op->o_bd->be_private;
1036         struct dn2id_cookie cx;
1037
1038         Debug( LDAP_DEBUG_TRACE, "=> mdb_dn2idl(\"%s\")\n",
1039                 ndn->bv_val, 0, 0 );
1040
1041 #ifndef MDB_MULTIPLE_SUFFIXES
1042         if ( op->ors_scope != LDAP_SCOPE_ONELEVEL && 
1043                 ( ei->bei_id == 0 ||
1044                 ( ei->bei_parent->bei_id == 0 && op->o_bd->be_suffix[0].bv_len )))
1045         {
1046                 MDB_IDL_ALL( mdb, ids );
1047                 return 0;
1048         }
1049 #endif
1050
1051         cx.id = ei->bei_id;
1052         MDB_ID2DISK( cx.id, &cx.nid );
1053         cx.ei = ei;
1054         cx.mdb = mdb;
1055         cx.db = cx.mdb->bi_dn2id->bdi_db;
1056         cx.prefix = (op->ors_scope == LDAP_SCOPE_ONELEVEL) ?
1057                 DN_ONE_PREFIX : DN_SUBTREE_PREFIX;
1058         cx.ids = ids;
1059         cx.tmp = stack;
1060         cx.buf = stack + MDB_IDL_UM_SIZE;
1061         cx.op = op;
1062         cx.txn = txn;
1063         cx.need_sort = 0;
1064         cx.depth = 0;
1065
1066         if ( cx.prefix == DN_SUBTREE_PREFIX ) {
1067                 ids[0] = 1;
1068                 ids[1] = cx.id;
1069         } else {
1070                 MDB_IDL_ZERO( ids );
1071         }
1072         if ( cx.ei->bei_state & CACHE_ENTRY_NO_KIDS )
1073                 return LDAP_SUCCESS;
1074
1075         DBTzero(&cx.key);
1076         cx.key.ulen = sizeof(ID);
1077         cx.key.size = sizeof(ID);
1078         cx.key.flags = DB_DBT_USERMEM;
1079
1080         DBTzero(&cx.data);
1081
1082         mdb_dn2idl_internal(&cx);
1083         if ( cx.need_sort ) {
1084                 char *ptr = ((char *)&cx.id)-1;
1085                 if ( !MDB_IDL_IS_RANGE( cx.ids ) && cx.ids[0] > 3 ) 
1086                         mdb_idl_sort( cx.ids, cx.tmp );
1087                 cx.key.data = ptr;
1088                 cx.key.size = sizeof(ID)+1;
1089                 *ptr = cx.prefix;
1090                 cx.id = ei->bei_id;
1091                 if ( cx.mdb->bi_idl_cache_max_size )
1092                         mdb_idl_cache_put( cx.mdb, cx.db, &cx.key, cx.ids, cx.rc );
1093         }
1094
1095         if ( cx.rc == DB_NOTFOUND )
1096                 cx.rc = LDAP_SUCCESS;
1097
1098         return cx.rc;
1099 }
1100 #endif