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-2015 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)
66 if (mdb->mi_maxentrysize && ec.len > mdb->mi_maxentrysize)
67 return LDAP_ADMINLIMIT_EXCEEDED;
70 data.mv_size = ec.len;
72 rc = mdb_cursor_put( mc, &key, &data, flag );
74 rc = mdb_put( txn, mdb->mi_id2entry, &key, &data, flag );
75 if (rc == MDB_SUCCESS) {
76 rc = mdb_entry_encode( op, e, &data, &ec );
77 if( rc != LDAP_SUCCESS )
81 /* Was there a hole from slapadd? */
82 if ( (flag & MDB_NOOVERWRITE) && data.mv_size == 0 ) {
86 Debug( LDAP_DEBUG_ANY,
87 "mdb_id2entry_put: mdb_put failed: %s(%d) \"%s\"\n",
90 if ( rc != MDB_KEYEXIST )
97 * This routine adds (or updates) an entry on disk.
98 * The cache should be already be updated.
102 int mdb_id2entry_add(
108 return mdb_id2entry_put(op, txn, mc, e, ADD_FLAGS);
111 int mdb_id2entry_update(
117 return mdb_id2entry_put(op, txn, mc, e, 0);
130 key.mv_size = sizeof(ID);
133 rc = mdb_cursor_get( mc, &key, data, MDB_SET );
134 /* stubs from missing parents - DB is actually invalid */
135 if ( rc == MDB_SUCCESS && !data->mv_size )
146 struct mdb_info *mdb = (struct mdb_info *) op->o_bd->be_private;
153 key.mv_size = sizeof(ID);
156 rc = mdb_cursor_get( mc, &key, &data, MDB_SET );
157 if ( rc == MDB_NOTFOUND ) {
158 /* Looking for root entry on an empty-dn suffix? */
159 if ( !id && BER_BVISEMPTY( &op->o_bd->be_nsuffix[0] )) {
160 struct berval gluebv = BER_BVC("glue");
161 Entry *r = mdb_entry_alloc(op, 2, 4);
162 Attribute *a = r->e_attrs;
166 r->e_ocflags = SLAP_OC_GLUE|SLAP_OC__END;
168 a->a_flags = SLAP_ATTR_DONT_FREE_DATA | SLAP_ATTR_DONT_FREE_VALS;
169 a->a_desc = slap_schema.si_ad_objectClass;
170 a->a_nvals = a->a_vals;
177 a->a_flags = SLAP_ATTR_DONT_FREE_DATA | SLAP_ATTR_DONT_FREE_VALS;
178 a->a_desc = slap_schema.si_ad_structuralObjectClass;
180 a->a_nvals = a->a_vals;
189 /* stubs from missing parents - DB is actually invalid */
190 if ( rc == MDB_SUCCESS && !data.mv_size )
194 rc = mdb_entry_decode( op, mdb_cursor_txn( mc ), &data, e );
198 (*e)->e_name.bv_val = NULL;
199 (*e)->e_nname.bv_val = NULL;
204 int mdb_id2entry_delete(
209 struct mdb_info *mdb = (struct mdb_info *) be->be_private;
210 MDB_dbi dbi = mdb->mi_id2entry;
214 key.mv_data = &e->e_id;
215 key.mv_size = sizeof(ID);
217 /* delete from database */
218 rc = mdb_del( tid, dbi, &key, NULL );
223 static Entry * mdb_entry_alloc(
228 Entry *e = op->o_tmpalloc( sizeof(Entry) +
229 nattrs * sizeof(Attribute) +
230 nvals * sizeof(struct berval), op->o_tmpmemctx );
231 BER_BVZERO(&e->e_bv);
234 e->e_attrs = (Attribute *)(e+1);
235 e->e_attrs->a_vals = (struct berval *)(e->e_attrs+nattrs);
243 int mdb_entry_return(
250 if ( e->e_private ) {
251 if ( op->o_hdr && op->o_tmpmfuncs ) {
252 op->o_tmpfree( e->e_nname.bv_val, op->o_tmpmemctx );
253 op->o_tmpfree( e->e_name.bv_val, op->o_tmpmemctx );
254 op->o_tmpfree( e, op->o_tmpmemctx );
256 ch_free( e->e_nname.bv_val );
257 ch_free( e->e_name.bv_val );
266 int mdb_entry_release(
271 struct mdb_info *mdb = (struct mdb_info *) op->o_bd->be_private;
272 struct mdb_op_info *moi = NULL;
275 /* slapMode : SLAP_SERVER_MODE, SLAP_TOOL_MODE,
276 SLAP_TRUNCATE_MODE, SLAP_UNDEFINED_MODE */
279 if ( slapMode & SLAP_SERVER_MODE ) {
281 LDAP_SLIST_FOREACH( oex, &op->o_extra, oe_next ) {
283 if ( oex->oe_key == mdb ) {
284 mdb_entry_return( op, e );
285 moi = (mdb_op_info *)oex;
286 /* If it was setup by entry_get we should probably free it */
287 if ( moi->moi_flag & MOI_FREEIT ) {
289 if ( moi->moi_ref < 1 ) {
290 mdb_txn_reset( moi->moi_txn );
292 LDAP_SLIST_REMOVE( &op->o_extra, &moi->moi_oe, OpExtra, oe_next );
293 op->o_tmpfree( moi, op->o_tmpmemctx );
302 mdb_entry_return( op, e );
307 /* return LDAP_SUCCESS IFF we can retrieve the specified entry.
313 AttributeDescription *at,
317 struct mdb_info *mdb = (struct mdb_info *) op->o_bd->be_private;
318 struct mdb_op_info *moi = NULL;
322 const char *at_name = at ? at->ad_cname.bv_val : "(null)";
324 Debug( LDAP_DEBUG_ARGS,
325 "=> mdb_entry_get: ndn: \"%s\"\n", ndn->bv_val, 0, 0 );
326 Debug( LDAP_DEBUG_ARGS,
327 "=> mdb_entry_get: oc: \"%s\", at: \"%s\"\n",
328 oc ? oc->soc_cname.bv_val : "(null)", at_name, 0);
330 rc = mdb_opinfo_get( op, mdb, rw == 0, &moi );
335 /* can we find entry */
336 rc = mdb_dn2entry( op, txn, NULL, ndn, &e, NULL, 0 );
342 return (rc != LDAP_BUSY) ? LDAP_OTHER : LDAP_BUSY;
345 Debug( LDAP_DEBUG_ACL,
346 "=> mdb_entry_get: cannot find entry: \"%s\"\n",
348 rc = LDAP_NO_SUCH_OBJECT;
352 Debug( LDAP_DEBUG_ACL,
353 "=> mdb_entry_get: found entry: \"%s\"\n",
356 if ( oc && !is_entry_objectclass( e, oc, 0 )) {
357 Debug( LDAP_DEBUG_ACL,
358 "<= mdb_entry_get: failed to find objectClass %s\n",
359 oc->soc_cname.bv_val, 0, 0 );
360 rc = LDAP_NO_SUCH_ATTRIBUTE;
364 /* NOTE: attr_find() or attrs_find()? */
365 if ( at && attr_find( e->e_attrs, at ) == NULL ) {
366 Debug( LDAP_DEBUG_ACL,
367 "<= mdb_entry_get: failed to find attribute %s\n",
368 at->ad_cname.bv_val, 0, 0 );
369 rc = LDAP_NO_SUCH_ATTRIBUTE;
374 if( rc != LDAP_SUCCESS ) {
376 mdb_entry_release( op, e, rw );
381 Debug( LDAP_DEBUG_TRACE,
382 "mdb_entry_get: rc=%d\n",
388 mdb_reader_free( void *key, void *data )
392 if ( txn ) mdb_txn_abort( txn );
395 /* free up any keys used by the main thread */
397 mdb_reader_flush( MDB_env *env )
400 void *ctx = ldap_pvt_thread_pool_context();
402 if ( !ldap_pvt_thread_pool_getkey( ctx, env, &data, NULL ) ) {
403 ldap_pvt_thread_pool_setkey( ctx, env, NULL, 0, NULL, NULL );
404 mdb_reader_free( env, data );
408 extern MDB_txn *mdb_tool_txn;
411 mdb_opinfo_get( Operation *op, struct mdb_info *mdb, int rdonly, mdb_op_info **moip )
416 mdb_op_info *moi = NULL;
419 assert( op != NULL );
421 if ( !mdb || !moip ) return -1;
423 /* If no op was provided, try to find the ctx anyway... */
425 ctx = op->o_threadctx;
427 ctx = ldap_pvt_thread_pool_context();
431 LDAP_SLIST_FOREACH( oex, &op->o_extra, oe_next ) {
432 if ( oex->oe_key == mdb ) break;
434 moi = (mdb_op_info *)oex;
442 moi = op->o_tmpalloc(sizeof(struct mdb_op_info),op->o_tmpmemctx);
444 moi = ch_malloc(sizeof(mdb_op_info));
446 moi->moi_flag = MOI_FREEIT;
449 LDAP_SLIST_INSERT_HEAD( &op->o_extra, &moi->moi_oe, oe_next );
450 moi->moi_oe.oe_key = mdb;
456 /* This op started as a reader, but now wants to write. */
457 if ( moi->moi_flag & MOI_READER ) {
459 LDAP_SLIST_INSERT_HEAD( &op->o_extra, &moi->moi_oe, oe_next );
461 /* This op is continuing an existing write txn */
465 if ( !moi->moi_txn ) {
466 if (( slapMode & SLAP_TOOL_MODE ) && mdb_tool_txn ) {
467 moi->moi_txn = mdb_tool_txn;
470 if ( get_lazyCommit( op ))
471 flag |= MDB_NOMETASYNC;
472 rc = mdb_txn_begin( mdb->mi_dbenv, NULL, flag, &moi->moi_txn );
474 Debug( LDAP_DEBUG_ANY, "mdb_opinfo_get: err %s(%d)\n",
475 mdb_strerror(rc), rc, 0 );
483 /* OK, this is a reader */
484 if ( !moi->moi_txn ) {
485 if (( slapMode & SLAP_TOOL_MODE ) && mdb_tool_txn ) {
486 moi->moi_txn = mdb_tool_txn;
490 /* Shouldn't happen unless we're single-threaded */
491 rc = mdb_txn_begin( mdb->mi_dbenv, NULL, MDB_RDONLY, &moi->moi_txn );
493 Debug( LDAP_DEBUG_ANY, "mdb_opinfo_get: err %s(%d)\n",
494 mdb_strerror(rc), rc, 0 );
498 if ( ldap_pvt_thread_pool_getkey( ctx, mdb->mi_dbenv, &data, NULL ) ) {
499 rc = mdb_txn_begin( mdb->mi_dbenv, NULL, MDB_RDONLY, &moi->moi_txn );
501 Debug( LDAP_DEBUG_ANY, "mdb_opinfo_get: err %s(%d)\n",
502 mdb_strerror(rc), rc, 0 );
506 if ( ( rc = ldap_pvt_thread_pool_setkey( ctx, mdb->mi_dbenv,
507 data, mdb_reader_free, NULL, NULL ) ) ) {
508 mdb_txn_abort( moi->moi_txn );
510 Debug( LDAP_DEBUG_ANY, "mdb_opinfo_get: thread_pool_setkey failed err (%d)\n",
518 moi->moi_flag |= MOI_READER;
521 if ( moi->moi_ref < 1 ) {
525 rc = mdb_txn_renew( moi->moi_txn );
536 int mdb_txn( Operation *op, int txnop, OpExtra **ptr )
538 struct mdb_info *mdb = (struct mdb_info *) op->o_bd->be_private;
539 mdb_op_info **moip = (mdb_op_info **)ptr, *moi = *moip;
544 return mdb_opinfo_get( op, mdb, 0, moip );
545 case SLAP_TXN_COMMIT:
546 rc = mdb_txn_commit( moi->moi_txn );
547 op->o_tmpfree( moi, op->o_tmpmemctx );
550 mdb_txn_abort( moi->moi_txn );
551 op->o_tmpfree( moi, op->o_tmpmemctx );
558 /* Count up the sizes of the components of an entry */
559 static int mdb_entry_partsize(struct mdb_info *mdb, MDB_txn *txn, Entry *e,
563 int i, nat = 0, nval = 0, nnval = 0;
566 len = 4*sizeof(int); /* nattrs, nvals, ocflags, offset */
567 for (a=e->e_attrs; a; a=a->a_next) {
568 /* For AttributeDesc, we only store the attr index */
570 if (a->a_desc->ad_index >= MDB_MAXADS) {
571 Debug( LDAP_DEBUG_ANY, "mdb_entry_partsize: too many AttributeDescriptions used\n",
575 if (!mdb->mi_adxs[a->a_desc->ad_index]) {
576 int rc = mdb_ad_get(mdb, txn, a->a_desc);
580 len += 2*sizeof(int); /* AD index, numvals */
581 nval += a->a_numvals + 1; /* empty berval at end */
582 for (i=0; i<a->a_numvals; i++) {
583 len += a->a_vals[i].bv_len + 1 + sizeof(int); /* len */
585 if (a->a_nvals != a->a_vals) {
586 nval += a->a_numvals + 1;
588 for (i=0; i<a->a_numvals; i++) {
589 len += a->a_nvals[i].bv_len + 1 + sizeof(int);;
594 len = (len + sizeof(ID)-1) & ~(sizeof(ID)-1);
598 eh->offset = nat + nval - nnval;
602 #define HIGH_BIT (1<<(sizeof(unsigned int)*CHAR_BIT-1))
604 /* Flatten an Entry into a buffer. The buffer starts with the count of the
605 * number of attributes in the entry, the total number of values in the
606 * entry, and the e_ocflags. It then contains a list of integers for each
607 * attribute. For each attribute the first integer gives the index of the
608 * matching AttributeDescription, followed by the number of values in the
609 * attribute. If the high bit of the attr index is set, the attribute's
610 * values are already sorted.
611 * If the high bit of numvals is set, the attribute also has normalized
612 * values present. (Note - a_numvals is an unsigned int, so this means
613 * it's possible to receive an attribute that we can't encode due to size
614 * overflow. In practice, this should not be an issue.) Then the length
615 * of each value is listed. If there are normalized values, their lengths
616 * come next. This continues for each attribute. After all of the lengths
617 * for the last attribute, the actual values are copied, with a NUL
618 * terminator after each value. The buffer is padded to the sizeof(ID).
619 * The entire buffer size is precomputed so that a single malloc can be
622 static int mdb_entry_encode(Operation *op, Entry *e, MDB_val *data, Ecount *eh)
624 struct mdb_info *mdb = (struct mdb_info *) op->o_bd->be_private;
631 Debug( LDAP_DEBUG_TRACE, "=> mdb_entry_encode(0x%08lx): %s\n",
632 (long) e->e_id, e->e_dn, 0 );
634 /* make sure e->e_ocflags is set */
635 if (is_entry_referral(e))
638 lp = (unsigned int *)data->mv_data;
641 *lp++ = (unsigned int)e->e_ocflags;
643 ptr = (unsigned char *)(lp + eh->offset);
645 for (a=e->e_attrs; a; a=a->a_next) {
646 if (!a->a_desc->ad_index)
647 return LDAP_UNDEFINED_TYPE;
648 l = mdb->mi_adxs[a->a_desc->ad_index];
649 if (a->a_flags & SLAP_ATTR_SORTED_VALS)
653 if (a->a_nvals != a->a_vals)
657 for (i=0; a->a_vals[i].bv_val; i++);
658 assert( i == a->a_numvals );
659 for (i=0; i<a->a_numvals; i++) {
660 *lp++ = a->a_vals[i].bv_len;
661 memcpy(ptr, a->a_vals[i].bv_val,
662 a->a_vals[i].bv_len);
663 ptr += a->a_vals[i].bv_len;
666 if (a->a_nvals != a->a_vals) {
667 for (i=0; i<a->a_numvals; i++) {
668 *lp++ = a->a_nvals[i].bv_len;
669 memcpy(ptr, a->a_nvals[i].bv_val,
670 a->a_nvals[i].bv_len);
671 ptr += a->a_nvals[i].bv_len;
678 Debug( LDAP_DEBUG_TRACE, "<= mdb_entry_encode(0x%08lx): %s\n",
679 (long) e->e_id, e->e_dn, 0 );
684 /* Retrieve an Entry that was stored using entry_encode above.
686 * Note: everything is stored in a single contiguous block, so
687 * you can not free individual attributes or names from this
688 * structure. Attempting to do so will likely corrupt memory.
691 int mdb_entry_decode(Operation *op, MDB_txn *txn, MDB_val *data, Entry **e)
693 struct mdb_info *mdb = (struct mdb_info *) op->o_bd->be_private;
694 int i, j, nattrs, nvals;
699 AttributeDescription *ad;
700 unsigned int *lp = (unsigned int *)data->mv_data;
704 Debug( LDAP_DEBUG_TRACE,
705 "=> mdb_entry_decode:\n",
710 x = mdb_entry_alloc(op, nattrs, nvals);
711 x->e_ocflags = *lp++;
718 ptr = (unsigned char *)(lp + i);
720 for (;nattrs>0; nattrs--) {
722 a->a_flags = SLAP_ATTR_DONT_FREE_DATA | SLAP_ATTR_DONT_FREE_VALS;
726 a->a_flags |= SLAP_ATTR_SORTED_VALS;
728 if (i > mdb->mi_numads) {
729 rc = mdb_ad_read(mdb, txn);
732 if (i > mdb->mi_numads) {
733 Debug( LDAP_DEBUG_ANY,
734 "mdb_entry_decode: attribute index %d not recognized\n",
739 a->a_desc = mdb->mi_ads[i];
740 a->a_numvals = *lp++;
741 if (a->a_numvals & HIGH_BIT) {
742 a->a_numvals ^= HIGH_BIT;
746 for (i=0; i<a->a_numvals; i++) {
747 bptr->bv_len = *lp++;;
748 bptr->bv_val = (char *)ptr;
749 ptr += bptr->bv_len+1;
758 for (i=0; i<a->a_numvals; i++) {
759 bptr->bv_len = *lp++;
760 bptr->bv_val = (char *)ptr;
761 ptr += bptr->bv_len+1;
768 a->a_nvals = a->a_vals;
770 /* FIXME: This is redundant once a sorted entry is saved into the DB */
771 if (( a->a_desc->ad_type->sat_flags & SLAP_AT_SORTED_VAL )
772 && !(a->a_flags & SLAP_ATTR_SORTED_VALS)) {
773 rc = slap_sort_vals( (Modifications *)a, &text, &j, NULL );
774 if ( rc == LDAP_SUCCESS ) {
775 a->a_flags |= SLAP_ATTR_SORTED_VALS;
776 } else if ( rc == LDAP_TYPE_OR_VALUE_EXISTS ) {
777 /* should never happen */
778 Debug( LDAP_DEBUG_ANY,
779 "mdb_entry_decode: attributeType %s value #%d provided more than once\n",
780 a->a_desc->ad_cname.bv_val, j, 0 );
790 Debug(LDAP_DEBUG_TRACE, "<= mdb_entry_decode\n",