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-2011 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, MDB_txn *txn, Entry *e, MDB_val *data,
36 static Entry *mdb_entry_alloc( Operation *op, int nattrs, int nvals );
38 static int mdb_id2entry_put(
44 struct mdb_info *mdb = (struct mdb_info *) op->o_bd->be_private;
45 MDB_dbi dbi = mdb->mi_id2entry;
50 /* We only store rdns, and they go in the dn2id database. */
52 key.mv_data = &e->e_id;
53 key.mv_size = sizeof(ID);
55 rc = mdb_entry_partsize( mdb, tid, e, &ec );
62 data.mv_size = ec.len;
63 rc = mdb_put( tid, dbi, &key, &data, flag );
64 if (rc == MDB_SUCCESS) {
65 rc = mdb_entry_encode( op, tid, e, &data, &ec );
66 if( rc != LDAP_SUCCESS )
70 /* Was there a hole from slapadd? */
71 if ( (flag & MDB_NOOVERWRITE) && data.mv_size == 0 ) {
72 flag ^= MDB_NOOVERWRITE;
75 Debug( LDAP_DEBUG_ANY,
76 "mdb_id2entry_put: mdb_put failed: %s(%d) \"%s\"\n",
85 * This routine adds (or updates) an entry on disk.
86 * The cache should be already be updated.
95 return mdb_id2entry_put(op, tid, e, MDB_NOOVERWRITE);
98 int mdb_id2entry_update(
103 return mdb_id2entry_put(op, tid, e, 0);
112 struct mdb_info *mdb = (struct mdb_info *) op->o_bd->be_private;
119 key.mv_size = sizeof(ID);
122 rc = mdb_cursor_get( mc, &key, &data, MDB_SET );
123 if ( rc == MDB_NOTFOUND ) {
124 /* Looking for root entry on an empty-dn suffix? */
125 if ( !id && BER_BVISEMPTY( &op->o_bd->be_nsuffix[0] )) {
126 struct berval gluebv = BER_BVC("glue");
127 Entry *r = mdb_entry_alloc(op, 2, 4);
128 Attribute *a = r->e_attrs;
132 r->e_ocflags = SLAP_OC_GLUE|SLAP_OC__END;
134 a->a_flags = SLAP_ATTR_DONT_FREE_DATA | SLAP_ATTR_DONT_FREE_VALS;
135 a->a_desc = slap_schema.si_ad_objectClass;
136 a->a_nvals = a->a_vals;
143 a->a_flags = SLAP_ATTR_DONT_FREE_DATA | SLAP_ATTR_DONT_FREE_VALS;
144 a->a_desc = slap_schema.si_ad_structuralObjectClass;
146 a->a_nvals = a->a_vals;
157 rc = mdb_entry_decode( op, &data, e );
161 (*e)->e_name.bv_val = NULL;
162 (*e)->e_nname.bv_val = NULL;
167 int mdb_id2entry_delete(
172 struct mdb_info *mdb = (struct mdb_info *) be->be_private;
173 MDB_dbi dbi = mdb->mi_id2entry;
177 key.mv_data = &e->e_id;
178 key.mv_size = sizeof(ID);
180 /* delete from database */
181 rc = mdb_del( tid, dbi, &key, NULL );
186 static Entry * mdb_entry_alloc(
191 Entry *e = op->o_tmpalloc( sizeof(Entry) +
192 nattrs * sizeof(Attribute) +
193 nvals * sizeof(struct berval), op->o_tmpmemctx );
194 BER_BVZERO(&e->e_bv);
197 e->e_attrs = (Attribute *)(e+1);
198 e->e_attrs->a_vals = (struct berval *)(e->e_attrs+nattrs);
206 int mdb_entry_return(
211 if ( e->e_private ) {
212 if ( slapMode & SLAP_TOOL_MODE ) {
213 ch_free( e->e_nname.bv_val );
214 ch_free( e->e_name.bv_val );
217 op->o_tmpfree( e->e_nname.bv_val, op->o_tmpmemctx );
218 op->o_tmpfree( e->e_name.bv_val, op->o_tmpmemctx );
219 op->o_tmpfree( e, op->o_tmpmemctx );
227 int mdb_entry_release(
232 struct mdb_info *mdb = (struct mdb_info *) op->o_bd->be_private;
233 struct mdb_op_info *moi = NULL;
236 /* slapMode : SLAP_SERVER_MODE, SLAP_TOOL_MODE,
237 SLAP_TRUNCATE_MODE, SLAP_UNDEFINED_MODE */
239 mdb_entry_return( op, e );
240 if ( slapMode == SLAP_SERVER_MODE ) {
242 LDAP_SLIST_FOREACH( oex, &op->o_extra, oe_next ) {
243 if ( oex->oe_key == mdb ) {
244 moi = (mdb_op_info *)oex;
245 /* If it was setup by entry_get we should probably free it */
246 if ( moi->moi_flag & MOI_FREEIT ) {
248 if ( moi->moi_ref < 1 ) {
249 mdb_txn_reset( moi->moi_txn );
251 LDAP_SLIST_REMOVE( &op->o_extra, &moi->moi_oe, OpExtra, oe_next );
252 op->o_tmpfree( moi, op->o_tmpmemctx );
263 /* return LDAP_SUCCESS IFF we can retrieve the specified entry.
269 AttributeDescription *at,
273 struct mdb_info *mdb = (struct mdb_info *) op->o_bd->be_private;
274 struct mdb_op_info *moi = NULL;
278 const char *at_name = at ? at->ad_cname.bv_val : "(null)";
280 Debug( LDAP_DEBUG_ARGS,
281 "=> mdb_entry_get: ndn: \"%s\"\n", ndn->bv_val, 0, 0 );
282 Debug( LDAP_DEBUG_ARGS,
283 "=> mdb_entry_get: oc: \"%s\", at: \"%s\"\n",
284 oc ? oc->soc_cname.bv_val : "(null)", at_name, 0);
286 rc = mdb_opinfo_get( op, mdb, rw == 0, &moi );
291 /* can we find entry */
292 rc = mdb_dn2entry( op, txn, ndn, &e, 0 );
298 return (rc != LDAP_BUSY) ? LDAP_OTHER : LDAP_BUSY;
301 Debug( LDAP_DEBUG_ACL,
302 "=> mdb_entry_get: cannot find entry: \"%s\"\n",
304 rc = LDAP_NO_SUCH_OBJECT;
308 Debug( LDAP_DEBUG_ACL,
309 "=> mdb_entry_get: found entry: \"%s\"\n",
312 if ( oc && !is_entry_objectclass( e, oc, 0 )) {
313 Debug( LDAP_DEBUG_ACL,
314 "<= mdb_entry_get: failed to find objectClass %s\n",
315 oc->soc_cname.bv_val, 0, 0 );
316 rc = LDAP_NO_SUCH_ATTRIBUTE;
320 /* NOTE: attr_find() or attrs_find()? */
321 if ( at && attr_find( e->e_attrs, at ) == NULL ) {
322 Debug( LDAP_DEBUG_ACL,
323 "<= mdb_entry_get: failed to find attribute %s\n",
324 at->ad_cname.bv_val, 0, 0 );
325 rc = LDAP_NO_SUCH_ATTRIBUTE;
330 if( rc != LDAP_SUCCESS ) {
333 mdb_entry_return( op, e );
335 if (moi->moi_ref == 1) {
336 LDAP_SLIST_REMOVE( &op->o_extra, &moi->moi_oe, OpExtra, oe_next );
337 mdb_txn_reset( txn );
338 op->o_tmpfree( moi, op->o_tmpmemctx );
344 Debug( LDAP_DEBUG_TRACE,
345 "mdb_entry_get: rc=%d\n",
351 mdb_reader_free( void *key, void *data )
355 if ( txn ) mdb_txn_abort( txn );
358 /* free up any keys used by the main thread */
360 mdb_reader_flush( MDB_env *env )
363 void *ctx = ldap_pvt_thread_pool_context();
365 if ( !ldap_pvt_thread_pool_getkey( ctx, env, &data, NULL ) ) {
366 ldap_pvt_thread_pool_setkey( ctx, env, NULL, 0, NULL, NULL );
367 mdb_reader_free( env, data );
372 mdb_opinfo_get( Operation *op, struct mdb_info *mdb, int rdonly, mdb_op_info **moip )
377 mdb_op_info *moi = NULL;
380 assert( op != NULL );
382 if ( !mdb || !moip ) return -1;
384 /* If no op was provided, try to find the ctx anyway... */
386 ctx = op->o_threadctx;
388 ctx = ldap_pvt_thread_pool_context();
392 LDAP_SLIST_FOREACH( oex, &op->o_extra, oe_next ) {
393 if ( oex->oe_key == mdb ) break;
395 moi = (mdb_op_info *)oex;
403 moi = op->o_tmpalloc(sizeof(struct mdb_op_info),op->o_tmpmemctx);
405 moi = ch_malloc(sizeof(mdb_op_info));
407 moi->moi_flag = MOI_FREEIT;
410 LDAP_SLIST_INSERT_HEAD( &op->o_extra, &moi->moi_oe, oe_next );
411 moi->moi_oe.oe_key = mdb;
417 /* This op started as a reader, but now wants to write. */
418 if ( moi->moi_flag & MOI_READER ) {
420 LDAP_SLIST_INSERT_HEAD( &op->o_extra, &moi->moi_oe, oe_next );
422 /* This op is continuing an existing write txn */
426 if ( !moi->moi_txn ) {
427 rc = mdb_txn_begin( mdb->mi_dbenv, NULL, 0, &moi->moi_txn );
429 Debug( LDAP_DEBUG_ANY, "mdb_opinfo_get: err %s(%d)\n",
430 mdb_strerror(rc), rc, 0 );
437 /* OK, this is a reader */
438 if ( !moi->moi_txn ) {
440 /* Shouldn't happen unless we're single-threaded */
441 rc = mdb_txn_begin( mdb->mi_dbenv, NULL, MDB_RDONLY, &moi->moi_txn );
443 Debug( LDAP_DEBUG_ANY, "mdb_opinfo_get: err %s(%d)\n",
444 mdb_strerror(rc), rc, 0 );
448 if ( ldap_pvt_thread_pool_getkey( ctx, mdb->mi_dbenv, &data, NULL ) ) {
449 rc = mdb_txn_begin( mdb->mi_dbenv, NULL, MDB_RDONLY, &moi->moi_txn );
451 Debug( LDAP_DEBUG_ANY, "mdb_opinfo_get: err %s(%d)\n",
452 mdb_strerror(rc), rc, 0 );
456 if ( ( rc = ldap_pvt_thread_pool_setkey( ctx, mdb->mi_dbenv,
457 data, mdb_reader_free, NULL, NULL ) ) ) {
458 mdb_txn_abort( moi->moi_txn );
460 Debug( LDAP_DEBUG_ANY, "mdb_opinfo_get: thread_pool_setkey failed err (%d)\n",
468 moi->moi_flag |= MOI_READER;
470 if ( moi->moi_ref < 1 ) {
474 mdb_txn_renew( moi->moi_txn );
483 /* Count up the sizes of the components of an entry */
484 static int mdb_entry_partsize(struct mdb_info *mdb, MDB_txn *txn, Entry *e,
488 int i, nat = 0, nval = 0, nnval = 0;
491 len = 4*sizeof(int); /* nattrs, nvals, ocflags, offset */
492 for (a=e->e_attrs; a; a=a->a_next) {
493 /* For AttributeDesc, we only store the attr index */
495 if (!mdb->mi_adxs[a->a_desc->ad_index]) {
496 int rc = mdb_ad_get(mdb, txn, a->a_desc);
500 len += 2*sizeof(int); /* AD index, numvals */
501 nval += a->a_numvals + 1; /* empty berval at end */
502 for (i=0; i<a->a_numvals; i++) {
503 len += a->a_vals[i].bv_len + 1 + sizeof(int); /* len */
505 if (a->a_nvals != a->a_vals) {
506 nval += a->a_numvals + 1;
508 for (i=0; i<a->a_numvals; i++) {
509 len += a->a_nvals[i].bv_len + 1 + sizeof(int);;
514 len = (len + sizeof(ID)-1) & ~(sizeof(ID)-1);
518 eh->offset = nat + nval - nnval;
522 #define HIGH_BIT (1<<(sizeof(unsigned int)*CHAR_BIT-1))
524 /* Flatten an Entry into a buffer. The buffer starts with the count of the
525 * number of attributes in the entry, the total number of values in the
526 * entry, and the e_ocflags. It then contains a list of integers for each
527 * attribute. For each attribute the first integer gives the index of the
528 * matching AttributeDescription, followed by the number of values in the
529 * attribute. If the high bit is set, the attribute also has normalized
530 * values present. (Note - a_numvals is an unsigned int, so this means
531 * it's possible to receive an attribute that we can't encode due to size
532 * overflow. In practice, this should not be an issue.) Then the length
533 * of each value is listed. If there are normalized values, their lengths
534 * come next. This continues for each attribute. After all of the lengths
535 * for the last attribute, the actual values are copied, with a NUL
536 * terminator after each value. The buffer is padded to the sizeof(ID).
537 * The entire buffer size is precomputed so that a single malloc can be
540 static int mdb_entry_encode(Operation *op, MDB_txn *txn, Entry *e, MDB_val *data, Ecount *eh)
542 struct mdb_info *mdb = (struct mdb_info *) op->o_bd->be_private;
549 Debug( LDAP_DEBUG_TRACE, "=> mdb_entry_encode(0x%08lx): %s\n",
550 (long) e->e_id, e->e_dn, 0 );
552 /* make sure e->e_ocflags is set */
553 if (is_entry_referral(e))
556 lp = (unsigned int *)data->mv_data;
559 *lp++ = (unsigned int)e->e_ocflags;
561 ptr = (unsigned char *)(lp + eh->offset);
563 for (a=e->e_attrs; a; a=a->a_next) {
564 *lp++ = mdb->mi_adxs[a->a_desc->ad_index];
566 if (a->a_nvals != a->a_vals)
570 for (i=0; a->a_vals[i].bv_val; i++);
571 assert( i == a->a_numvals );
572 for (i=0; i<a->a_numvals; i++) {
573 *lp++ = a->a_vals[i].bv_len;
574 memcpy(ptr, a->a_vals[i].bv_val,
575 a->a_vals[i].bv_len);
576 ptr += a->a_vals[i].bv_len;
579 if (a->a_nvals != a->a_vals) {
580 for (i=0; i<a->a_numvals; i++) {
581 *lp++ = a->a_nvals[i].bv_len;
582 memcpy(ptr, a->a_nvals[i].bv_val,
583 a->a_nvals[i].bv_len);
584 ptr += a->a_nvals[i].bv_len;
591 Debug( LDAP_DEBUG_TRACE, "<= mdb_entry_encode(0x%08lx): %s\n",
592 (long) e->e_id, e->e_dn, 0 );
597 /* Retrieve an Entry that was stored using entry_encode above.
599 * Note: everything is stored in a single contiguous block, so
600 * you can not free individual attributes or names from this
601 * structure. Attempting to do so will likely corrupt memory.
604 int mdb_entry_decode(Operation *op, MDB_val *data, Entry **e)
606 struct mdb_info *mdb = (struct mdb_info *) op->o_bd->be_private;
607 int i, j, nattrs, nvals;
612 AttributeDescription *ad;
613 unsigned int *lp = (unsigned int *)data->mv_data;
617 Debug( LDAP_DEBUG_TRACE,
618 "=> mdb_entry_decode:\n",
623 x = mdb_entry_alloc(op, nattrs, nvals);
624 x->e_ocflags = *lp++;
631 ptr = (unsigned char *)(lp + i);
633 for (;nattrs>0; nattrs--) {
635 a->a_desc = mdb->mi_ads[*lp++];
636 a->a_flags = SLAP_ATTR_DONT_FREE_DATA | SLAP_ATTR_DONT_FREE_VALS;
637 a->a_numvals = *lp++;
638 if (a->a_numvals & HIGH_BIT) {
639 a->a_numvals ^= HIGH_BIT;
643 for (i=0; i<a->a_numvals; i++) {
644 bptr->bv_len = *lp++;;
645 bptr->bv_val = (char *)ptr;
646 ptr += bptr->bv_len+1;
655 for (i=0; i<a->a_numvals; i++) {
656 bptr->bv_len = *lp++;
657 bptr->bv_val = (char *)ptr;
658 ptr += bptr->bv_len+1;
665 a->a_nvals = a->a_vals;
667 /* FIXME: This is redundant once a sorted entry is saved into the DB */
668 if ( a->a_desc->ad_type->sat_flags & SLAP_AT_SORTED_VAL ) {
669 rc = slap_sort_vals( (Modifications *)a, &text, &j, NULL );
670 if ( rc == LDAP_SUCCESS ) {
671 a->a_flags |= SLAP_ATTR_SORTED_VALS;
672 } else if ( rc == LDAP_TYPE_OR_VALUE_EXISTS ) {
673 /* should never happen */
674 Debug( LDAP_DEBUG_ANY,
675 "mdb_entry_decode: attributeType %s value #%d provided more than once\n",
676 a->a_desc->ad_cname.bv_val, j, 0 );
686 Debug(LDAP_DEBUG_TRACE, "<= mdb_entry_decode\n",