1 /* id2entry.c - routines to deal with the id2entry database */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 2000-2014 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>
25 typedef struct Ecount {
32 static int mdb_entry_partsize(struct mdb_info *mdb, MDB_txn *txn, Entry *e,
34 static int mdb_entry_encode(Operation *op, Entry *e, MDB_val *data,
36 static Entry *mdb_entry_alloc( Operation *op, int nattrs, int nvals );
38 #define ADD_FLAGS (MDB_NOOVERWRITE|MDB_APPEND)
40 static int mdb_id2entry_put(
47 struct mdb_info *mdb = (struct mdb_info *) op->o_bd->be_private;
52 /* We only store rdns, and they go in the dn2id database. */
54 key.mv_data = &e->e_id;
55 key.mv_size = sizeof(ID);
57 rc = mdb_entry_partsize( mdb, txn, e, &ec );
63 if (e->e_id < mdb->mi_nextid)
67 data.mv_size = ec.len;
69 rc = mdb_cursor_put( mc, &key, &data, flag );
71 rc = mdb_put( txn, mdb->mi_id2entry, &key, &data, flag );
72 if (rc == MDB_SUCCESS) {
73 rc = mdb_entry_encode( op, e, &data, &ec );
74 if( rc != LDAP_SUCCESS )
78 /* Was there a hole from slapadd? */
79 if ( (flag & MDB_NOOVERWRITE) && data.mv_size == 0 ) {
83 Debug( LDAP_DEBUG_ANY,
84 "mdb_id2entry_put: mdb_put failed: %s(%d) \"%s\"\n",
87 if ( rc != MDB_KEYEXIST )
94 * This routine adds (or updates) an entry on disk.
95 * The cache should be already be updated.
105 return mdb_id2entry_put(op, txn, mc, e, ADD_FLAGS);
108 int mdb_id2entry_update(
114 return mdb_id2entry_put(op, txn, mc, e, 0);
127 key.mv_size = sizeof(ID);
130 rc = mdb_cursor_get( mc, &key, data, MDB_SET );
131 /* stubs from missing parents - DB is actually invalid */
132 if ( rc == MDB_SUCCESS && !data->mv_size )
143 struct mdb_info *mdb = (struct mdb_info *) op->o_bd->be_private;
150 key.mv_size = sizeof(ID);
153 rc = mdb_cursor_get( mc, &key, &data, MDB_SET );
154 if ( rc == MDB_NOTFOUND ) {
155 /* Looking for root entry on an empty-dn suffix? */
156 if ( !id && BER_BVISEMPTY( &op->o_bd->be_nsuffix[0] )) {
157 struct berval gluebv = BER_BVC("glue");
158 Entry *r = mdb_entry_alloc(op, 2, 4);
159 Attribute *a = r->e_attrs;
163 r->e_ocflags = SLAP_OC_GLUE|SLAP_OC__END;
165 a->a_flags = SLAP_ATTR_DONT_FREE_DATA | SLAP_ATTR_DONT_FREE_VALS;
166 a->a_desc = slap_schema.si_ad_objectClass;
167 a->a_nvals = a->a_vals;
174 a->a_flags = SLAP_ATTR_DONT_FREE_DATA | SLAP_ATTR_DONT_FREE_VALS;
175 a->a_desc = slap_schema.si_ad_structuralObjectClass;
177 a->a_nvals = a->a_vals;
186 /* stubs from missing parents - DB is actually invalid */
187 if ( rc == MDB_SUCCESS && !data.mv_size )
191 rc = mdb_entry_decode( op, mdb_cursor_txn( mc ), &data, e );
195 (*e)->e_name.bv_val = NULL;
196 (*e)->e_nname.bv_val = NULL;
201 int mdb_id2entry_delete(
206 struct mdb_info *mdb = (struct mdb_info *) be->be_private;
207 MDB_dbi dbi = mdb->mi_id2entry;
211 key.mv_data = &e->e_id;
212 key.mv_size = sizeof(ID);
214 /* delete from database */
215 rc = mdb_del( tid, dbi, &key, NULL );
220 static Entry * mdb_entry_alloc(
225 Entry *e = op->o_tmpalloc( sizeof(Entry) +
226 nattrs * sizeof(Attribute) +
227 nvals * sizeof(struct berval), op->o_tmpmemctx );
228 BER_BVZERO(&e->e_bv);
231 e->e_attrs = (Attribute *)(e+1);
232 e->e_attrs->a_vals = (struct berval *)(e->e_attrs+nattrs);
240 int mdb_entry_return(
247 if ( e->e_private ) {
248 if ( op->o_hdr && op->o_tmpmfuncs ) {
249 op->o_tmpfree( e->e_nname.bv_val, op->o_tmpmemctx );
250 op->o_tmpfree( e->e_name.bv_val, op->o_tmpmemctx );
251 op->o_tmpfree( e, op->o_tmpmemctx );
253 ch_free( e->e_nname.bv_val );
254 ch_free( e->e_name.bv_val );
263 int mdb_entry_release(
268 struct mdb_info *mdb = (struct mdb_info *) op->o_bd->be_private;
269 struct mdb_op_info *moi = NULL;
272 /* slapMode : SLAP_SERVER_MODE, SLAP_TOOL_MODE,
273 SLAP_TRUNCATE_MODE, SLAP_UNDEFINED_MODE */
275 if ( slapMode & SLAP_SERVER_MODE ) {
277 LDAP_SLIST_FOREACH( oex, &op->o_extra, oe_next ) {
278 if ( oex->oe_key == mdb ) {
279 mdb_entry_return( op, e );
280 moi = (mdb_op_info *)oex;
281 /* If it was setup by entry_get we should probably free it */
282 if ( moi->moi_flag & MOI_FREEIT ) {
284 if ( moi->moi_ref < 1 ) {
285 mdb_txn_reset( moi->moi_txn );
287 LDAP_SLIST_REMOVE( &op->o_extra, &moi->moi_oe, OpExtra, oe_next );
288 op->o_tmpfree( moi, op->o_tmpmemctx );
295 mdb_entry_return( op, e );
301 /* return LDAP_SUCCESS IFF we can retrieve the specified entry.
307 AttributeDescription *at,
311 struct mdb_info *mdb = (struct mdb_info *) op->o_bd->be_private;
312 struct mdb_op_info *moi = NULL;
316 const char *at_name = at ? at->ad_cname.bv_val : "(null)";
318 Debug( LDAP_DEBUG_ARGS,
319 "=> mdb_entry_get: ndn: \"%s\"\n", ndn->bv_val, 0, 0 );
320 Debug( LDAP_DEBUG_ARGS,
321 "=> mdb_entry_get: oc: \"%s\", at: \"%s\"\n",
322 oc ? oc->soc_cname.bv_val : "(null)", at_name, 0);
324 rc = mdb_opinfo_get( op, mdb, rw == 0, &moi );
329 /* can we find entry */
330 rc = mdb_dn2entry( op, txn, NULL, ndn, &e, NULL, 0 );
336 return (rc != LDAP_BUSY) ? LDAP_OTHER : LDAP_BUSY;
339 Debug( LDAP_DEBUG_ACL,
340 "=> mdb_entry_get: cannot find entry: \"%s\"\n",
342 rc = LDAP_NO_SUCH_OBJECT;
346 Debug( LDAP_DEBUG_ACL,
347 "=> mdb_entry_get: found entry: \"%s\"\n",
350 if ( oc && !is_entry_objectclass( e, oc, 0 )) {
351 Debug( LDAP_DEBUG_ACL,
352 "<= mdb_entry_get: failed to find objectClass %s\n",
353 oc->soc_cname.bv_val, 0, 0 );
354 rc = LDAP_NO_SUCH_ATTRIBUTE;
358 /* NOTE: attr_find() or attrs_find()? */
359 if ( at && attr_find( e->e_attrs, at ) == NULL ) {
360 Debug( LDAP_DEBUG_ACL,
361 "<= mdb_entry_get: failed to find attribute %s\n",
362 at->ad_cname.bv_val, 0, 0 );
363 rc = LDAP_NO_SUCH_ATTRIBUTE;
368 if( rc != LDAP_SUCCESS ) {
370 mdb_entry_release( op, e, rw );
375 Debug( LDAP_DEBUG_TRACE,
376 "mdb_entry_get: rc=%d\n",
382 mdb_reader_free( void *key, void *data )
386 if ( txn ) mdb_txn_abort( txn );
389 /* free up any keys used by the main thread */
391 mdb_reader_flush( MDB_env *env )
394 void *ctx = ldap_pvt_thread_pool_context();
396 if ( !ldap_pvt_thread_pool_getkey( ctx, env, &data, NULL ) ) {
397 ldap_pvt_thread_pool_setkey( ctx, env, NULL, 0, NULL, NULL );
398 mdb_reader_free( env, data );
403 mdb_opinfo_get( Operation *op, struct mdb_info *mdb, int rdonly, mdb_op_info **moip )
408 mdb_op_info *moi = NULL;
411 assert( op != NULL );
413 if ( !mdb || !moip ) return -1;
415 /* If no op was provided, try to find the ctx anyway... */
417 ctx = op->o_threadctx;
419 ctx = ldap_pvt_thread_pool_context();
423 LDAP_SLIST_FOREACH( oex, &op->o_extra, oe_next ) {
424 if ( oex->oe_key == mdb ) break;
426 moi = (mdb_op_info *)oex;
434 moi = op->o_tmpalloc(sizeof(struct mdb_op_info),op->o_tmpmemctx);
436 moi = ch_malloc(sizeof(mdb_op_info));
438 moi->moi_flag = MOI_FREEIT;
441 LDAP_SLIST_INSERT_HEAD( &op->o_extra, &moi->moi_oe, oe_next );
442 moi->moi_oe.oe_key = mdb;
448 /* This op started as a reader, but now wants to write. */
449 if ( moi->moi_flag & MOI_READER ) {
451 LDAP_SLIST_INSERT_HEAD( &op->o_extra, &moi->moi_oe, oe_next );
453 /* This op is continuing an existing write txn */
457 if ( !moi->moi_txn ) {
458 rc = mdb_txn_begin( mdb->mi_dbenv, NULL, 0, &moi->moi_txn );
460 Debug( LDAP_DEBUG_ANY, "mdb_opinfo_get: err %s(%d)\n",
461 mdb_strerror(rc), rc, 0 );
468 /* OK, this is a reader */
469 if ( !moi->moi_txn ) {
471 /* Shouldn't happen unless we're single-threaded */
472 rc = mdb_txn_begin( mdb->mi_dbenv, NULL, MDB_RDONLY, &moi->moi_txn );
474 Debug( LDAP_DEBUG_ANY, "mdb_opinfo_get: err %s(%d)\n",
475 mdb_strerror(rc), rc, 0 );
479 if ( ldap_pvt_thread_pool_getkey( ctx, mdb->mi_dbenv, &data, NULL ) ) {
480 rc = mdb_txn_begin( mdb->mi_dbenv, NULL, MDB_RDONLY, &moi->moi_txn );
482 Debug( LDAP_DEBUG_ANY, "mdb_opinfo_get: err %s(%d)\n",
483 mdb_strerror(rc), rc, 0 );
487 if ( ( rc = ldap_pvt_thread_pool_setkey( ctx, mdb->mi_dbenv,
488 data, mdb_reader_free, NULL, NULL ) ) ) {
489 mdb_txn_abort( moi->moi_txn );
491 Debug( LDAP_DEBUG_ANY, "mdb_opinfo_get: thread_pool_setkey failed err (%d)\n",
499 moi->moi_flag |= MOI_READER;
501 if ( moi->moi_ref < 1 ) {
505 rc = mdb_txn_renew( moi->moi_txn );
515 /* Count up the sizes of the components of an entry */
516 static int mdb_entry_partsize(struct mdb_info *mdb, MDB_txn *txn, Entry *e,
520 int i, nat = 0, nval = 0, nnval = 0;
523 len = 4*sizeof(int); /* nattrs, nvals, ocflags, offset */
524 for (a=e->e_attrs; a; a=a->a_next) {
525 /* For AttributeDesc, we only store the attr index */
527 if (a->a_desc->ad_index >= MDB_MAXADS) {
528 Debug( LDAP_DEBUG_ANY, "mdb_entry_partsize: too many AttributeDescriptions used\n",
532 if (!mdb->mi_adxs[a->a_desc->ad_index]) {
533 int rc = mdb_ad_get(mdb, txn, a->a_desc);
537 len += 2*sizeof(int); /* AD index, numvals */
538 nval += a->a_numvals + 1; /* empty berval at end */
539 for (i=0; i<a->a_numvals; i++) {
540 len += a->a_vals[i].bv_len + 1 + sizeof(int); /* len */
542 if (a->a_nvals != a->a_vals) {
543 nval += a->a_numvals + 1;
545 for (i=0; i<a->a_numvals; i++) {
546 len += a->a_nvals[i].bv_len + 1 + sizeof(int);;
551 len = (len + sizeof(ID)-1) & ~(sizeof(ID)-1);
555 eh->offset = nat + nval - nnval;
559 #define HIGH_BIT (1<<(sizeof(unsigned int)*CHAR_BIT-1))
561 /* Flatten an Entry into a buffer. The buffer starts with the count of the
562 * number of attributes in the entry, the total number of values in the
563 * entry, and the e_ocflags. It then contains a list of integers for each
564 * attribute. For each attribute the first integer gives the index of the
565 * matching AttributeDescription, followed by the number of values in the
566 * attribute. If the high bit of the attr index is set, the attribute's
567 * values are already sorted.
568 * If the high bit of numvals is set, the attribute also has normalized
569 * values present. (Note - a_numvals is an unsigned int, so this means
570 * it's possible to receive an attribute that we can't encode due to size
571 * overflow. In practice, this should not be an issue.) Then the length
572 * of each value is listed. If there are normalized values, their lengths
573 * come next. This continues for each attribute. After all of the lengths
574 * for the last attribute, the actual values are copied, with a NUL
575 * terminator after each value. The buffer is padded to the sizeof(ID).
576 * The entire buffer size is precomputed so that a single malloc can be
579 static int mdb_entry_encode(Operation *op, Entry *e, MDB_val *data, Ecount *eh)
581 struct mdb_info *mdb = (struct mdb_info *) op->o_bd->be_private;
588 Debug( LDAP_DEBUG_TRACE, "=> mdb_entry_encode(0x%08lx): %s\n",
589 (long) e->e_id, e->e_dn, 0 );
591 /* make sure e->e_ocflags is set */
592 if (is_entry_referral(e))
595 lp = (unsigned int *)data->mv_data;
598 *lp++ = (unsigned int)e->e_ocflags;
600 ptr = (unsigned char *)(lp + eh->offset);
602 for (a=e->e_attrs; a; a=a->a_next) {
603 if (!a->a_desc->ad_index)
604 return LDAP_UNDEFINED_TYPE;
605 l = mdb->mi_adxs[a->a_desc->ad_index];
606 if (a->a_flags & SLAP_ATTR_SORTED_VALS)
610 if (a->a_nvals != a->a_vals)
614 for (i=0; a->a_vals[i].bv_val; i++);
615 assert( i == a->a_numvals );
616 for (i=0; i<a->a_numvals; i++) {
617 *lp++ = a->a_vals[i].bv_len;
618 memcpy(ptr, a->a_vals[i].bv_val,
619 a->a_vals[i].bv_len);
620 ptr += a->a_vals[i].bv_len;
623 if (a->a_nvals != a->a_vals) {
624 for (i=0; i<a->a_numvals; i++) {
625 *lp++ = a->a_nvals[i].bv_len;
626 memcpy(ptr, a->a_nvals[i].bv_val,
627 a->a_nvals[i].bv_len);
628 ptr += a->a_nvals[i].bv_len;
635 Debug( LDAP_DEBUG_TRACE, "<= mdb_entry_encode(0x%08lx): %s\n",
636 (long) e->e_id, e->e_dn, 0 );
641 /* Retrieve an Entry that was stored using entry_encode above.
643 * Note: everything is stored in a single contiguous block, so
644 * you can not free individual attributes or names from this
645 * structure. Attempting to do so will likely corrupt memory.
648 int mdb_entry_decode(Operation *op, MDB_txn *txn, MDB_val *data, Entry **e)
650 struct mdb_info *mdb = (struct mdb_info *) op->o_bd->be_private;
651 int i, j, nattrs, nvals;
656 AttributeDescription *ad;
657 unsigned int *lp = (unsigned int *)data->mv_data;
661 Debug( LDAP_DEBUG_TRACE,
662 "=> mdb_entry_decode:\n",
667 x = mdb_entry_alloc(op, nattrs, nvals);
668 x->e_ocflags = *lp++;
675 ptr = (unsigned char *)(lp + i);
677 for (;nattrs>0; nattrs--) {
679 a->a_flags = SLAP_ATTR_DONT_FREE_DATA | SLAP_ATTR_DONT_FREE_VALS;
683 a->a_flags |= SLAP_ATTR_SORTED_VALS;
685 if (i > mdb->mi_numads) {
686 rc = mdb_ad_read(mdb, txn);
689 if (i > mdb->mi_numads) {
690 Debug( LDAP_DEBUG_ANY,
691 "mdb_entry_decode: attribute index %d not recognized\n",
696 a->a_desc = mdb->mi_ads[i];
697 a->a_numvals = *lp++;
698 if (a->a_numvals & HIGH_BIT) {
699 a->a_numvals ^= HIGH_BIT;
703 for (i=0; i<a->a_numvals; i++) {
704 bptr->bv_len = *lp++;;
705 bptr->bv_val = (char *)ptr;
706 ptr += bptr->bv_len+1;
715 for (i=0; i<a->a_numvals; i++) {
716 bptr->bv_len = *lp++;
717 bptr->bv_val = (char *)ptr;
718 ptr += bptr->bv_len+1;
725 a->a_nvals = a->a_vals;
727 /* FIXME: This is redundant once a sorted entry is saved into the DB */
728 if (( a->a_desc->ad_type->sat_flags & SLAP_AT_SORTED_VAL )
729 && !(a->a_flags & SLAP_ATTR_SORTED_VALS)) {
730 rc = slap_sort_vals( (Modifications *)a, &text, &j, NULL );
731 if ( rc == LDAP_SUCCESS ) {
732 a->a_flags |= SLAP_ATTR_SORTED_VALS;
733 } else if ( rc == LDAP_TYPE_OR_VALUE_EXISTS ) {
734 /* should never happen */
735 Debug( LDAP_DEBUG_ANY,
736 "mdb_entry_decode: attributeType %s value #%d provided more than once\n",
737 a->a_desc->ad_cname.bv_val, j, 0 );
747 Debug(LDAP_DEBUG_TRACE, "<= mdb_entry_decode\n",