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, Entry *e, MDB_val *data,
36 static Entry *mdb_entry_alloc( Operation *op, int nattrs, int nvals );
38 static int mdb_id2entry_put(
45 struct mdb_info *mdb = (struct mdb_info *) op->o_bd->be_private;
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, txn, e, &ec );
62 data.mv_size = ec.len;
64 rc = mdb_cursor_put( mc, &key, &data, flag );
66 rc = mdb_put( txn, mdb->mi_id2entry, &key, &data, flag );
67 if (rc == MDB_SUCCESS) {
68 rc = mdb_entry_encode( op, e, &data, &ec );
69 if( rc != LDAP_SUCCESS )
73 /* Was there a hole from slapadd? */
74 if ( (flag & MDB_NOOVERWRITE) && data.mv_size == 0 ) {
75 flag ^= MDB_NOOVERWRITE;
78 Debug( LDAP_DEBUG_ANY,
79 "mdb_id2entry_put: mdb_put failed: %s(%d) \"%s\"\n",
82 if ( rc != MDB_KEYEXIST )
89 * This routine adds (or updates) an entry on disk.
90 * The cache should be already be updated.
100 return mdb_id2entry_put(op, txn, mc, e, MDB_NOOVERWRITE|MDB_APPEND);
103 int mdb_id2entry_update(
109 return mdb_id2entry_put(op, txn, mc, e, 0);
118 struct mdb_info *mdb = (struct mdb_info *) op->o_bd->be_private;
125 key.mv_size = sizeof(ID);
128 rc = mdb_cursor_get( mc, &key, &data, MDB_SET );
129 if ( rc == MDB_NOTFOUND ) {
130 /* Looking for root entry on an empty-dn suffix? */
131 if ( !id && BER_BVISEMPTY( &op->o_bd->be_nsuffix[0] )) {
132 struct berval gluebv = BER_BVC("glue");
133 Entry *r = mdb_entry_alloc(op, 2, 4);
134 Attribute *a = r->e_attrs;
138 r->e_ocflags = SLAP_OC_GLUE|SLAP_OC__END;
140 a->a_flags = SLAP_ATTR_DONT_FREE_DATA | SLAP_ATTR_DONT_FREE_VALS;
141 a->a_desc = slap_schema.si_ad_objectClass;
142 a->a_nvals = a->a_vals;
149 a->a_flags = SLAP_ATTR_DONT_FREE_DATA | SLAP_ATTR_DONT_FREE_VALS;
150 a->a_desc = slap_schema.si_ad_structuralObjectClass;
152 a->a_nvals = a->a_vals;
163 rc = mdb_entry_decode( op, &data, e );
167 (*e)->e_name.bv_val = NULL;
168 (*e)->e_nname.bv_val = NULL;
173 int mdb_id2entry_delete(
178 struct mdb_info *mdb = (struct mdb_info *) be->be_private;
179 MDB_dbi dbi = mdb->mi_id2entry;
183 key.mv_data = &e->e_id;
184 key.mv_size = sizeof(ID);
186 /* delete from database */
187 rc = mdb_del( tid, dbi, &key, NULL );
192 static Entry * mdb_entry_alloc(
197 Entry *e = op->o_tmpalloc( sizeof(Entry) +
198 nattrs * sizeof(Attribute) +
199 nvals * sizeof(struct berval), op->o_tmpmemctx );
200 BER_BVZERO(&e->e_bv);
203 e->e_attrs = (Attribute *)(e+1);
204 e->e_attrs->a_vals = (struct berval *)(e->e_attrs+nattrs);
212 int mdb_entry_return(
217 if ( e->e_private ) {
218 if ( slapMode & SLAP_TOOL_MODE ) {
219 ch_free( e->e_nname.bv_val );
220 ch_free( e->e_name.bv_val );
223 op->o_tmpfree( e->e_nname.bv_val, op->o_tmpmemctx );
224 op->o_tmpfree( e->e_name.bv_val, op->o_tmpmemctx );
225 op->o_tmpfree( e, op->o_tmpmemctx );
233 int mdb_entry_release(
238 struct mdb_info *mdb = (struct mdb_info *) op->o_bd->be_private;
239 struct mdb_op_info *moi = NULL;
242 /* slapMode : SLAP_SERVER_MODE, SLAP_TOOL_MODE,
243 SLAP_TRUNCATE_MODE, SLAP_UNDEFINED_MODE */
245 mdb_entry_return( op, e );
246 if ( slapMode == SLAP_SERVER_MODE ) {
248 LDAP_SLIST_FOREACH( oex, &op->o_extra, oe_next ) {
249 if ( oex->oe_key == mdb ) {
250 moi = (mdb_op_info *)oex;
251 /* If it was setup by entry_get we should probably free it */
252 if ( moi->moi_flag & MOI_FREEIT ) {
254 if ( moi->moi_ref < 1 ) {
255 mdb_txn_reset( moi->moi_txn );
257 LDAP_SLIST_REMOVE( &op->o_extra, &moi->moi_oe, OpExtra, oe_next );
258 op->o_tmpfree( moi, op->o_tmpmemctx );
269 /* return LDAP_SUCCESS IFF we can retrieve the specified entry.
275 AttributeDescription *at,
279 struct mdb_info *mdb = (struct mdb_info *) op->o_bd->be_private;
280 struct mdb_op_info *moi = NULL;
284 const char *at_name = at ? at->ad_cname.bv_val : "(null)";
286 Debug( LDAP_DEBUG_ARGS,
287 "=> mdb_entry_get: ndn: \"%s\"\n", ndn->bv_val, 0, 0 );
288 Debug( LDAP_DEBUG_ARGS,
289 "=> mdb_entry_get: oc: \"%s\", at: \"%s\"\n",
290 oc ? oc->soc_cname.bv_val : "(null)", at_name, 0);
292 rc = mdb_opinfo_get( op, mdb, rw == 0, &moi );
297 /* can we find entry */
298 rc = mdb_dn2entry( op, txn, NULL, ndn, &e, 0 );
304 return (rc != LDAP_BUSY) ? LDAP_OTHER : LDAP_BUSY;
307 Debug( LDAP_DEBUG_ACL,
308 "=> mdb_entry_get: cannot find entry: \"%s\"\n",
310 rc = LDAP_NO_SUCH_OBJECT;
314 Debug( LDAP_DEBUG_ACL,
315 "=> mdb_entry_get: found entry: \"%s\"\n",
318 if ( oc && !is_entry_objectclass( e, oc, 0 )) {
319 Debug( LDAP_DEBUG_ACL,
320 "<= mdb_entry_get: failed to find objectClass %s\n",
321 oc->soc_cname.bv_val, 0, 0 );
322 rc = LDAP_NO_SUCH_ATTRIBUTE;
326 /* NOTE: attr_find() or attrs_find()? */
327 if ( at && attr_find( e->e_attrs, at ) == NULL ) {
328 Debug( LDAP_DEBUG_ACL,
329 "<= mdb_entry_get: failed to find attribute %s\n",
330 at->ad_cname.bv_val, 0, 0 );
331 rc = LDAP_NO_SUCH_ATTRIBUTE;
336 if( rc != LDAP_SUCCESS ) {
339 mdb_entry_return( op, e );
341 if (moi->moi_ref == 1) {
342 LDAP_SLIST_REMOVE( &op->o_extra, &moi->moi_oe, OpExtra, oe_next );
343 mdb_txn_reset( txn );
344 op->o_tmpfree( moi, op->o_tmpmemctx );
350 Debug( LDAP_DEBUG_TRACE,
351 "mdb_entry_get: rc=%d\n",
357 mdb_reader_free( void *key, void *data )
361 if ( txn ) mdb_txn_abort( txn );
364 /* free up any keys used by the main thread */
366 mdb_reader_flush( MDB_env *env )
369 void *ctx = ldap_pvt_thread_pool_context();
371 if ( !ldap_pvt_thread_pool_getkey( ctx, env, &data, NULL ) ) {
372 ldap_pvt_thread_pool_setkey( ctx, env, NULL, 0, NULL, NULL );
373 mdb_reader_free( env, data );
378 mdb_opinfo_get( Operation *op, struct mdb_info *mdb, int rdonly, mdb_op_info **moip )
383 mdb_op_info *moi = NULL;
386 assert( op != NULL );
388 if ( !mdb || !moip ) return -1;
390 /* If no op was provided, try to find the ctx anyway... */
392 ctx = op->o_threadctx;
394 ctx = ldap_pvt_thread_pool_context();
398 LDAP_SLIST_FOREACH( oex, &op->o_extra, oe_next ) {
399 if ( oex->oe_key == mdb ) break;
401 moi = (mdb_op_info *)oex;
409 moi = op->o_tmpalloc(sizeof(struct mdb_op_info),op->o_tmpmemctx);
411 moi = ch_malloc(sizeof(mdb_op_info));
413 moi->moi_flag = MOI_FREEIT;
416 LDAP_SLIST_INSERT_HEAD( &op->o_extra, &moi->moi_oe, oe_next );
417 moi->moi_oe.oe_key = mdb;
423 /* This op started as a reader, but now wants to write. */
424 if ( moi->moi_flag & MOI_READER ) {
426 LDAP_SLIST_INSERT_HEAD( &op->o_extra, &moi->moi_oe, oe_next );
428 /* This op is continuing an existing write txn */
432 if ( !moi->moi_txn ) {
433 rc = mdb_txn_begin( mdb->mi_dbenv, NULL, 0, &moi->moi_txn );
435 Debug( LDAP_DEBUG_ANY, "mdb_opinfo_get: err %s(%d)\n",
436 mdb_strerror(rc), rc, 0 );
443 /* OK, this is a reader */
444 if ( !moi->moi_txn ) {
446 /* Shouldn't happen unless we're single-threaded */
447 rc = mdb_txn_begin( mdb->mi_dbenv, NULL, MDB_RDONLY, &moi->moi_txn );
449 Debug( LDAP_DEBUG_ANY, "mdb_opinfo_get: err %s(%d)\n",
450 mdb_strerror(rc), rc, 0 );
454 if ( ldap_pvt_thread_pool_getkey( ctx, mdb->mi_dbenv, &data, NULL ) ) {
455 rc = mdb_txn_begin( mdb->mi_dbenv, NULL, MDB_RDONLY, &moi->moi_txn );
457 Debug( LDAP_DEBUG_ANY, "mdb_opinfo_get: err %s(%d)\n",
458 mdb_strerror(rc), rc, 0 );
462 if ( ( rc = ldap_pvt_thread_pool_setkey( ctx, mdb->mi_dbenv,
463 data, mdb_reader_free, NULL, NULL ) ) ) {
464 mdb_txn_abort( moi->moi_txn );
466 Debug( LDAP_DEBUG_ANY, "mdb_opinfo_get: thread_pool_setkey failed err (%d)\n",
474 moi->moi_flag |= MOI_READER;
476 if ( moi->moi_ref < 1 ) {
480 mdb_txn_renew( moi->moi_txn );
489 /* Count up the sizes of the components of an entry */
490 static int mdb_entry_partsize(struct mdb_info *mdb, MDB_txn *txn, Entry *e,
494 int i, nat = 0, nval = 0, nnval = 0;
497 len = 4*sizeof(int); /* nattrs, nvals, ocflags, offset */
498 for (a=e->e_attrs; a; a=a->a_next) {
499 /* For AttributeDesc, we only store the attr index */
501 if (!mdb->mi_adxs[a->a_desc->ad_index]) {
502 int rc = mdb_ad_get(mdb, txn, a->a_desc);
506 len += 2*sizeof(int); /* AD index, numvals */
507 nval += a->a_numvals + 1; /* empty berval at end */
508 for (i=0; i<a->a_numvals; i++) {
509 len += a->a_vals[i].bv_len + 1 + sizeof(int); /* len */
511 if (a->a_nvals != a->a_vals) {
512 nval += a->a_numvals + 1;
514 for (i=0; i<a->a_numvals; i++) {
515 len += a->a_nvals[i].bv_len + 1 + sizeof(int);;
520 len = (len + sizeof(ID)-1) & ~(sizeof(ID)-1);
524 eh->offset = nat + nval - nnval;
528 #define HIGH_BIT (1<<(sizeof(unsigned int)*CHAR_BIT-1))
530 /* Flatten an Entry into a buffer. The buffer starts with the count of the
531 * number of attributes in the entry, the total number of values in the
532 * entry, and the e_ocflags. It then contains a list of integers for each
533 * attribute. For each attribute the first integer gives the index of the
534 * matching AttributeDescription, followed by the number of values in the
535 * attribute. If the high bit is set, the attribute also has normalized
536 * values present. (Note - a_numvals is an unsigned int, so this means
537 * it's possible to receive an attribute that we can't encode due to size
538 * overflow. In practice, this should not be an issue.) Then the length
539 * of each value is listed. If there are normalized values, their lengths
540 * come next. This continues for each attribute. After all of the lengths
541 * for the last attribute, the actual values are copied, with a NUL
542 * terminator after each value. The buffer is padded to the sizeof(ID).
543 * The entire buffer size is precomputed so that a single malloc can be
546 static int mdb_entry_encode(Operation *op, Entry *e, MDB_val *data, Ecount *eh)
548 struct mdb_info *mdb = (struct mdb_info *) op->o_bd->be_private;
555 Debug( LDAP_DEBUG_TRACE, "=> mdb_entry_encode(0x%08lx): %s\n",
556 (long) e->e_id, e->e_dn, 0 );
558 /* make sure e->e_ocflags is set */
559 if (is_entry_referral(e))
562 lp = (unsigned int *)data->mv_data;
565 *lp++ = (unsigned int)e->e_ocflags;
567 ptr = (unsigned char *)(lp + eh->offset);
569 for (a=e->e_attrs; a; a=a->a_next) {
570 *lp++ = mdb->mi_adxs[a->a_desc->ad_index];
572 if (a->a_nvals != a->a_vals)
576 for (i=0; a->a_vals[i].bv_val; i++);
577 assert( i == a->a_numvals );
578 for (i=0; i<a->a_numvals; i++) {
579 *lp++ = a->a_vals[i].bv_len;
580 memcpy(ptr, a->a_vals[i].bv_val,
581 a->a_vals[i].bv_len);
582 ptr += a->a_vals[i].bv_len;
585 if (a->a_nvals != a->a_vals) {
586 for (i=0; i<a->a_numvals; i++) {
587 *lp++ = a->a_nvals[i].bv_len;
588 memcpy(ptr, a->a_nvals[i].bv_val,
589 a->a_nvals[i].bv_len);
590 ptr += a->a_nvals[i].bv_len;
597 Debug( LDAP_DEBUG_TRACE, "<= mdb_entry_encode(0x%08lx): %s\n",
598 (long) e->e_id, e->e_dn, 0 );
603 /* Retrieve an Entry that was stored using entry_encode above.
605 * Note: everything is stored in a single contiguous block, so
606 * you can not free individual attributes or names from this
607 * structure. Attempting to do so will likely corrupt memory.
610 int mdb_entry_decode(Operation *op, MDB_val *data, Entry **e)
612 struct mdb_info *mdb = (struct mdb_info *) op->o_bd->be_private;
613 int i, j, nattrs, nvals;
618 AttributeDescription *ad;
619 unsigned int *lp = (unsigned int *)data->mv_data;
623 Debug( LDAP_DEBUG_TRACE,
624 "=> mdb_entry_decode:\n",
629 x = mdb_entry_alloc(op, nattrs, nvals);
630 x->e_ocflags = *lp++;
637 ptr = (unsigned char *)(lp + i);
639 for (;nattrs>0; nattrs--) {
641 a->a_desc = mdb->mi_ads[*lp++];
642 a->a_flags = SLAP_ATTR_DONT_FREE_DATA | SLAP_ATTR_DONT_FREE_VALS;
643 a->a_numvals = *lp++;
644 if (a->a_numvals & HIGH_BIT) {
645 a->a_numvals ^= HIGH_BIT;
649 for (i=0; i<a->a_numvals; i++) {
650 bptr->bv_len = *lp++;;
651 bptr->bv_val = (char *)ptr;
652 ptr += bptr->bv_len+1;
661 for (i=0; i<a->a_numvals; i++) {
662 bptr->bv_len = *lp++;
663 bptr->bv_val = (char *)ptr;
664 ptr += bptr->bv_len+1;
671 a->a_nvals = a->a_vals;
673 /* FIXME: This is redundant once a sorted entry is saved into the DB */
674 if ( a->a_desc->ad_type->sat_flags & SLAP_AT_SORTED_VAL ) {
675 rc = slap_sort_vals( (Modifications *)a, &text, &j, NULL );
676 if ( rc == LDAP_SUCCESS ) {
677 a->a_flags |= SLAP_ATTR_SORTED_VALS;
678 } else if ( rc == LDAP_TYPE_OR_VALUE_EXISTS ) {
679 /* should never happen */
680 Debug( LDAP_DEBUG_ANY,
681 "mdb_entry_decode: attributeType %s value #%d provided more than once\n",
682 a->a_desc->ad_cname.bv_val, j, 0 );
692 Debug(LDAP_DEBUG_TRACE, "<= mdb_entry_decode\n",