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