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;
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, &data );
45 if( rc != LDAP_SUCCESS ) {
49 rc = mdb_put( tid, dbi, &key, &data, flag );
50 op->o_tmpfree( data.mv_data, op->o_tmpmemctx );
52 Debug( LDAP_DEBUG_ANY,
53 "mdb_id2entry_put: mdb_put failed: %s(%d) \"%s\"\n",
62 * This routine adds (or updates) an entry on disk.
63 * The cache should be already be updated.
72 return mdb_id2entry_put(op, tid, e, MDB_NOOVERWRITE);
75 int mdb_id2entry_update(
80 return mdb_id2entry_put(op, tid, e, 0);
89 struct mdb_info *mdb = (struct mdb_info *) op->o_bd->be_private;
96 key.mv_size = sizeof(ID);
99 rc = mdb_cursor_get( mc, &key, &data, MDB_SET );
100 if ( rc == MDB_NOTFOUND ) {
101 /* Looking for root entry on an empty-dn suffix? */
102 if ( !id && BER_BVISEMPTY( &op->o_bd->be_nsuffix[0] )) {
103 struct berval gluebv = BER_BVC("glue");
104 Entry *r = mdb_entry_alloc(op, 2, 4);
105 Attribute *a = r->e_attrs;
109 r->e_ocflags = SLAP_OC_GLUE|SLAP_OC__END;
111 a->a_flags = SLAP_ATTR_DONT_FREE_DATA | SLAP_ATTR_DONT_FREE_VALS;
112 a->a_desc = slap_schema.si_ad_objectClass;
113 a->a_nvals = a->a_vals;
119 a->a_flags = SLAP_ATTR_DONT_FREE_DATA | SLAP_ATTR_DONT_FREE_VALS;
120 a->a_desc = slap_schema.si_ad_structuralObjectClass;
122 a->a_nvals = a->a_vals;
132 rc = mdb_entry_decode( op, &data, e );
136 (*e)->e_name.bv_val = NULL;
137 (*e)->e_nname.bv_val = NULL;
142 int mdb_id2entry_delete(
147 struct mdb_info *mdb = (struct mdb_info *) be->be_private;
148 MDB_dbi dbi = mdb->mi_id2entry;
152 key.mv_data = &e->e_id;
153 key.mv_size = sizeof(ID);
155 /* delete from database */
156 rc = mdb_del( tid, dbi, &key, NULL );
161 static Attribute * mdb_attrs_alloc(
168 if (!nattrs || !nvals) return NULL;
170 s = op->o_tmpalloc( nattrs * sizeof(Attribute) +
171 nvals * sizeof(struct berval), op->o_tmpmemctx );
173 for (a=s; nattrs>1; nattrs--) {
178 s->a_vals = (struct berval *)(a+1);
182 static Entry * mdb_entry_alloc(
187 Entry *e = op->o_tmpalloc( sizeof(Entry), op->o_tmpmemctx );
188 BER_BVZERO(&e->e_bv);
189 e->e_attrs = mdb_attrs_alloc( op, nattrs, nvals );
194 int mdb_entry_return(
199 if ( e->e_private ) {
200 op->o_tmpfree( e->e_nname.bv_val, op->o_tmpmemctx );
201 op->o_tmpfree( e->e_name.bv_val, op->o_tmpmemctx );
202 op->o_tmpfree( e->e_attrs, op->o_tmpmemctx );
203 op->o_tmpfree( e, op->o_tmpmemctx );
210 int mdb_entry_release(
215 struct mdb_info *mdb = (struct mdb_info *) op->o_bd->be_private;
216 struct mdb_op_info *moi = NULL;
219 /* slapMode : SLAP_SERVER_MODE, SLAP_TOOL_MODE,
220 SLAP_TRUNCATE_MODE, SLAP_UNDEFINED_MODE */
222 mdb_entry_return( op, e );
223 if ( slapMode == SLAP_SERVER_MODE ) {
225 LDAP_SLIST_FOREACH( oex, &op->o_extra, oe_next ) {
226 if ( oex->oe_key == mdb ) {
227 moi = (mdb_op_info *)oex;
228 /* If it was setup by entry_get we should probably free it */
229 if ( moi->moi_flag & MOI_FREEIT ) {
231 if ( moi->moi_ref < 1 ) {
232 mdb_txn_reset( moi->moi_txn );
234 LDAP_SLIST_REMOVE( &op->o_extra, &moi->moi_oe, OpExtra, oe_next );
235 op->o_tmpfree( moi, op->o_tmpmemctx );
246 /* return LDAP_SUCCESS IFF we can retrieve the specified entry.
252 AttributeDescription *at,
256 struct mdb_info *mdb = (struct mdb_info *) op->o_bd->be_private;
257 struct mdb_op_info *moi = NULL;
261 const char *at_name = at ? at->ad_cname.bv_val : "(null)";
263 Debug( LDAP_DEBUG_ARGS,
264 "=> mdb_entry_get: ndn: \"%s\"\n", ndn->bv_val, 0, 0 );
265 Debug( LDAP_DEBUG_ARGS,
266 "=> mdb_entry_get: oc: \"%s\", at: \"%s\"\n",
267 oc ? oc->soc_cname.bv_val : "(null)", at_name, 0);
269 rc = mdb_opinfo_get( op, mdb, rw == 0, &moi );
274 /* can we find entry */
275 rc = mdb_dn2entry( op, txn, ndn, &e, 0 );
281 return (rc != LDAP_BUSY) ? LDAP_OTHER : LDAP_BUSY;
284 Debug( LDAP_DEBUG_ACL,
285 "=> mdb_entry_get: cannot find entry: \"%s\"\n",
287 rc = LDAP_NO_SUCH_OBJECT;
291 Debug( LDAP_DEBUG_ACL,
292 "=> mdb_entry_get: found entry: \"%s\"\n",
295 if ( oc && !is_entry_objectclass( e, oc, 0 )) {
296 Debug( LDAP_DEBUG_ACL,
297 "<= mdb_entry_get: failed to find objectClass %s\n",
298 oc->soc_cname.bv_val, 0, 0 );
299 rc = LDAP_NO_SUCH_ATTRIBUTE;
303 /* NOTE: attr_find() or attrs_find()? */
304 if ( at && attr_find( e->e_attrs, at ) == NULL ) {
305 Debug( LDAP_DEBUG_ACL,
306 "<= mdb_entry_get: failed to find attribute %s\n",
307 at->ad_cname.bv_val, 0, 0 );
308 rc = LDAP_NO_SUCH_ATTRIBUTE;
313 if( rc != LDAP_SUCCESS ) {
316 mdb_entry_return( op, e );
318 if (moi->moi_ref == 1) {
319 LDAP_SLIST_REMOVE( &op->o_extra, &moi->moi_oe, OpExtra, oe_next );
320 mdb_txn_reset( txn );
321 op->o_tmpfree( moi, op->o_tmpmemctx );
327 Debug( LDAP_DEBUG_TRACE,
328 "mdb_entry_get: rc=%d\n",
334 mdb_reader_free( void *key, void *data )
338 if ( txn ) mdb_txn_abort( txn );
341 /* free up any keys used by the main thread */
343 mdb_reader_flush( MDB_env *env )
346 void *ctx = ldap_pvt_thread_pool_context();
348 if ( !ldap_pvt_thread_pool_getkey( ctx, env, &data, NULL ) ) {
349 ldap_pvt_thread_pool_setkey( ctx, env, NULL, 0, NULL, NULL );
350 mdb_reader_free( env, data );
355 mdb_opinfo_get( Operation *op, struct mdb_info *mdb, int rdonly, mdb_op_info **moip )
360 mdb_op_info *moi = NULL;
363 assert( op != NULL );
365 if ( !mdb || !moip ) return -1;
367 /* If no op was provided, try to find the ctx anyway... */
369 ctx = op->o_threadctx;
371 ctx = ldap_pvt_thread_pool_context();
375 LDAP_SLIST_FOREACH( oex, &op->o_extra, oe_next ) {
376 if ( oex->oe_key == mdb ) break;
378 moi = (mdb_op_info *)oex;
386 moi = op->o_tmpalloc(sizeof(struct mdb_op_info),op->o_tmpmemctx);
388 moi = ch_malloc(sizeof(mdb_op_info));
390 moi->moi_flag = MOI_FREEIT;
393 LDAP_SLIST_INSERT_HEAD( &op->o_extra, &moi->moi_oe, oe_next );
394 moi->moi_oe.oe_key = mdb;
400 /* This op started as a reader, but now wants to write. */
401 if ( moi->moi_flag & MOI_READER ) {
403 LDAP_SLIST_INSERT_HEAD( &op->o_extra, &moi->moi_oe, oe_next );
405 /* This op is continuing an existing write txn */
409 if ( !moi->moi_txn ) {
410 rc = mdb_txn_begin( mdb->mi_dbenv, 0, &moi->moi_txn );
412 Debug( LDAP_DEBUG_ANY, "mdb_opinfo_get: err %s(%d)\n",
413 mdb_strerror(rc), rc, 0 );
420 /* OK, this is a reader */
421 if ( !moi->moi_txn ) {
423 /* Shouldn't happen unless we're single-threaded */
424 rc = mdb_txn_begin( mdb->mi_dbenv, MDB_RDONLY, &moi->moi_txn );
426 Debug( LDAP_DEBUG_ANY, "mdb_opinfo_get: err %s(%d)\n",
427 mdb_strerror(rc), rc, 0 );
431 if ( ldap_pvt_thread_pool_getkey( ctx, mdb->mi_dbenv, &data, NULL ) ) {
432 rc = mdb_txn_begin( mdb->mi_dbenv, MDB_RDONLY, &moi->moi_txn );
434 Debug( LDAP_DEBUG_ANY, "mdb_opinfo_get: err %s(%d)\n",
435 mdb_strerror(rc), rc, 0 );
439 if ( ( rc = ldap_pvt_thread_pool_setkey( ctx, mdb->mi_dbenv,
440 data, mdb_reader_free, NULL, NULL ) ) ) {
441 mdb_txn_abort( moi->moi_txn );
443 Debug( LDAP_DEBUG_ANY, "mdb_opinfo_get: thread_pool_setkey failed err (%d)\n",
451 moi->moi_flag |= MOI_READER;
453 if ( moi->moi_ref < 1 ) {
457 mdb_txn_renew( moi->moi_txn );
466 /* This is like a ber_len */
467 #define entry_lenlen(l) (((l) < 0x80) ? 1 : ((l) < 0x100) ? 2 : \
468 ((l) < 0x10000) ? 3 : ((l) < 0x1000000) ? 4 : 5)
471 mdb_entry_putlen(unsigned char **buf, ber_len_t len)
473 ber_len_t lenlen = entry_lenlen(len);
476 **buf = (unsigned char) len;
479 **buf = 0x80 | ((unsigned char) lenlen - 1);
480 for (i=lenlen-1; i>0; i--) {
481 (*buf)[i] = (unsigned char) len;
489 mdb_entry_getlen(unsigned char **buf)
506 /* Count up the sizes of the components of an entry */
507 static int mdb_entry_partsize(struct mdb_info *mdb, MDB_txn *txn, Entry *e,
511 int i, nat = 0, nval = 0;
514 for (a=e->e_attrs; a; a=a->a_next) {
515 /* For AttributeDesc, we only store the attr index */
517 if (!mdb->mi_adxs[a->a_desc->ad_index]) {
518 int rc = mdb_ad_get(mdb, txn, a->a_desc);
522 len += entry_lenlen(mdb->mi_adxs[a->a_desc->ad_index]);
523 for (i=0; a->a_vals[i].bv_val; i++) {
525 len += a->a_vals[i].bv_len + 1;
526 len += entry_lenlen(a->a_vals[i].bv_len);
528 len += entry_lenlen(i);
529 nval++; /* empty berval at end */
530 if (a->a_nvals != a->a_vals) {
531 for (i=0; a->a_nvals[i].bv_val; i++) {
533 len += a->a_nvals[i].bv_len + 1;
534 len += entry_lenlen(a->a_nvals[i].bv_len);
536 len += entry_lenlen(i); /* i nvals */
539 len += entry_lenlen(0); /* 0 nvals */
542 len += entry_lenlen(e->e_ocflags);
543 len += entry_lenlen(nat);
544 len += entry_lenlen(nval);
551 /* Flatten an Entry into a buffer. The buffer is filled with just the
552 * strings/bervals of all the entry components. Each field is preceded
553 * by its length, encoded the way ber_put_len works. Every field is NUL
554 * terminated. The entire buffer size is precomputed so that a single
555 * malloc can be performed. The entry size is also recorded,
556 * to aid in entry_decode.
558 static int mdb_entry_encode(Operation *op, MDB_txn *txn, Entry *e, MDB_val *data)
560 struct mdb_info *mdb = (struct mdb_info *) op->o_bd->be_private;
561 ber_len_t len, dnlen, ndnlen, i;
567 Debug( LDAP_DEBUG_TRACE, "=> mdb_entry_encode(0x%08lx): %s\n",
568 (long) e->e_id, e->e_dn, 0 );
570 if (is_entry_referral(e))
573 rc = mdb_entry_partsize( mdb, txn, e, &eh );
576 data->mv_size = eh.bv.bv_len;
577 data->mv_data = op->o_tmpalloc(data->mv_size, op->o_tmpmemctx);
578 ptr = (unsigned char *)data->mv_data;
579 mdb_entry_putlen(&ptr, eh.nattrs);
580 mdb_entry_putlen(&ptr, eh.nvals);
581 mdb_entry_putlen(&ptr, e->e_ocflags);
583 for (a=e->e_attrs; a; a=a->a_next) {
584 mdb_entry_putlen(&ptr, mdb->mi_adxs[a->a_desc->ad_index]);
586 for (i=0; a->a_vals[i].bv_val; i++);
587 assert( i == a->a_numvals );
588 mdb_entry_putlen(&ptr, i);
589 for (i=0; a->a_vals[i].bv_val; i++) {
590 mdb_entry_putlen(&ptr, a->a_vals[i].bv_len);
591 AC_MEMCPY(ptr, a->a_vals[i].bv_val,
592 a->a_vals[i].bv_len);
593 ptr += a->a_vals[i].bv_len;
596 if (a->a_nvals != a->a_vals) {
597 mdb_entry_putlen(&ptr, i);
598 for (i=0; a->a_nvals[i].bv_val; i++) {
599 mdb_entry_putlen(&ptr, a->a_nvals[i].bv_len);
600 AC_MEMCPY(ptr, a->a_nvals[i].bv_val,
601 a->a_nvals[i].bv_len);
602 ptr += a->a_nvals[i].bv_len;
606 mdb_entry_putlen(&ptr, 0);
611 Debug( LDAP_DEBUG_TRACE, "<= mdb_entry_encode(0x%08lx): %s\n",
612 (long) e->e_id, e->e_dn, 0 );
617 /* Retrieve an Entry that was stored using entry_encode above.
619 * Note: everything is stored in a single contiguous block, so
620 * you can not free individual attributes or names from this
621 * structure. Attempting to do so will likely corrupt memory.
624 int mdb_entry_decode(Operation *op, MDB_val *data, Entry **e)
626 struct mdb_info *mdb = (struct mdb_info *) op->o_bd->be_private;
627 int i, j, nattrs, nvals;
632 AttributeDescription *ad;
633 unsigned char *ptr = (unsigned char *)data->mv_data;
636 Debug( LDAP_DEBUG_TRACE,
637 "=> mdb_entry_decode:\n",
640 nattrs = mdb_entry_getlen(&ptr);
641 nvals = mdb_entry_getlen(&ptr);
642 x = mdb_entry_alloc(op, nattrs, nvals);
643 x->e_ocflags = mdb_entry_getlen(&ptr);
650 while ((i = mdb_entry_getlen(&ptr))) {
651 a->a_desc = mdb->mi_ads[i];
652 a->a_flags = SLAP_ATTR_DONT_FREE_DATA | SLAP_ATTR_DONT_FREE_VALS;
653 j = mdb_entry_getlen(&ptr);
658 i = mdb_entry_getlen(&ptr);
660 bptr->bv_val = (char *)ptr;
669 j = mdb_entry_getlen(&ptr);
673 i = mdb_entry_getlen(&ptr);
675 bptr->bv_val = (char *)ptr;
684 a->a_nvals = a->a_vals;
686 /* FIXME: This is redundant once a sorted entry is saved into the DB */
687 if ( a->a_desc->ad_type->sat_flags & SLAP_AT_SORTED_VAL ) {
688 rc = slap_sort_vals( (Modifications *)a, &text, &j, NULL );
689 if ( rc == LDAP_SUCCESS ) {
690 a->a_flags |= SLAP_ATTR_SORTED_VALS;
691 } else if ( rc == LDAP_TYPE_OR_VALUE_EXISTS ) {
692 /* should never happen */
693 Debug( LDAP_DEBUG_ANY,
694 "mdb_entry_decode: attributeType %s value #%d provided more than once\n",
695 a->a_desc->ad_cname.bv_val, j, 0 );
706 Debug(LDAP_DEBUG_TRACE, "<= mdb_entry_decode\n",