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