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 static int mdb_entry_encode(Operation *op, MDB_txn *txn, Entry *e, MDB_val *data);
26 static Entry *mdb_entry_alloc( Operation *op, int nattrs, int nvals );
28 static int mdb_id2entry_put(
34 struct mdb_info *mdb = (struct mdb_info *) op->o_bd->be_private;
35 MDB_dbi dbi = mdb->mi_id2entry;
36 MDB_val key, data, d2;
39 /* We only store rdns, and they go in the dn2id database. */
41 key.mv_data = &e->e_id;
42 key.mv_size = sizeof(ID);
44 rc = mdb_entry_encode( op, tid, e, &d2 );
45 if( rc != LDAP_SUCCESS ) {
51 rc = mdb_put( tid, dbi, &key, &data, flag );
53 /* Was there a hole from slapadd? */
54 if ( flag == MDB_NOOVERWRITE && data.mv_size == 0 ) {
58 Debug( LDAP_DEBUG_ANY,
59 "mdb_id2entry_put: mdb_put failed: %s(%d) \"%s\"\n",
64 op->o_tmpfree( d2.mv_data, op->o_tmpmemctx );
69 * This routine adds (or updates) an entry on disk.
70 * The cache should be already be updated.
79 return mdb_id2entry_put(op, tid, e, MDB_NOOVERWRITE);
82 int mdb_id2entry_update(
87 return mdb_id2entry_put(op, tid, e, 0);
96 struct mdb_info *mdb = (struct mdb_info *) op->o_bd->be_private;
103 key.mv_size = sizeof(ID);
106 rc = mdb_cursor_get( mc, &key, &data, MDB_SET );
107 if ( rc == MDB_NOTFOUND ) {
108 /* Looking for root entry on an empty-dn suffix? */
109 if ( !id && BER_BVISEMPTY( &op->o_bd->be_nsuffix[0] )) {
110 struct berval gluebv = BER_BVC("glue");
111 Entry *r = mdb_entry_alloc(op, 2, 4);
112 Attribute *a = r->e_attrs;
116 r->e_ocflags = SLAP_OC_GLUE|SLAP_OC__END;
118 a->a_flags = SLAP_ATTR_DONT_FREE_DATA | SLAP_ATTR_DONT_FREE_VALS;
119 a->a_desc = slap_schema.si_ad_objectClass;
120 a->a_nvals = a->a_vals;
127 a->a_flags = SLAP_ATTR_DONT_FREE_DATA | SLAP_ATTR_DONT_FREE_VALS;
128 a->a_desc = slap_schema.si_ad_structuralObjectClass;
130 a->a_nvals = a->a_vals;
141 rc = mdb_entry_decode( op, &data, e );
145 (*e)->e_name.bv_val = NULL;
146 (*e)->e_nname.bv_val = NULL;
151 int mdb_id2entry_delete(
156 struct mdb_info *mdb = (struct mdb_info *) be->be_private;
157 MDB_dbi dbi = mdb->mi_id2entry;
161 key.mv_data = &e->e_id;
162 key.mv_size = sizeof(ID);
164 /* delete from database */
165 rc = mdb_del( tid, dbi, &key, NULL );
170 static Entry * mdb_entry_alloc(
175 Entry *e = op->o_tmpalloc( sizeof(Entry) +
176 nattrs * sizeof(Attribute) +
177 nvals * sizeof(struct berval), op->o_tmpmemctx );
178 BER_BVZERO(&e->e_bv);
181 e->e_attrs = (Attribute *)(e+1);
182 e->e_attrs->a_vals = (struct berval *)(e->e_attrs+nattrs);
190 int mdb_entry_return(
195 if ( e->e_private ) {
196 if ( slapMode & SLAP_TOOL_MODE ) {
197 ch_free( e->e_nname.bv_val );
198 ch_free( e->e_name.bv_val );
201 op->o_tmpfree( e->e_nname.bv_val, op->o_tmpmemctx );
202 op->o_tmpfree( e->e_name.bv_val, op->o_tmpmemctx );
203 op->o_tmpfree( e, op->o_tmpmemctx );
211 int mdb_entry_release(
216 struct mdb_info *mdb = (struct mdb_info *) op->o_bd->be_private;
217 struct mdb_op_info *moi = NULL;
220 /* slapMode : SLAP_SERVER_MODE, SLAP_TOOL_MODE,
221 SLAP_TRUNCATE_MODE, SLAP_UNDEFINED_MODE */
223 mdb_entry_return( op, e );
224 if ( slapMode == SLAP_SERVER_MODE ) {
226 LDAP_SLIST_FOREACH( oex, &op->o_extra, oe_next ) {
227 if ( oex->oe_key == mdb ) {
228 moi = (mdb_op_info *)oex;
229 /* If it was setup by entry_get we should probably free it */
230 if ( moi->moi_flag & MOI_FREEIT ) {
232 if ( moi->moi_ref < 1 ) {
233 mdb_txn_reset( moi->moi_txn );
235 LDAP_SLIST_REMOVE( &op->o_extra, &moi->moi_oe, OpExtra, oe_next );
236 op->o_tmpfree( moi, op->o_tmpmemctx );
247 /* return LDAP_SUCCESS IFF we can retrieve the specified entry.
253 AttributeDescription *at,
257 struct mdb_info *mdb = (struct mdb_info *) op->o_bd->be_private;
258 struct mdb_op_info *moi = NULL;
262 const char *at_name = at ? at->ad_cname.bv_val : "(null)";
264 Debug( LDAP_DEBUG_ARGS,
265 "=> mdb_entry_get: ndn: \"%s\"\n", ndn->bv_val, 0, 0 );
266 Debug( LDAP_DEBUG_ARGS,
267 "=> mdb_entry_get: oc: \"%s\", at: \"%s\"\n",
268 oc ? oc->soc_cname.bv_val : "(null)", at_name, 0);
270 rc = mdb_opinfo_get( op, mdb, rw == 0, &moi );
275 /* can we find entry */
276 rc = mdb_dn2entry( op, txn, ndn, &e, 0 );
282 return (rc != LDAP_BUSY) ? LDAP_OTHER : LDAP_BUSY;
285 Debug( LDAP_DEBUG_ACL,
286 "=> mdb_entry_get: cannot find entry: \"%s\"\n",
288 rc = LDAP_NO_SUCH_OBJECT;
292 Debug( LDAP_DEBUG_ACL,
293 "=> mdb_entry_get: found entry: \"%s\"\n",
296 if ( oc && !is_entry_objectclass( e, oc, 0 )) {
297 Debug( LDAP_DEBUG_ACL,
298 "<= mdb_entry_get: failed to find objectClass %s\n",
299 oc->soc_cname.bv_val, 0, 0 );
300 rc = LDAP_NO_SUCH_ATTRIBUTE;
304 /* NOTE: attr_find() or attrs_find()? */
305 if ( at && attr_find( e->e_attrs, at ) == NULL ) {
306 Debug( LDAP_DEBUG_ACL,
307 "<= mdb_entry_get: failed to find attribute %s\n",
308 at->ad_cname.bv_val, 0, 0 );
309 rc = LDAP_NO_SUCH_ATTRIBUTE;
314 if( rc != LDAP_SUCCESS ) {
317 mdb_entry_return( op, e );
319 if (moi->moi_ref == 1) {
320 LDAP_SLIST_REMOVE( &op->o_extra, &moi->moi_oe, OpExtra, oe_next );
321 mdb_txn_reset( txn );
322 op->o_tmpfree( moi, op->o_tmpmemctx );
328 Debug( LDAP_DEBUG_TRACE,
329 "mdb_entry_get: rc=%d\n",
335 mdb_reader_free( void *key, void *data )
339 if ( txn ) mdb_txn_abort( txn );
342 /* free up any keys used by the main thread */
344 mdb_reader_flush( MDB_env *env )
347 void *ctx = ldap_pvt_thread_pool_context();
349 if ( !ldap_pvt_thread_pool_getkey( ctx, env, &data, NULL ) ) {
350 ldap_pvt_thread_pool_setkey( ctx, env, NULL, 0, NULL, NULL );
351 mdb_reader_free( env, data );
356 mdb_opinfo_get( Operation *op, struct mdb_info *mdb, int rdonly, mdb_op_info **moip )
361 mdb_op_info *moi = NULL;
364 assert( op != NULL );
366 if ( !mdb || !moip ) return -1;
368 /* If no op was provided, try to find the ctx anyway... */
370 ctx = op->o_threadctx;
372 ctx = ldap_pvt_thread_pool_context();
376 LDAP_SLIST_FOREACH( oex, &op->o_extra, oe_next ) {
377 if ( oex->oe_key == mdb ) break;
379 moi = (mdb_op_info *)oex;
387 moi = op->o_tmpalloc(sizeof(struct mdb_op_info),op->o_tmpmemctx);
389 moi = ch_malloc(sizeof(mdb_op_info));
391 moi->moi_flag = MOI_FREEIT;
394 LDAP_SLIST_INSERT_HEAD( &op->o_extra, &moi->moi_oe, oe_next );
395 moi->moi_oe.oe_key = mdb;
401 /* This op started as a reader, but now wants to write. */
402 if ( moi->moi_flag & MOI_READER ) {
404 LDAP_SLIST_INSERT_HEAD( &op->o_extra, &moi->moi_oe, oe_next );
406 /* This op is continuing an existing write txn */
410 if ( !moi->moi_txn ) {
411 rc = mdb_txn_begin( mdb->mi_dbenv, 0, &moi->moi_txn );
413 Debug( LDAP_DEBUG_ANY, "mdb_opinfo_get: err %s(%d)\n",
414 mdb_strerror(rc), rc, 0 );
421 /* OK, this is a reader */
422 if ( !moi->moi_txn ) {
424 /* Shouldn't happen unless we're single-threaded */
425 rc = mdb_txn_begin( mdb->mi_dbenv, MDB_RDONLY, &moi->moi_txn );
427 Debug( LDAP_DEBUG_ANY, "mdb_opinfo_get: err %s(%d)\n",
428 mdb_strerror(rc), rc, 0 );
432 if ( ldap_pvt_thread_pool_getkey( ctx, mdb->mi_dbenv, &data, NULL ) ) {
433 rc = mdb_txn_begin( mdb->mi_dbenv, MDB_RDONLY, &moi->moi_txn );
435 Debug( LDAP_DEBUG_ANY, "mdb_opinfo_get: err %s(%d)\n",
436 mdb_strerror(rc), rc, 0 );
440 if ( ( rc = ldap_pvt_thread_pool_setkey( ctx, mdb->mi_dbenv,
441 data, mdb_reader_free, NULL, NULL ) ) ) {
442 mdb_txn_abort( moi->moi_txn );
444 Debug( LDAP_DEBUG_ANY, "mdb_opinfo_get: thread_pool_setkey failed err (%d)\n",
452 moi->moi_flag |= MOI_READER;
454 if ( moi->moi_ref < 1 ) {
458 mdb_txn_renew( moi->moi_txn );
467 typedef struct Ecount {
474 /* Count up the sizes of the components of an entry */
475 static int mdb_entry_partsize(struct mdb_info *mdb, MDB_txn *txn, Entry *e,
479 int i, nat = 0, nval = 0, nnval = 0;
482 len = 4*sizeof(int); /* nattrs, nvals, ocflags, offset */
483 for (a=e->e_attrs; a; a=a->a_next) {
484 /* For AttributeDesc, we only store the attr index */
486 if (!mdb->mi_adxs[a->a_desc->ad_index]) {
487 int rc = mdb_ad_get(mdb, txn, a->a_desc);
491 len += 2*sizeof(int); /* AD index, numvals */
492 nval += a->a_numvals + 1; /* empty berval at end */
493 for (i=0; i<a->a_numvals; i++) {
494 len += a->a_vals[i].bv_len + 1 + sizeof(int); /* len */
496 if (a->a_nvals != a->a_vals) {
497 nval += a->a_numvals + 1;
499 for (i=0; i<a->a_numvals; i++) {
500 len += a->a_nvals[i].bv_len + 1 + sizeof(int);;
505 len = (len + sizeof(ID)-1) & ~(sizeof(ID)-1);
509 eh->offset = nat + nval - nnval;
513 #define HIGH_BIT (1<<(sizeof(unsigned int)*CHAR_BIT-1))
515 /* Flatten an Entry into a buffer. The buffer starts with the count of the
516 * number of attributes in the entry, the total number of values in the
517 * entry, and the e_ocflags. It then contains a list of integers for each
518 * attribute. For each attribute the first integer gives the index of the
519 * matching AttributeDescription, followed by the number of values in the
520 * attribute. If the high bit is set, the attribute also has normalized
521 * values present. (Note - a_numvals is an unsigned int, so this means
522 * it's possible to receive an attribute that we can't encode due to size
523 * overflow. In practice, this should not be an issue.) Then the length
524 * of each value is listed. If there are normalized values, their lengths
525 * come next. This continues for each attribute. After all of the lengths
526 * for the last attribute, the actual values are copied, with a NUL
527 * terminator after each value. The buffer is padded to the sizeof(ID).
528 * The entire buffer size is precomputed so that a single malloc can be
531 static int mdb_entry_encode(Operation *op, MDB_txn *txn, Entry *e, MDB_val *data)
533 struct mdb_info *mdb = (struct mdb_info *) op->o_bd->be_private;
541 Debug( LDAP_DEBUG_TRACE, "=> mdb_entry_encode(0x%08lx): %s\n",
542 (long) e->e_id, e->e_dn, 0 );
544 /* make sure e->e_ocflags is set */
545 if (is_entry_referral(e))
548 rc = mdb_entry_partsize( mdb, txn, e, &eh );
551 data->mv_size = eh.len;
552 data->mv_data = op->o_tmpalloc(data->mv_size, op->o_tmpmemctx);
553 lp = (unsigned int *)data->mv_data;
556 *lp++ = (unsigned int)e->e_ocflags;
558 ptr = (unsigned char *)(lp + eh.offset);
560 for (a=e->e_attrs; a; a=a->a_next) {
561 *lp++ = mdb->mi_adxs[a->a_desc->ad_index];
563 if (a->a_nvals != a->a_vals)
567 for (i=0; a->a_vals[i].bv_val; i++);
568 assert( i == a->a_numvals );
569 for (i=0; i<a->a_numvals; i++) {
570 *lp++ = a->a_vals[i].bv_len;
571 memcpy(ptr, a->a_vals[i].bv_val,
572 a->a_vals[i].bv_len);
573 ptr += a->a_vals[i].bv_len;
576 if (a->a_nvals != a->a_vals) {
577 for (i=0; i<a->a_numvals; i++) {
578 *lp++ = a->a_nvals[i].bv_len;
579 memcpy(ptr, a->a_nvals[i].bv_val,
580 a->a_nvals[i].bv_len);
581 ptr += a->a_nvals[i].bv_len;
588 Debug( LDAP_DEBUG_TRACE, "<= mdb_entry_encode(0x%08lx): %s\n",
589 (long) e->e_id, e->e_dn, 0 );
594 /* Retrieve an Entry that was stored using entry_encode above.
596 * Note: everything is stored in a single contiguous block, so
597 * you can not free individual attributes or names from this
598 * structure. Attempting to do so will likely corrupt memory.
601 int mdb_entry_decode(Operation *op, MDB_val *data, Entry **e)
603 struct mdb_info *mdb = (struct mdb_info *) op->o_bd->be_private;
604 int i, j, nattrs, nvals;
609 AttributeDescription *ad;
610 unsigned int *lp = (unsigned int *)data->mv_data;
614 Debug( LDAP_DEBUG_TRACE,
615 "=> mdb_entry_decode:\n",
620 x = mdb_entry_alloc(op, nattrs, nvals);
621 x->e_ocflags = *lp++;
628 ptr = (unsigned char *)(lp + i);
630 for (;nattrs>0; nattrs--) {
632 a->a_desc = mdb->mi_ads[*lp++];
633 a->a_flags = SLAP_ATTR_DONT_FREE_DATA | SLAP_ATTR_DONT_FREE_VALS;
634 a->a_numvals = *lp++;
635 if (a->a_numvals & HIGH_BIT) {
636 a->a_numvals ^= HIGH_BIT;
640 for (i=0; i<a->a_numvals; i++) {
641 bptr->bv_len = *lp++;;
642 bptr->bv_val = (char *)ptr;
643 ptr += bptr->bv_len+1;
652 for (i=0; i<a->a_numvals; i++) {
653 bptr->bv_len = *lp++;
654 bptr->bv_val = (char *)ptr;
655 ptr += bptr->bv_len+1;
662 a->a_nvals = a->a_vals;
664 /* FIXME: This is redundant once a sorted entry is saved into the DB */
665 if ( a->a_desc->ad_type->sat_flags & SLAP_AT_SORTED_VAL ) {
666 rc = slap_sort_vals( (Modifications *)a, &text, &j, NULL );
667 if ( rc == LDAP_SUCCESS ) {
668 a->a_flags |= SLAP_ATTR_SORTED_VALS;
669 } else if ( rc == LDAP_TYPE_OR_VALUE_EXISTS ) {
670 /* should never happen */
671 Debug( LDAP_DEBUG_ANY,
672 "mdb_entry_decode: attributeType %s value #%d provided more than once\n",
673 a->a_desc->ad_cname.bv_val, j, 0 );
683 Debug(LDAP_DEBUG_TRACE, "<= mdb_entry_decode\n",