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;
126 a->a_flags = SLAP_ATTR_DONT_FREE_DATA | SLAP_ATTR_DONT_FREE_VALS;
127 a->a_desc = slap_schema.si_ad_structuralObjectClass;
129 a->a_nvals = a->a_vals;
139 rc = mdb_entry_decode( op, &data, e );
143 (*e)->e_name.bv_val = NULL;
144 (*e)->e_nname.bv_val = NULL;
149 int mdb_id2entry_delete(
154 struct mdb_info *mdb = (struct mdb_info *) be->be_private;
155 MDB_dbi dbi = mdb->mi_id2entry;
159 key.mv_data = &e->e_id;
160 key.mv_size = sizeof(ID);
162 /* delete from database */
163 rc = mdb_del( tid, dbi, &key, NULL );
168 static Attribute * mdb_attrs_alloc(
175 if (!nattrs || !nvals) return NULL;
177 s = op->o_tmpalloc( nattrs * sizeof(Attribute) +
178 nvals * sizeof(struct berval), op->o_tmpmemctx );
180 for (a=s; nattrs>1; nattrs--) {
185 s->a_vals = (struct berval *)(a+1);
189 static Entry * mdb_entry_alloc(
194 Entry *e = op->o_tmpalloc( sizeof(Entry), op->o_tmpmemctx );
195 BER_BVZERO(&e->e_bv);
196 e->e_attrs = mdb_attrs_alloc( op, nattrs, nvals );
201 int mdb_entry_return(
206 if ( e->e_private ) {
207 if ( slapMode & SLAP_TOOL_MODE ) {
208 ch_free( e->e_nname.bv_val );
209 ch_free( e->e_name.bv_val );
210 ch_free( e->e_attrs );
213 op->o_tmpfree( e->e_nname.bv_val, op->o_tmpmemctx );
214 op->o_tmpfree( e->e_name.bv_val, op->o_tmpmemctx );
215 op->o_tmpfree( e->e_attrs, op->o_tmpmemctx );
216 op->o_tmpfree( e, op->o_tmpmemctx );
224 int mdb_entry_release(
229 struct mdb_info *mdb = (struct mdb_info *) op->o_bd->be_private;
230 struct mdb_op_info *moi = NULL;
233 /* slapMode : SLAP_SERVER_MODE, SLAP_TOOL_MODE,
234 SLAP_TRUNCATE_MODE, SLAP_UNDEFINED_MODE */
236 mdb_entry_return( op, e );
237 if ( slapMode == SLAP_SERVER_MODE ) {
239 LDAP_SLIST_FOREACH( oex, &op->o_extra, oe_next ) {
240 if ( oex->oe_key == mdb ) {
241 moi = (mdb_op_info *)oex;
242 /* If it was setup by entry_get we should probably free it */
243 if ( moi->moi_flag & MOI_FREEIT ) {
245 if ( moi->moi_ref < 1 ) {
246 mdb_txn_reset( moi->moi_txn );
248 LDAP_SLIST_REMOVE( &op->o_extra, &moi->moi_oe, OpExtra, oe_next );
249 op->o_tmpfree( moi, op->o_tmpmemctx );
260 /* return LDAP_SUCCESS IFF we can retrieve the specified entry.
266 AttributeDescription *at,
270 struct mdb_info *mdb = (struct mdb_info *) op->o_bd->be_private;
271 struct mdb_op_info *moi = NULL;
275 const char *at_name = at ? at->ad_cname.bv_val : "(null)";
277 Debug( LDAP_DEBUG_ARGS,
278 "=> mdb_entry_get: ndn: \"%s\"\n", ndn->bv_val, 0, 0 );
279 Debug( LDAP_DEBUG_ARGS,
280 "=> mdb_entry_get: oc: \"%s\", at: \"%s\"\n",
281 oc ? oc->soc_cname.bv_val : "(null)", at_name, 0);
283 rc = mdb_opinfo_get( op, mdb, rw == 0, &moi );
288 /* can we find entry */
289 rc = mdb_dn2entry( op, txn, ndn, &e, 0 );
295 return (rc != LDAP_BUSY) ? LDAP_OTHER : LDAP_BUSY;
298 Debug( LDAP_DEBUG_ACL,
299 "=> mdb_entry_get: cannot find entry: \"%s\"\n",
301 rc = LDAP_NO_SUCH_OBJECT;
305 Debug( LDAP_DEBUG_ACL,
306 "=> mdb_entry_get: found entry: \"%s\"\n",
309 if ( oc && !is_entry_objectclass( e, oc, 0 )) {
310 Debug( LDAP_DEBUG_ACL,
311 "<= mdb_entry_get: failed to find objectClass %s\n",
312 oc->soc_cname.bv_val, 0, 0 );
313 rc = LDAP_NO_SUCH_ATTRIBUTE;
317 /* NOTE: attr_find() or attrs_find()? */
318 if ( at && attr_find( e->e_attrs, at ) == NULL ) {
319 Debug( LDAP_DEBUG_ACL,
320 "<= mdb_entry_get: failed to find attribute %s\n",
321 at->ad_cname.bv_val, 0, 0 );
322 rc = LDAP_NO_SUCH_ATTRIBUTE;
327 if( rc != LDAP_SUCCESS ) {
330 mdb_entry_return( op, e );
332 if (moi->moi_ref == 1) {
333 LDAP_SLIST_REMOVE( &op->o_extra, &moi->moi_oe, OpExtra, oe_next );
334 mdb_txn_reset( txn );
335 op->o_tmpfree( moi, op->o_tmpmemctx );
341 Debug( LDAP_DEBUG_TRACE,
342 "mdb_entry_get: rc=%d\n",
348 mdb_reader_free( void *key, void *data )
352 if ( txn ) mdb_txn_abort( txn );
355 /* free up any keys used by the main thread */
357 mdb_reader_flush( MDB_env *env )
360 void *ctx = ldap_pvt_thread_pool_context();
362 if ( !ldap_pvt_thread_pool_getkey( ctx, env, &data, NULL ) ) {
363 ldap_pvt_thread_pool_setkey( ctx, env, NULL, 0, NULL, NULL );
364 mdb_reader_free( env, data );
369 mdb_opinfo_get( Operation *op, struct mdb_info *mdb, int rdonly, mdb_op_info **moip )
374 mdb_op_info *moi = NULL;
377 assert( op != NULL );
379 if ( !mdb || !moip ) return -1;
381 /* If no op was provided, try to find the ctx anyway... */
383 ctx = op->o_threadctx;
385 ctx = ldap_pvt_thread_pool_context();
389 LDAP_SLIST_FOREACH( oex, &op->o_extra, oe_next ) {
390 if ( oex->oe_key == mdb ) break;
392 moi = (mdb_op_info *)oex;
400 moi = op->o_tmpalloc(sizeof(struct mdb_op_info),op->o_tmpmemctx);
402 moi = ch_malloc(sizeof(mdb_op_info));
404 moi->moi_flag = MOI_FREEIT;
407 LDAP_SLIST_INSERT_HEAD( &op->o_extra, &moi->moi_oe, oe_next );
408 moi->moi_oe.oe_key = mdb;
414 /* This op started as a reader, but now wants to write. */
415 if ( moi->moi_flag & MOI_READER ) {
417 LDAP_SLIST_INSERT_HEAD( &op->o_extra, &moi->moi_oe, oe_next );
419 /* This op is continuing an existing write txn */
423 if ( !moi->moi_txn ) {
424 rc = mdb_txn_begin( mdb->mi_dbenv, 0, &moi->moi_txn );
426 Debug( LDAP_DEBUG_ANY, "mdb_opinfo_get: err %s(%d)\n",
427 mdb_strerror(rc), rc, 0 );
434 /* OK, this is a reader */
435 if ( !moi->moi_txn ) {
437 /* Shouldn't happen unless we're single-threaded */
438 rc = mdb_txn_begin( mdb->mi_dbenv, MDB_RDONLY, &moi->moi_txn );
440 Debug( LDAP_DEBUG_ANY, "mdb_opinfo_get: err %s(%d)\n",
441 mdb_strerror(rc), rc, 0 );
445 if ( ldap_pvt_thread_pool_getkey( ctx, mdb->mi_dbenv, &data, NULL ) ) {
446 rc = mdb_txn_begin( mdb->mi_dbenv, MDB_RDONLY, &moi->moi_txn );
448 Debug( LDAP_DEBUG_ANY, "mdb_opinfo_get: err %s(%d)\n",
449 mdb_strerror(rc), rc, 0 );
453 if ( ( rc = ldap_pvt_thread_pool_setkey( ctx, mdb->mi_dbenv,
454 data, mdb_reader_free, NULL, NULL ) ) ) {
455 mdb_txn_abort( moi->moi_txn );
457 Debug( LDAP_DEBUG_ANY, "mdb_opinfo_get: thread_pool_setkey failed err (%d)\n",
465 moi->moi_flag |= MOI_READER;
467 if ( moi->moi_ref < 1 ) {
471 mdb_txn_renew( moi->moi_txn );
480 /* This is like a ber_len */
481 #define entry_lenlen(l) (((l) < 0x80) ? 1 : ((l) < 0x100) ? 2 : \
482 ((l) < 0x10000) ? 3 : ((l) < 0x1000000) ? 4 : 5)
485 mdb_entry_putlen(unsigned char **buf, ber_len_t len)
487 ber_len_t lenlen = entry_lenlen(len);
490 **buf = (unsigned char) len;
493 **buf = 0x80 | ((unsigned char) lenlen - 1);
494 for (i=lenlen-1; i>0; i--) {
495 (*buf)[i] = (unsigned char) len;
503 mdb_entry_getlen(unsigned char **buf)
520 /* Count up the sizes of the components of an entry */
521 static int mdb_entry_partsize(struct mdb_info *mdb, MDB_txn *txn, Entry *e,
525 int i, nat = 0, nval = 0;
528 for (a=e->e_attrs; a; a=a->a_next) {
529 /* For AttributeDesc, we only store the attr index */
531 if (!mdb->mi_adxs[a->a_desc->ad_index]) {
532 int rc = mdb_ad_get(mdb, txn, a->a_desc);
536 len += entry_lenlen(mdb->mi_adxs[a->a_desc->ad_index]);
537 for (i=0; a->a_vals[i].bv_val; i++) {
539 len += a->a_vals[i].bv_len + 1;
540 len += entry_lenlen(a->a_vals[i].bv_len);
542 len += entry_lenlen(i);
543 nval++; /* empty berval at end */
544 if (a->a_nvals != a->a_vals) {
545 for (i=0; a->a_nvals[i].bv_val; i++) {
547 len += a->a_nvals[i].bv_len + 1;
548 len += entry_lenlen(a->a_nvals[i].bv_len);
550 len += entry_lenlen(i); /* i nvals */
553 len += entry_lenlen(0); /* 0 nvals */
556 len += entry_lenlen(e->e_ocflags);
557 len += entry_lenlen(nat);
558 len += entry_lenlen(nval);
565 /* Flatten an Entry into a buffer. The buffer is filled with just the
566 * strings/bervals of all the entry components. Each field is preceded
567 * by its length, encoded the way ber_put_len works. Every field is NUL
568 * terminated. The entire buffer size is precomputed so that a single
569 * malloc can be performed. The entry size is also recorded,
570 * to aid in entry_decode.
572 static int mdb_entry_encode(Operation *op, MDB_txn *txn, Entry *e, MDB_val *data)
574 struct mdb_info *mdb = (struct mdb_info *) op->o_bd->be_private;
575 ber_len_t len, dnlen, ndnlen, i;
581 Debug( LDAP_DEBUG_TRACE, "=> mdb_entry_encode(0x%08lx): %s\n",
582 (long) e->e_id, e->e_dn, 0 );
584 if (is_entry_referral(e))
587 rc = mdb_entry_partsize( mdb, txn, e, &eh );
590 data->mv_size = eh.bv.bv_len;
591 data->mv_data = op->o_tmpalloc(data->mv_size, op->o_tmpmemctx);
592 ptr = (unsigned char *)data->mv_data;
593 mdb_entry_putlen(&ptr, eh.nattrs);
594 mdb_entry_putlen(&ptr, eh.nvals);
595 mdb_entry_putlen(&ptr, e->e_ocflags);
597 for (a=e->e_attrs; a; a=a->a_next) {
598 mdb_entry_putlen(&ptr, mdb->mi_adxs[a->a_desc->ad_index]);
600 for (i=0; a->a_vals[i].bv_val; i++);
601 assert( i == a->a_numvals );
602 mdb_entry_putlen(&ptr, i);
603 for (i=0; a->a_vals[i].bv_val; i++) {
604 mdb_entry_putlen(&ptr, a->a_vals[i].bv_len);
605 memcpy(ptr, a->a_vals[i].bv_val,
606 a->a_vals[i].bv_len);
607 ptr += a->a_vals[i].bv_len;
610 if (a->a_nvals != a->a_vals) {
611 mdb_entry_putlen(&ptr, i);
612 for (i=0; a->a_nvals[i].bv_val; i++) {
613 mdb_entry_putlen(&ptr, a->a_nvals[i].bv_len);
614 memcpy(ptr, a->a_nvals[i].bv_val,
615 a->a_nvals[i].bv_len);
616 ptr += a->a_nvals[i].bv_len;
620 mdb_entry_putlen(&ptr, 0);
625 Debug( LDAP_DEBUG_TRACE, "<= mdb_entry_encode(0x%08lx): %s\n",
626 (long) e->e_id, e->e_dn, 0 );
631 /* Retrieve an Entry that was stored using entry_encode above.
633 * Note: everything is stored in a single contiguous block, so
634 * you can not free individual attributes or names from this
635 * structure. Attempting to do so will likely corrupt memory.
638 int mdb_entry_decode(Operation *op, MDB_val *data, Entry **e)
640 struct mdb_info *mdb = (struct mdb_info *) op->o_bd->be_private;
641 int i, j, nattrs, nvals;
646 AttributeDescription *ad;
647 unsigned char *ptr = (unsigned char *)data->mv_data;
650 Debug( LDAP_DEBUG_TRACE,
651 "=> mdb_entry_decode:\n",
654 nattrs = mdb_entry_getlen(&ptr);
655 nvals = mdb_entry_getlen(&ptr);
656 x = mdb_entry_alloc(op, nattrs, nvals);
657 x->e_ocflags = mdb_entry_getlen(&ptr);
664 while ((i = mdb_entry_getlen(&ptr))) {
665 a->a_desc = mdb->mi_ads[i];
666 a->a_flags = SLAP_ATTR_DONT_FREE_DATA | SLAP_ATTR_DONT_FREE_VALS;
667 j = mdb_entry_getlen(&ptr);
672 i = mdb_entry_getlen(&ptr);
674 bptr->bv_val = (char *)ptr;
683 j = mdb_entry_getlen(&ptr);
687 i = mdb_entry_getlen(&ptr);
689 bptr->bv_val = (char *)ptr;
698 a->a_nvals = a->a_vals;
700 /* FIXME: This is redundant once a sorted entry is saved into the DB */
701 if ( a->a_desc->ad_type->sat_flags & SLAP_AT_SORTED_VAL ) {
702 rc = slap_sort_vals( (Modifications *)a, &text, &j, NULL );
703 if ( rc == LDAP_SUCCESS ) {
704 a->a_flags |= SLAP_ATTR_SORTED_VALS;
705 } else if ( rc == LDAP_TYPE_OR_VALUE_EXISTS ) {
706 /* should never happen */
707 Debug( LDAP_DEBUG_ANY,
708 "mdb_entry_decode: attributeType %s value #%d provided more than once\n",
709 a->a_desc->ad_cname.bv_val, j, 0 );
720 Debug(LDAP_DEBUG_TRACE, "<= mdb_entry_decode\n",