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