]> git.sur5r.net Git - openldap/blob - servers/slapd/back-mdb/id2entry.c
Merge remote-tracking branch 'origin/mdb.RE/0.9'
[openldap] / servers / slapd / back-mdb / id2entry.c
1 /* id2entry.c - routines to deal with the id2entry database */
2 /* $OpenLDAP$ */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4  *
5  * Copyright 2000-2014 The OpenLDAP Foundation.
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted only as authorized by the OpenLDAP
10  * Public License.
11  *
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>.
15  */
16
17 #include "portable.h"
18
19 #include <stdio.h>
20 #include <ac/string.h>
21 #include <ac/errno.h>
22
23 #include "back-mdb.h"
24
25 typedef struct Ecount {
26         ber_len_t len;
27         int nattrs;
28         int nvals;
29         int offset;
30 } Ecount;
31
32 static int mdb_entry_partsize(struct mdb_info *mdb, MDB_txn *txn, Entry *e,
33         Ecount *eh);
34 static int mdb_entry_encode(Operation *op, Entry *e, MDB_val *data,
35         Ecount *ec);
36 static Entry *mdb_entry_alloc( Operation *op, int nattrs, int nvals );
37
38 #define ADD_FLAGS       (MDB_NOOVERWRITE|MDB_APPEND)
39
40 static int mdb_id2entry_put(
41         Operation *op,
42         MDB_txn *txn,
43         MDB_cursor *mc,
44         Entry *e,
45         int flag )
46 {
47         struct mdb_info *mdb = (struct mdb_info *) op->o_bd->be_private;
48         Ecount ec;
49         MDB_val key, data;
50         int rc;
51
52         /* We only store rdns, and they go in the dn2id database. */
53
54         key.mv_data = &e->e_id;
55         key.mv_size = sizeof(ID);
56
57         rc = mdb_entry_partsize( mdb, txn, e, &ec );
58         if (rc)
59                 return LDAP_OTHER;
60
61         flag |= MDB_RESERVE;
62
63         if (e->e_id < mdb->mi_nextid)
64                 flag &= ~MDB_APPEND;
65
66         if (mdb->mi_maxentrysize && ec.len > mdb->mi_maxentrysize)
67                 return LDAP_ADMINLIMIT_EXCEEDED;
68
69 again:
70         data.mv_size = ec.len;
71         if ( mc )
72                 rc = mdb_cursor_put( mc, &key, &data, flag );
73         else
74                 rc = mdb_put( txn, mdb->mi_id2entry, &key, &data, flag );
75         if (rc == MDB_SUCCESS) {
76                 rc = mdb_entry_encode( op, e, &data, &ec );
77                 if( rc != LDAP_SUCCESS )
78                         return rc;
79         }
80         if (rc) {
81                 /* Was there a hole from slapadd? */
82                 if ( (flag & MDB_NOOVERWRITE) && data.mv_size == 0 ) {
83                         flag &= ~ADD_FLAGS;
84                         goto again;
85                 }
86                 Debug( LDAP_DEBUG_ANY,
87                         "mdb_id2entry_put: mdb_put failed: %s(%d) \"%s\"\n",
88                         mdb_strerror(rc), rc,
89                         e->e_nname.bv_val );
90                 if ( rc != MDB_KEYEXIST )
91                         rc = LDAP_OTHER;
92         }
93         return rc;
94 }
95
96 /*
97  * This routine adds (or updates) an entry on disk.
98  * The cache should be already be updated.
99  */
100
101
102 int mdb_id2entry_add(
103         Operation *op,
104         MDB_txn *txn,
105         MDB_cursor *mc,
106         Entry *e )
107 {
108         return mdb_id2entry_put(op, txn, mc, e, ADD_FLAGS);
109 }
110
111 int mdb_id2entry_update(
112         Operation *op,
113         MDB_txn *txn,
114         MDB_cursor *mc,
115         Entry *e )
116 {
117         return mdb_id2entry_put(op, txn, mc, e, 0);
118 }
119
120 int mdb_id2edata(
121         Operation *op,
122         MDB_cursor *mc,
123         ID id,
124         MDB_val *data )
125 {
126         MDB_val key;
127         int rc;
128
129         key.mv_data = &id;
130         key.mv_size = sizeof(ID);
131
132         /* fetch it */
133         rc = mdb_cursor_get( mc, &key, data, MDB_SET );
134         /* stubs from missing parents - DB is actually invalid */
135         if ( rc == MDB_SUCCESS && !data->mv_size )
136                 rc = MDB_NOTFOUND;
137         return rc;
138 }
139
140 int mdb_id2entry(
141         Operation *op,
142         MDB_cursor *mc,
143         ID id,
144         Entry **e )
145 {
146         struct mdb_info *mdb = (struct mdb_info *) op->o_bd->be_private;
147         MDB_val key, data;
148         int rc = 0;
149
150         *e = NULL;
151
152         key.mv_data = &id;
153         key.mv_size = sizeof(ID);
154
155         /* fetch it */
156         rc = mdb_cursor_get( mc, &key, &data, MDB_SET );
157         if ( rc == MDB_NOTFOUND ) {
158                 /* Looking for root entry on an empty-dn suffix? */
159                 if ( !id && BER_BVISEMPTY( &op->o_bd->be_nsuffix[0] )) {
160                         struct berval gluebv = BER_BVC("glue");
161                         Entry *r = mdb_entry_alloc(op, 2, 4);
162                         Attribute *a = r->e_attrs;
163                         struct berval *bptr;
164
165                         r->e_id = 0;
166                         r->e_ocflags = SLAP_OC_GLUE|SLAP_OC__END;
167                         bptr = a->a_vals;
168                         a->a_flags = SLAP_ATTR_DONT_FREE_DATA | SLAP_ATTR_DONT_FREE_VALS;
169                         a->a_desc = slap_schema.si_ad_objectClass;
170                         a->a_nvals = a->a_vals;
171                         a->a_numvals = 1;
172                         *bptr++ = gluebv;
173                         BER_BVZERO(bptr);
174                         bptr++;
175                         a->a_next = a+1;
176                         a = a->a_next;
177                         a->a_flags = SLAP_ATTR_DONT_FREE_DATA | SLAP_ATTR_DONT_FREE_VALS;
178                         a->a_desc = slap_schema.si_ad_structuralObjectClass;
179                         a->a_vals = bptr;
180                         a->a_nvals = a->a_vals;
181                         a->a_numvals = 1;
182                         *bptr++ = gluebv;
183                         BER_BVZERO(bptr);
184                         a->a_next = NULL;
185                         *e = r;
186                         return MDB_SUCCESS;
187                 }
188         }
189         /* stubs from missing parents - DB is actually invalid */
190         if ( rc == MDB_SUCCESS && !data.mv_size )
191                 rc = MDB_NOTFOUND;
192         if ( rc ) return rc;
193
194         rc = mdb_entry_decode( op, mdb_cursor_txn( mc ), &data, e );
195         if ( rc ) return rc;
196
197         (*e)->e_id = id;
198         (*e)->e_name.bv_val = NULL;
199         (*e)->e_nname.bv_val = NULL;
200
201         return rc;
202 }
203
204 int mdb_id2entry_delete(
205         BackendDB *be,
206         MDB_txn *tid,
207         Entry *e )
208 {
209         struct mdb_info *mdb = (struct mdb_info *) be->be_private;
210         MDB_dbi dbi = mdb->mi_id2entry;
211         MDB_val key;
212         int rc;
213
214         key.mv_data = &e->e_id;
215         key.mv_size = sizeof(ID);
216
217         /* delete from database */
218         rc = mdb_del( tid, dbi, &key, NULL );
219
220         return rc;
221 }
222
223 static Entry * mdb_entry_alloc(
224         Operation *op,
225         int nattrs,
226         int nvals )
227 {
228         Entry *e = op->o_tmpalloc( sizeof(Entry) +
229                 nattrs * sizeof(Attribute) +
230                 nvals * sizeof(struct berval), op->o_tmpmemctx );
231         BER_BVZERO(&e->e_bv);
232         e->e_private = e;
233         if (nattrs) {
234                 e->e_attrs = (Attribute *)(e+1);
235                 e->e_attrs->a_vals = (struct berval *)(e->e_attrs+nattrs);
236         } else {
237                 e->e_attrs = NULL;
238         }
239
240         return e;
241 }
242
243 int mdb_entry_return(
244         Operation *op,
245         Entry *e
246 )
247 {
248         if ( !e )
249                 return 0;
250         if ( e->e_private ) {
251                 if ( op->o_hdr && op->o_tmpmfuncs ) {
252                         op->o_tmpfree( e->e_nname.bv_val, op->o_tmpmemctx );
253                         op->o_tmpfree( e->e_name.bv_val, op->o_tmpmemctx );
254                         op->o_tmpfree( e, op->o_tmpmemctx );
255                 } else {
256                         ch_free( e->e_nname.bv_val );
257                         ch_free( e->e_name.bv_val );
258                         ch_free( e );
259                 }
260         } else {
261                 entry_free( e );
262         }
263         return 0;
264 }
265
266 int mdb_entry_release(
267         Operation *op,
268         Entry *e,
269         int rw )
270 {
271         struct mdb_info *mdb = (struct mdb_info *) op->o_bd->be_private;
272         struct mdb_op_info *moi = NULL;
273         int rc;
274  
275         /* slapMode : SLAP_SERVER_MODE, SLAP_TOOL_MODE,
276                         SLAP_TRUNCATE_MODE, SLAP_UNDEFINED_MODE */
277  
278         int release = 1;
279         if ( slapMode & SLAP_SERVER_MODE ) {
280                 OpExtra *oex;
281                 LDAP_SLIST_FOREACH( oex, &op->o_extra, oe_next ) {
282                         release = 0;
283                         if ( oex->oe_key == mdb ) {
284                                 mdb_entry_return( op, e );
285                                 moi = (mdb_op_info *)oex;
286                                 /* If it was setup by entry_get we should probably free it */
287                                 if ( moi->moi_flag & MOI_FREEIT ) {
288                                         moi->moi_ref--;
289                                         if ( moi->moi_ref < 1 ) {
290                                                 mdb_txn_reset( moi->moi_txn );
291                                                 moi->moi_ref = 0;
292                                                 LDAP_SLIST_REMOVE( &op->o_extra, &moi->moi_oe, OpExtra, oe_next );
293                                                 op->o_tmpfree( moi, op->o_tmpmemctx );
294                                         }
295                                 }
296                                 break;
297                         }
298                 }
299         }
300
301         if (release)
302                 mdb_entry_return( op, e );
303  
304         return 0;
305 }
306
307 /* return LDAP_SUCCESS IFF we can retrieve the specified entry.
308  */
309 int mdb_entry_get(
310         Operation *op,
311         struct berval *ndn,
312         ObjectClass *oc,
313         AttributeDescription *at,
314         int rw,
315         Entry **ent )
316 {
317         struct mdb_info *mdb = (struct mdb_info *) op->o_bd->be_private;
318         struct mdb_op_info *moi = NULL;
319         MDB_txn *txn = NULL;
320         Entry *e = NULL;
321         int     rc;
322         const char *at_name = at ? at->ad_cname.bv_val : "(null)";
323
324         Debug( LDAP_DEBUG_ARGS,
325                 "=> mdb_entry_get: ndn: \"%s\"\n", ndn->bv_val, 0, 0 ); 
326         Debug( LDAP_DEBUG_ARGS,
327                 "=> mdb_entry_get: oc: \"%s\", at: \"%s\"\n",
328                 oc ? oc->soc_cname.bv_val : "(null)", at_name, 0);
329
330         rc = mdb_opinfo_get( op, mdb, rw == 0, &moi );
331         if ( rc )
332                 return LDAP_OTHER;
333         txn = moi->moi_txn;
334
335         /* can we find entry */
336         rc = mdb_dn2entry( op, txn, NULL, ndn, &e, NULL, 0 );
337         switch( rc ) {
338         case MDB_NOTFOUND:
339         case 0:
340                 break;
341         default:
342                 return (rc != LDAP_BUSY) ? LDAP_OTHER : LDAP_BUSY;
343         }
344         if (e == NULL) {
345                 Debug( LDAP_DEBUG_ACL,
346                         "=> mdb_entry_get: cannot find entry: \"%s\"\n",
347                                 ndn->bv_val, 0, 0 ); 
348                 rc = LDAP_NO_SUCH_OBJECT;
349                 goto return_results;
350         }
351         
352         Debug( LDAP_DEBUG_ACL,
353                 "=> mdb_entry_get: found entry: \"%s\"\n",
354                 ndn->bv_val, 0, 0 ); 
355
356         if ( oc && !is_entry_objectclass( e, oc, 0 )) {
357                 Debug( LDAP_DEBUG_ACL,
358                         "<= mdb_entry_get: failed to find objectClass %s\n",
359                         oc->soc_cname.bv_val, 0, 0 ); 
360                 rc = LDAP_NO_SUCH_ATTRIBUTE;
361                 goto return_results;
362         }
363
364         /* NOTE: attr_find() or attrs_find()? */
365         if ( at && attr_find( e->e_attrs, at ) == NULL ) {
366                 Debug( LDAP_DEBUG_ACL,
367                         "<= mdb_entry_get: failed to find attribute %s\n",
368                         at->ad_cname.bv_val, 0, 0 ); 
369                 rc = LDAP_NO_SUCH_ATTRIBUTE;
370                 goto return_results;
371         }
372
373 return_results:
374         if( rc != LDAP_SUCCESS ) {
375                 /* free entry */
376                 mdb_entry_release( op, e, rw );
377         } else {
378                 *ent = e;
379         }
380
381         Debug( LDAP_DEBUG_TRACE,
382                 "mdb_entry_get: rc=%d\n",
383                 rc, 0, 0 ); 
384         return(rc);
385 }
386
387 static void
388 mdb_reader_free( void *key, void *data )
389 {
390         MDB_txn *txn = data;
391
392         if ( txn ) mdb_txn_abort( txn );
393 }
394
395 /* free up any keys used by the main thread */
396 void
397 mdb_reader_flush( MDB_env *env )
398 {
399         void *data;
400         void *ctx = ldap_pvt_thread_pool_context();
401
402         if ( !ldap_pvt_thread_pool_getkey( ctx, env, &data, NULL ) ) {
403                 ldap_pvt_thread_pool_setkey( ctx, env, NULL, 0, NULL, NULL );
404                 mdb_reader_free( env, data );
405         }
406 }
407
408 extern MDB_txn *mdb_tool_txn;
409
410 int
411 mdb_opinfo_get( Operation *op, struct mdb_info *mdb, int rdonly, mdb_op_info **moip )
412 {
413         int rc, renew = 0;
414         void *data;
415         void *ctx;
416         mdb_op_info *moi = NULL;
417         OpExtra *oex;
418
419         assert( op != NULL );
420
421         if ( !mdb || !moip ) return -1;
422
423         /* If no op was provided, try to find the ctx anyway... */
424         if ( op ) {
425                 ctx = op->o_threadctx;
426         } else {
427                 ctx = ldap_pvt_thread_pool_context();
428         }
429
430         if ( op ) {
431                 LDAP_SLIST_FOREACH( oex, &op->o_extra, oe_next ) {
432                         if ( oex->oe_key == mdb ) break;
433                 }
434                 moi = (mdb_op_info *)oex;
435         }
436
437         if ( !moi ) {
438                 moi = *moip;
439
440                 if ( !moi ) {
441                         if ( op ) {
442                                 moi = op->o_tmpalloc(sizeof(struct mdb_op_info),op->o_tmpmemctx);
443                         } else {
444                                 moi = ch_malloc(sizeof(mdb_op_info));
445                         }
446                         moi->moi_flag = MOI_FREEIT;
447                         *moip = moi;
448                 }
449                 LDAP_SLIST_INSERT_HEAD( &op->o_extra, &moi->moi_oe, oe_next );
450                 moi->moi_oe.oe_key = mdb;
451                 moi->moi_ref = 0;
452                 moi->moi_txn = NULL;
453         }
454
455         if ( !rdonly ) {
456                 /* This op started as a reader, but now wants to write. */
457                 if ( moi->moi_flag & MOI_READER ) {
458                         moi = *moip;
459                         LDAP_SLIST_INSERT_HEAD( &op->o_extra, &moi->moi_oe, oe_next );
460                 } else {
461                 /* This op is continuing an existing write txn */
462                         *moip = moi;
463                 }
464                 moi->moi_ref++;
465                 if ( !moi->moi_txn ) {
466                         if (( slapMode & SLAP_TOOL_MODE ) && mdb_tool_txn ) {
467                                 moi->moi_txn = mdb_tool_txn;
468                         } else {
469                                 rc = mdb_txn_begin( mdb->mi_dbenv, NULL, 0, &moi->moi_txn );
470                                 if (rc) {
471                                         Debug( LDAP_DEBUG_ANY, "mdb_opinfo_get: err %s(%d)\n",
472                                                 mdb_strerror(rc), rc, 0 );
473                                 }
474                                 return rc;
475                         }
476                 }
477                 return 0;
478         }
479
480         /* OK, this is a reader */
481         if ( !moi->moi_txn ) {
482                 if (( slapMode & SLAP_TOOL_MODE ) && mdb_tool_txn ) {
483                         moi->moi_txn = mdb_tool_txn;
484                         goto ok;
485                 }
486                 if ( !ctx ) {
487                         /* Shouldn't happen unless we're single-threaded */
488                         rc = mdb_txn_begin( mdb->mi_dbenv, NULL, MDB_RDONLY, &moi->moi_txn );
489                         if (rc) {
490                                 Debug( LDAP_DEBUG_ANY, "mdb_opinfo_get: err %s(%d)\n",
491                                         mdb_strerror(rc), rc, 0 );
492                         }
493                         return rc;
494                 }
495                 if ( ldap_pvt_thread_pool_getkey( ctx, mdb->mi_dbenv, &data, NULL ) ) {
496                         rc = mdb_txn_begin( mdb->mi_dbenv, NULL, MDB_RDONLY, &moi->moi_txn );
497                         if (rc) {
498                                 Debug( LDAP_DEBUG_ANY, "mdb_opinfo_get: err %s(%d)\n",
499                                         mdb_strerror(rc), rc, 0 );
500                                 return rc;
501                         }
502                         data = moi->moi_txn;
503                         if ( ( rc = ldap_pvt_thread_pool_setkey( ctx, mdb->mi_dbenv,
504                                 data, mdb_reader_free, NULL, NULL ) ) ) {
505                                 mdb_txn_abort( moi->moi_txn );
506                                 moi->moi_txn = NULL;
507                                 Debug( LDAP_DEBUG_ANY, "mdb_opinfo_get: thread_pool_setkey failed err (%d)\n",
508                                         rc, 0, 0 );
509                                 return rc;
510                         }
511                 } else {
512                         moi->moi_txn = data;
513                         renew = 1;
514                 }
515                 moi->moi_flag |= MOI_READER;
516         }
517 ok:
518         if ( moi->moi_ref < 1 ) {
519                 moi->moi_ref = 0;
520         }
521         if ( renew ) {
522                 rc = mdb_txn_renew( moi->moi_txn );
523                 assert(!rc);
524         }
525         moi->moi_ref++;
526         if ( *moip != moi )
527                 *moip = moi;
528
529         return 0;
530 }
531
532 #ifdef LDAP_X_TXN
533 int mdb_txn( Operation *op, int txnop, OpExtra **ptr )
534 {
535         struct mdb_info *mdb = (struct mdb_info *) op->o_bd->be_private;
536         mdb_op_info **moip = (mdb_op_info **)ptr, *moi = *moip;
537         int rc;
538
539         switch( txnop ) {
540         case SLAP_TXN_BEGIN:
541                 return mdb_opinfo_get( op, mdb, 0, moip );
542         case SLAP_TXN_COMMIT:
543                 rc = mdb_txn_commit( moi->moi_txn );
544                 op->o_tmpfree( moi, op->o_tmpmemctx );
545                 return rc;
546         case SLAP_TXN_ABORT:
547                 mdb_txn_abort( moi->moi_txn );
548                 op->o_tmpfree( moi, op->o_tmpmemctx );
549                 return 0;
550         }
551         return LDAP_OTHER;
552 }
553 #endif
554
555 /* Count up the sizes of the components of an entry */
556 static int mdb_entry_partsize(struct mdb_info *mdb, MDB_txn *txn, Entry *e,
557         Ecount *eh)
558 {
559         ber_len_t len;
560         int i, nat = 0, nval = 0, nnval = 0;
561         Attribute *a;
562
563         len = 4*sizeof(int);    /* nattrs, nvals, ocflags, offset */
564         for (a=e->e_attrs; a; a=a->a_next) {
565                 /* For AttributeDesc, we only store the attr index */
566                 nat++;
567                 if (a->a_desc->ad_index >= MDB_MAXADS) {
568                         Debug( LDAP_DEBUG_ANY, "mdb_entry_partsize: too many AttributeDescriptions used\n",
569                                 0, 0, 0 );
570                         return LDAP_OTHER;
571                 }
572                 if (!mdb->mi_adxs[a->a_desc->ad_index]) {
573                         int rc = mdb_ad_get(mdb, txn, a->a_desc);
574                         if (rc)
575                                 return rc;
576                 }
577                 len += 2*sizeof(int);   /* AD index, numvals */
578                 nval += a->a_numvals + 1;       /* empty berval at end */
579                 for (i=0; i<a->a_numvals; i++) {
580                         len += a->a_vals[i].bv_len + 1 + sizeof(int);   /* len */
581                 }
582                 if (a->a_nvals != a->a_vals) {
583                         nval += a->a_numvals + 1;
584                         nnval++;
585                         for (i=0; i<a->a_numvals; i++) {
586                                 len += a->a_nvals[i].bv_len + 1 + sizeof(int);;
587                         }
588                 }
589         }
590         /* padding */
591         len = (len + sizeof(ID)-1) & ~(sizeof(ID)-1);
592         eh->len = len;
593         eh->nattrs = nat;
594         eh->nvals = nval;
595         eh->offset = nat + nval - nnval;
596         return 0;
597 }
598
599 #define HIGH_BIT (1<<(sizeof(unsigned int)*CHAR_BIT-1))
600
601 /* Flatten an Entry into a buffer. The buffer starts with the count of the
602  * number of attributes in the entry, the total number of values in the
603  * entry, and the e_ocflags. It then contains a list of integers for each
604  * attribute. For each attribute the first integer gives the index of the
605  * matching AttributeDescription, followed by the number of values in the
606  * attribute. If the high bit of the attr index is set, the attribute's
607  * values are already sorted.
608  * If the high bit of numvals is set, the attribute also has normalized
609  * values present. (Note - a_numvals is an unsigned int, so this means
610  * it's possible to receive an attribute that we can't encode due to size
611  * overflow. In practice, this should not be an issue.) Then the length
612  * of each value is listed. If there are normalized values, their lengths
613  * come next. This continues for each attribute. After all of the lengths
614  * for the last attribute, the actual values are copied, with a NUL
615  * terminator after each value. The buffer is padded to the sizeof(ID).
616  * The entire buffer size is precomputed so that a single malloc can be
617  * performed.
618  */
619 static int mdb_entry_encode(Operation *op, Entry *e, MDB_val *data, Ecount *eh)
620 {
621         struct mdb_info *mdb = (struct mdb_info *) op->o_bd->be_private;
622         ber_len_t len, i;
623         int rc;
624         Attribute *a;
625         unsigned char *ptr;
626         unsigned int *lp, l;
627
628         Debug( LDAP_DEBUG_TRACE, "=> mdb_entry_encode(0x%08lx): %s\n",
629                 (long) e->e_id, e->e_dn, 0 );
630
631         /* make sure e->e_ocflags is set */
632         if (is_entry_referral(e))
633                 ;       /* empty */
634
635         lp = (unsigned int *)data->mv_data;
636         *lp++ = eh->nattrs;
637         *lp++ = eh->nvals;
638         *lp++ = (unsigned int)e->e_ocflags;
639         *lp++ = eh->offset;
640         ptr = (unsigned char *)(lp + eh->offset);
641
642         for (a=e->e_attrs; a; a=a->a_next) {
643                 if (!a->a_desc->ad_index)
644                         return LDAP_UNDEFINED_TYPE;
645                 l = mdb->mi_adxs[a->a_desc->ad_index];
646                 if (a->a_flags & SLAP_ATTR_SORTED_VALS)
647                         l |= HIGH_BIT;
648                 *lp++ = l;
649                 l = a->a_numvals;
650                 if (a->a_nvals != a->a_vals)
651                         l |= HIGH_BIT;
652                 *lp++ = l;
653                 if (a->a_vals) {
654                         for (i=0; a->a_vals[i].bv_val; i++);
655                         assert( i == a->a_numvals );
656                         for (i=0; i<a->a_numvals; i++) {
657                                 *lp++ = a->a_vals[i].bv_len;
658                                 memcpy(ptr, a->a_vals[i].bv_val,
659                                         a->a_vals[i].bv_len);
660                                 ptr += a->a_vals[i].bv_len;
661                                 *ptr++ = '\0';
662                         }
663                         if (a->a_nvals != a->a_vals) {
664                                 for (i=0; i<a->a_numvals; i++) {
665                                         *lp++ = a->a_nvals[i].bv_len;
666                                         memcpy(ptr, a->a_nvals[i].bv_val,
667                                                 a->a_nvals[i].bv_len);
668                                         ptr += a->a_nvals[i].bv_len;
669                                         *ptr++ = '\0';
670                                 }
671                         }
672                 }
673         }
674
675         Debug( LDAP_DEBUG_TRACE, "<= mdb_entry_encode(0x%08lx): %s\n",
676                 (long) e->e_id, e->e_dn, 0 );
677
678         return 0;
679 }
680
681 /* Retrieve an Entry that was stored using entry_encode above.
682  *
683  * Note: everything is stored in a single contiguous block, so
684  * you can not free individual attributes or names from this
685  * structure. Attempting to do so will likely corrupt memory.
686  */
687
688 int mdb_entry_decode(Operation *op, MDB_txn *txn, MDB_val *data, Entry **e)
689 {
690         struct mdb_info *mdb = (struct mdb_info *) op->o_bd->be_private;
691         int i, j, nattrs, nvals;
692         int rc;
693         Attribute *a;
694         Entry *x;
695         const char *text;
696         AttributeDescription *ad;
697         unsigned int *lp = (unsigned int *)data->mv_data;
698         unsigned char *ptr;
699         BerVarray bptr;
700
701         Debug( LDAP_DEBUG_TRACE,
702                 "=> mdb_entry_decode:\n",
703                 0, 0, 0 );
704
705         nattrs = *lp++;
706         nvals = *lp++;
707         x = mdb_entry_alloc(op, nattrs, nvals);
708         x->e_ocflags = *lp++;
709         if (!nvals) {
710                 goto done;
711         }
712         a = x->e_attrs;
713         bptr = a->a_vals;
714         i = *lp++;
715         ptr = (unsigned char *)(lp + i);
716
717         for (;nattrs>0; nattrs--) {
718                 int have_nval = 0;
719                 a->a_flags = SLAP_ATTR_DONT_FREE_DATA | SLAP_ATTR_DONT_FREE_VALS;
720                 i = *lp++;
721                 if (i & HIGH_BIT) {
722                         i ^= HIGH_BIT;
723                         a->a_flags |= SLAP_ATTR_SORTED_VALS;
724                 }
725                 if (i > mdb->mi_numads) {
726                         rc = mdb_ad_read(mdb, txn);
727                         if (rc)
728                                 return rc;
729                         if (i > mdb->mi_numads) {
730                                 Debug( LDAP_DEBUG_ANY,
731                                         "mdb_entry_decode: attribute index %d not recognized\n",
732                                         i, 0, 0 );
733                                 return LDAP_OTHER;
734                         }
735                 }
736                 a->a_desc = mdb->mi_ads[i];
737                 a->a_numvals = *lp++;
738                 if (a->a_numvals & HIGH_BIT) {
739                         a->a_numvals ^= HIGH_BIT;
740                         have_nval = 1;
741                 }
742                 a->a_vals = bptr;
743                 for (i=0; i<a->a_numvals; i++) {
744                         bptr->bv_len = *lp++;;
745                         bptr->bv_val = (char *)ptr;
746                         ptr += bptr->bv_len+1;
747                         bptr++;
748                 }
749                 bptr->bv_val = NULL;
750                 bptr->bv_len = 0;
751                 bptr++;
752
753                 if (have_nval) {
754                         a->a_nvals = bptr;
755                         for (i=0; i<a->a_numvals; i++) {
756                                 bptr->bv_len = *lp++;
757                                 bptr->bv_val = (char *)ptr;
758                                 ptr += bptr->bv_len+1;
759                                 bptr++;
760                         }
761                         bptr->bv_val = NULL;
762                         bptr->bv_len = 0;
763                         bptr++;
764                 } else {
765                         a->a_nvals = a->a_vals;
766                 }
767                 /* FIXME: This is redundant once a sorted entry is saved into the DB */
768                 if (( a->a_desc->ad_type->sat_flags & SLAP_AT_SORTED_VAL )
769                         && !(a->a_flags & SLAP_ATTR_SORTED_VALS)) {
770                         rc = slap_sort_vals( (Modifications *)a, &text, &j, NULL );
771                         if ( rc == LDAP_SUCCESS ) {
772                                 a->a_flags |= SLAP_ATTR_SORTED_VALS;
773                         } else if ( rc == LDAP_TYPE_OR_VALUE_EXISTS ) {
774                                 /* should never happen */
775                                 Debug( LDAP_DEBUG_ANY,
776                                         "mdb_entry_decode: attributeType %s value #%d provided more than once\n",
777                                         a->a_desc->ad_cname.bv_val, j, 0 );
778                                 return rc;
779                         }
780                 }
781                 a->a_next = a+1;
782                 a = a->a_next;
783         }
784         a[-1].a_next = NULL;
785 done:
786
787         Debug(LDAP_DEBUG_TRACE, "<= mdb_entry_decode\n",
788                 0, 0, 0 );
789         *e = x;
790         return 0;
791 }