1 /* dn2id.c - routines to deal with the dn2id index */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 2000-2013 The OpenLDAP Foundation.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted only as authorized by the OpenLDAP
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>.
20 #include <ac/string.h>
26 /* Management routines for a hierarchically structured database.
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
39 * Also each child node contains a count of the number of entries in
40 * its subtree, appended after its entryID.
42 * The diskNode is a variable length structure. This definition is not
43 * directly usable for in-memory manipulation.
45 typedef struct diskNode {
46 unsigned char nrdnlen[2];
48 char rdn[1]; /* variable placement */
49 unsigned char entryID[sizeof(ID)]; /* variable placement */
50 /* unsigned char nsubs[sizeof(ID)]; in child nodes only */
53 /* Sort function for the sorted duplicate data items of a dn2id key.
54 * Sorts based on normalized RDN, in length order.
58 const MDB_val *usrkey,
65 un = (diskNode *)usrkey->mv_data;
66 cn = (diskNode *)curkey->mv_data;
68 /* data is not aligned, cannot compare directly */
69 rc = un->nrdnlen[0] - cn->nrdnlen[0];
71 rc = un->nrdnlen[1] - cn->nrdnlen[1];
74 nrlen = ((un->nrdnlen[0] & 0x7f) << 8) | un->nrdnlen[1];
75 return strncmp( un->nrdn, cn->nrdn, nrlen );
78 /* We add two elements to the DN2ID database - a data item under the parent's
79 * entryID containing the child's RDN and entryID, and an item under the
80 * child's entryID containing the parent's entryID.
91 struct mdb_info *mdb = (struct mdb_info *) op->o_bd->be_private;
98 Debug( LDAP_DEBUG_TRACE, "=> mdb_dn2id_add 0x%lx: \"%s\"\n",
99 e->e_id, e->e_ndn ? e->e_ndn : "", 0 );
101 nrlen = dn_rdnlen( op->o_bd, &e->e_nname );
103 rlen = dn_rdnlen( op->o_bd, &e->e_name );
105 nrlen = e->e_nname.bv_len;
106 rlen = e->e_name.bv_len;
109 d = op->o_tmpalloc(sizeof(diskNode) + rlen + nrlen + sizeof(ID), op->o_tmpmemctx);
110 d->nrdnlen[1] = nrlen & 0xff;
111 d->nrdnlen[0] = (nrlen >> 8) | 0x80;
112 ptr = lutil_strncopy( d->nrdn, e->e_nname.bv_val, nrlen );
114 ptr = lutil_strncopy( ptr, e->e_name.bv_val, rlen );
116 memcpy( ptr, &e->e_id, sizeof( ID ));
118 memcpy( ptr, &nsubs, sizeof( ID ));
120 key.mv_size = sizeof(ID);
125 /* Need to make dummy root node once. Subsequent attempts
126 * will fail harmlessly.
129 diskNode dummy = {{0, 0}, "", "", ""};
130 data.mv_data = &dummy;
131 data.mv_size = sizeof(diskNode);
133 mdb_cursor_put( mcp, &key, &data, MDB_NODUPDATA );
137 data.mv_size = sizeof(diskNode) + rlen + nrlen + sizeof( ID );
139 /* Add our child node under parent's key */
140 rc = mdb_cursor_put( mcp, &key, &data, MDB_NODUPDATA );
142 /* Add our own node */
144 int flag = MDB_NODUPDATA;
146 /* drop subtree count */
147 data.mv_size -= sizeof( ID );
149 memcpy( ptr, &pid, sizeof( ID ));
150 d->nrdnlen[0] ^= 0x80;
152 if ((slapMode & SLAP_TOOL_MODE) || (e->e_id == mdb->mi_nextid))
154 rc = mdb_cursor_put( mcd, &key, &data, flag );
156 op->o_tmpfree( d, op->o_tmpmemctx );
158 /* Add our subtree count to all superiors */
159 if ( rc == 0 && nsubs && pid ) {
163 /* Get parent's RDN */
164 rc = mdb_cursor_get( mcp, &key, &data, MDB_SET );
167 ptr = data.mv_data + data.mv_size - sizeof( ID );
168 memcpy( &nid, ptr, sizeof( ID ));
169 /* Get parent's node under grandparent */
171 rlen = ( d->nrdnlen[0] << 8 ) | d->nrdnlen[1];
172 p2 = op->o_tmpalloc( rlen + 2, op->o_tmpmemctx );
173 memcpy( p2, data.mv_data, rlen+2 );
176 rc = mdb_cursor_get( mcp, &key, &data, MDB_GET_BOTH );
177 op->o_tmpfree( p2, op->o_tmpmemctx );
179 /* Get parent's subtree count */
180 ptr = data.mv_data + data.mv_size - sizeof( ID );
181 memcpy( &subs, ptr, sizeof( ID ));
183 p2 = op->o_tmpalloc( data.mv_size, op->o_tmpmemctx );
184 memcpy( p2, data.mv_data, data.mv_size - sizeof( ID ));
185 memcpy( p2+data.mv_size - sizeof( ID ), &subs, sizeof( ID ));
187 rc = mdb_cursor_put( mcp, &key, &data, MDB_CURRENT );
188 op->o_tmpfree( p2, op->o_tmpmemctx );
196 Debug( LDAP_DEBUG_TRACE, "<= mdb_dn2id_add 0x%lx: %d\n", e->e_id, rc, 0 );
201 /* mc must have been set by mdb_dn2id */
213 Debug( LDAP_DEBUG_TRACE, "=> mdb_dn2id_delete 0x%lx\n",
216 /* Delete our ID from the parent's list */
217 rc = mdb_cursor_del( mc, 0 );
219 /* Delete our ID from the tree. With sorted duplicates, this
220 * will leave any child nodes still hanging around. This is OK
221 * for modrdn, which will add our info back in later.
226 mdb_cursor_get( mc, &key, NULL, MDB_GET_CURRENT );
227 memcpy( &nid, key.mv_data, sizeof( ID ));
229 key.mv_size = sizeof(ID);
231 rc = mdb_cursor_get( mc, &key, &data, MDB_SET );
233 rc = mdb_cursor_del( mc, 0 );
236 /* Delete our subtree count from all superiors */
237 if ( rc == 0 && nsubs && nid ) {
241 key.mv_size = sizeof( ID );
243 rc = mdb_cursor_get( mc, &key, &data, MDB_SET );
248 ptr = data.mv_data + data.mv_size - sizeof( ID );
249 memcpy( &nid, ptr, sizeof( ID ));
250 /* Get parent's node under grandparent */
252 rlen = ( d->nrdnlen[0] << 8 ) | d->nrdnlen[1];
253 p2 = op->o_tmpalloc( rlen + 2, op->o_tmpmemctx );
254 memcpy( p2, data.mv_data, rlen+2 );
257 rc = mdb_cursor_get( mc, &key, &data, MDB_GET_BOTH );
258 op->o_tmpfree( p2, op->o_tmpmemctx );
260 /* Get parent's subtree count */
261 ptr = data.mv_data + data.mv_size - sizeof( ID );
262 memcpy( &subs, ptr, sizeof( ID ));
264 p2 = op->o_tmpalloc( data.mv_size, op->o_tmpmemctx );
265 memcpy( p2, data.mv_data, data.mv_size - sizeof( ID ));
266 memcpy( p2+data.mv_size - sizeof( ID ), &subs, sizeof( ID ));
268 rc = mdb_cursor_put( mc, &key, &data, MDB_CURRENT );
269 op->o_tmpfree( p2, op->o_tmpmemctx );
278 Debug( LDAP_DEBUG_TRACE, "<= mdb_dn2id_delete 0x%lx: %d\n", id, rc, 0 );
282 /* return last found ID in *id if no match
283 * If mc is provided, it will be left pointing to the RDN's
284 * record under the parent's ID. If nsubs is provided, return
285 * the number of entries in this entry's subtree.
295 struct berval *matched,
296 struct berval *nmatched )
298 struct mdb_info *mdb = (struct mdb_info *) op->o_bd->be_private;
300 MDB_dbi dbi = mdb->mi_dn2id;
305 char dn[SLAP_LDAPDN_MAXLEN];
309 Debug( LDAP_DEBUG_TRACE, "=> mdb_dn2id(\"%s\")\n", in->bv_val ? in->bv_val : "", 0, 0 );
312 matched->bv_val = dn + sizeof(dn) - 1;
314 *matched->bv_val-- = '\0';
317 nmatched->bv_len = 0;
318 nmatched->bv_val = 0;
329 if ( op->o_bd->be_nsuffix[0].bv_len ) {
330 nrlen = tmp.bv_len - op->o_bd->be_nsuffix[0].bv_len;
332 tmp.bv_len = op->o_bd->be_nsuffix[0].bv_len;
334 for ( ptr = tmp.bv_val + tmp.bv_len - 1; ptr >= tmp.bv_val; ptr-- )
335 if (DN_SEPARATOR(*ptr))
338 tmp.bv_len -= ptr - tmp.bv_val;
342 key.mv_size = sizeof(ID);
347 rc = mdb_cursor_open( txn, dbi, &cursor );
355 data.mv_size = sizeof(diskNode) + tmp.bv_len;
356 d = op->o_tmpalloc( data.mv_size, op->o_tmpmemctx );
357 d->nrdnlen[1] = tmp.bv_len & 0xff;
358 d->nrdnlen[0] = (tmp.bv_len >> 8) | 0x80;
359 ptr = lutil_strncopy( d->nrdn, tmp.bv_val, tmp.bv_len );
362 rc = mdb_cursor_get( cursor, &key, &data, MDB_GET_BOTH );
363 op->o_tmpfree( d, op->o_tmpmemctx );
366 ptr = (char *) data.mv_data + data.mv_size - 2*sizeof(ID);
367 memcpy( &nid, ptr, sizeof(ID));
369 /* grab the non-normalized RDN */
373 rlen = data.mv_size - sizeof(diskNode) - tmp.bv_len - sizeof(ID);
374 matched->bv_len += rlen;
375 matched->bv_val -= rlen + 1;
376 ptr = lutil_strcopy( matched->bv_val, d->rdn + tmp.bv_len );
383 nmatched->bv_val = tmp.bv_val;
386 if ( tmp.bv_val > in->bv_val ) {
387 for (ptr = tmp.bv_val - 2; ptr > in->bv_val &&
388 !DN_SEPARATOR(*ptr); ptr--) /* empty */;
389 if ( ptr >= in->bv_val ) {
390 if (DN_SEPARATOR(*ptr)) ptr++;
391 tmp.bv_len = tmp.bv_val - ptr - 1;
399 /* return subtree count if requested */
400 if ( !rc && nsubs ) {
401 ptr = data.mv_data + data.mv_size - sizeof(ID);
402 memcpy( nsubs, ptr, sizeof( ID ));
405 mdb_cursor_close( cursor );
408 if ( matched->bv_len ) {
409 ptr = op->o_tmpalloc( matched->bv_len+1, op->o_tmpmemctx );
410 strcpy( ptr, matched->bv_val );
411 matched->bv_val = ptr;
413 if ( BER_BVISEMPTY( &op->o_bd->be_nsuffix[0] ) && !nid ) {
414 ber_dupbv( matched, (struct berval *)&slap_empty_bv );
416 matched->bv_val = NULL;
421 if ( nmatched->bv_val ) {
422 nmatched->bv_len = in->bv_len - (nmatched->bv_val - in->bv_val);
424 *nmatched = slap_empty_bv;
429 Debug( LDAP_DEBUG_TRACE, "<= mdb_dn2id: get failed: %s (%d)\n",
430 mdb_strerror( rc ), rc, 0 );
432 Debug( LDAP_DEBUG_TRACE, "<= mdb_dn2id: got id=0x%lx\n",
439 /* return IDs from root to parent of DN */
447 struct mdb_info *mdb = (struct mdb_info *) op->o_bd->be_private;
449 MDB_dbi dbi = mdb->mi_dn2id;
457 Debug( LDAP_DEBUG_TRACE, "=> mdb_dn2sups(\"%s\")\n", in->bv_val, 0, 0 );
465 nrlen = tmp.bv_len - op->o_bd->be_nsuffix[0].bv_len;
467 tmp.bv_len = op->o_bd->be_nsuffix[0].bv_len;
469 key.mv_size = sizeof(ID);
471 rc = mdb_cursor_open( txn, dbi, &cursor );
478 data.mv_size = sizeof(diskNode) + tmp.bv_len;
479 d = op->o_tmpalloc( data.mv_size, op->o_tmpmemctx );
480 d->nrdnlen[1] = tmp.bv_len & 0xff;
481 d->nrdnlen[0] = (tmp.bv_len >> 8) | 0x80;
482 ptr = lutil_strncopy( d->nrdn, tmp.bv_val, tmp.bv_len );
485 rc = mdb_cursor_get( cursor, &key, &data, MDB_GET_BOTH );
486 op->o_tmpfree( d, op->o_tmpmemctx );
488 mdb_cursor_close( cursor );
491 ptr = (char *) data.mv_data + data.mv_size - 2*sizeof(ID);
492 memcpy( &nid, ptr, sizeof(ID));
495 mdb_idl_insert( ids, pid );
497 if ( tmp.bv_val > in->bv_val ) {
498 for (ptr = tmp.bv_val - 2; ptr > in->bv_val &&
499 !DN_SEPARATOR(*ptr); ptr--) /* empty */;
500 if ( ptr >= in->bv_val ) {
501 if (DN_SEPARATOR(*ptr)) ptr++;
502 tmp.bv_len = tmp.bv_val - ptr - 1;
512 Debug( LDAP_DEBUG_TRACE, "<= mdb_dn2sups: get failed: %s (%d)\n",
513 mdb_strerror( rc ), rc, 0 );
525 struct mdb_info *mdb = (struct mdb_info *) op->o_bd->be_private;
526 MDB_dbi dbi = mdb->mi_dn2id;
532 key.mv_size = sizeof(ID);
536 rc = mdb_cursor_open( txn, dbi, &cursor );
539 rc = mdb_cursor_get( cursor, &key, &data, MDB_SET );
542 rc = mdb_cursor_count( cursor, &dkids );
544 if ( dkids < 2 ) rc = MDB_NOTFOUND;
547 mdb_cursor_close( cursor );
558 struct berval *nname )
560 struct mdb_info *mdb = (struct mdb_info *) op->o_bd->be_private;
561 MDB_dbi dbi = mdb->mi_dn2id;
565 char dn[SLAP_LDAPDN_MAXLEN], ndn[SLAP_LDAPDN_MAXLEN], *ptr;
569 key.mv_size = sizeof(ID);
572 rc = mdb_cursor_open( txn, dbi, cursp );
582 unsigned int nrlen, rlen;
586 rc = mdb_cursor_get( cursor, &key, &data, MDB_SET );
589 ptr += data.mv_size - sizeof(ID);
590 memcpy( &id, ptr, sizeof(ID) );
592 nrlen = (d->nrdnlen[0] << 8) | d->nrdnlen[1];
593 rlen = data.mv_size - sizeof(diskNode) - nrlen;
594 assert( nrlen < 1024 && rlen < 1024 ); /* FIXME: Sanity check */
599 /* copy name and trailing NUL */
600 memcpy( nptr, d->nrdn, nrlen+1 );
601 memcpy( dptr, d->nrdn+nrlen+1, rlen+1 );
606 name->bv_len = dptr - dn;
607 nname->bv_len = nptr - ndn;
608 name->bv_val = op->o_tmpalloc( name->bv_len + 1, op->o_tmpmemctx );
609 nname->bv_val = op->o_tmpalloc( nname->bv_len + 1, op->o_tmpmemctx );
610 memcpy( name->bv_val, dn, name->bv_len );
611 name->bv_val[name->bv_len] = '\0';
612 memcpy( nname->bv_val, ndn, nname->bv_len );
613 nname->bv_val[nname->bv_len] = '\0';
618 /* Find each id in ids that is a child of base and move it to res.
628 struct mdb_info *mdb = (struct mdb_info *) op->o_bd->be_private;
629 MDB_dbi dbi = mdb->mi_dn2id;
632 ID ida, id, cid, ci0, idc = 0;
636 key.mv_size = sizeof(ID);
640 rc = mdb_cursor_open( txn, dbi, &cursor );
643 ida = mdb_idl_first( ids, &cid );
645 /* Don't bother moving out of ids if it's a range */
646 if (!MDB_IDL_IS_RANGE(ids)) {
651 while (ida != NOID) {
655 rc = mdb_cursor_get( cursor, &key, &data, MDB_SET );
657 /* not found, move on to next */
666 ptr += data.mv_size - sizeof(ID);
667 memcpy( &id, ptr, sizeof(ID) );
681 if ( op->ors_scope == LDAP_SCOPE_ONELEVEL )
684 ida = mdb_idl_next( ids, &cid );
686 if (!MDB_IDL_IS_RANGE( ids ))
689 mdb_cursor_close( cursor );
693 /* See if base is a child of any of the scopes
700 struct mdb_info *mdb = (struct mdb_info *) op->o_bd->be_private;
701 MDB_dbi dbi = mdb->mi_dn2id;
708 unsigned int nrlen, rlen;
711 key.mv_size = sizeof(ID);
714 rc = mdb_cursor_open( isc->mt, dbi, &isc->mc );
722 rc = mdb_cursor_get( isc->mc, &key, &data, MDB_SET );
729 nrlen = (d->nrdnlen[0] << 8) | d->nrdnlen[1];
730 rlen = data.mv_size - sizeof(diskNode) - nrlen;
731 isc->nrdns[isc->numrdns].bv_len = nrlen;
732 isc->nrdns[isc->numrdns].bv_val = d->nrdn;
733 isc->rdns[isc->numrdns].bv_len = rlen;
734 isc->rdns[isc->numrdns].bv_val = d->nrdn+nrlen+1;
737 if (!rc && id != isc->id) {
740 mdb_id2l_insert( isc->scopes, &id2 );
743 ptr += data.mv_size - sizeof(ID);
744 memcpy( &id, ptr, sizeof(ID) );
745 x = mdb_id2l_search( isc->scopes, id );
746 if ( x <= isc->scopes[0].mid && isc->scopes[x].mid == id ) {
747 if ( !isc->scopes[x].mval.mv_data ) {
751 data = isc->scopes[x].mval;
754 if ( op->ors_scope == LDAP_SCOPE_ONELEVEL )
772 if ( !isc->numrdns ) {
773 key.mv_data = &isc->id;
774 key.mv_size = sizeof(ID);
775 rc = mdb_cursor_get( isc->mc, &key, &data, MDB_SET );
776 isc->scopes[0].mid = isc->id;
779 /* skip base if not a subtree walk */
780 if ( op->ors_scope == LDAP_SCOPE_SUBTREE ||
781 op->ors_scope == LDAP_SCOPE_BASE )
784 if ( op->ors_scope == LDAP_SCOPE_BASE )
788 /* Get next sibling */
789 rc = mdb_cursor_get( isc->mc, &key, &data, MDB_NEXT_DUP );
791 ptr = data.mv_data + data.mv_size - 2*sizeof(ID);
793 memcpy( &isc->id, ptr, sizeof(ID));
795 /* If we're pushing down, see if there's any children to find */
798 memcpy( &nsubs, ptr, sizeof(ID));
799 /* No children, go to next sibling */
804 isc->scopes[n].mid = isc->id;
806 isc->nrdns[n].bv_len = ((d->nrdnlen[0] & 0x7f) << 8) | d->nrdnlen[1];
807 isc->nrdns[n].bv_val = d->nrdn;
808 isc->rdns[n].bv_val = d->nrdn+isc->nrdns[n].bv_len+1;
809 isc->rdns[n].bv_len = data.mv_size - sizeof(diskNode) - isc->nrdns[n].bv_len - sizeof(ID);
810 /* return this ID to caller */
814 /* push down to child */
815 key.mv_data = &isc->id;
816 mdb_cursor_get( isc->mc, &key, &data, MDB_SET );
821 } else if ( rc == MDB_NOTFOUND ) {
822 if ( !isc->nscope && op->ors_scope != LDAP_SCOPE_ONELEVEL ) {
823 /* reset to first dup */
824 mdb_cursor_get( isc->mc, &key, NULL, MDB_GET_CURRENT );
825 mdb_cursor_get( isc->mc, &key, &data, MDB_SET );
830 /* stack is empty? */
833 /* pop up to prev node */
834 n = isc->numrdns - 1;
835 key.mv_data = &isc->scopes[n].mid;
836 key.mv_size = sizeof(ID);
837 data.mv_data = isc->nrdns[n].bv_val - 2;
838 data.mv_size = 1; /* just needs to be non-zero, mdb_dup_compare doesn't care */
839 mdb_cursor_get( isc->mc, &key, &data, MDB_GET_BOTH );